2019/08/13

C# Google API PhotosLibrary 예제

C# 예제가 거의 전무 하다 시피해서... 한참을  이것 저것 테스트 헤매다가 어케 어케....


using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using Google.Apis.PhotosLibrary.v1;
using Google.Apis.PhotosLibrary.v1.Data;


// 인증

UserCredential credential;

using (var stream = new FileStream("client_secret_oauth.json", FileMode.Open, FileAccess.Read))
{
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        new[] { PhotosLibraryService.Scope.Photoslibrary, PhotosLibraryService.Scope.DrivePhotosReadonly },
        "abcApp",   // 구분값
        CancellationToken.None,
        null).Result;
}

// 앨범 목록

PhotosLibraryService PhotosService = new PhotosLibraryService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
});

var albumsList = PhotosService.Albums.List().Execute();
foreach (var item in albumsList.Albums)
{
    Console.WriteLine("Album title: " + item.Title);
    Console.WriteLine("Album ID: " + item.Id);
}

// 사진 목록

SearchMediaItemsRequest x = new SearchMediaItemsRequest();
x.AlbumId = "AMhW9mr-M9UF3Tpo2";

var mitems = PhotosService.MediaItems.Search(x).Execute();  // 특정 앨범
foreach(var i in mitems.MediaItems)
{
    Console.WriteLine("Id : " +i.Id);
    Console.WriteLine("Filename : " + i.Filename);
    Console.WriteLine("BaseUrl : " + i.BaseUrl);
    Console.WriteLine("ProductUrl : " + i.ProductUrl);
}


댓글 없음:

댓글 쓰기

가장 많이 본 글