LoginSignup
0
1

[Google Photos Library API] JavaでGoogleフォトのアルバムを取得する

Last updated at Posted at 2024-04-16

Google Photos Library API(Java)を使用して、
Googleフォトのアルバムを取得する方法
についてご紹介します。

アルバムはGoogleフォトアプリのアルバムタブから確認することができます。

APIを利用する環境の準備から始める場合や、コードを実行する際は、
⧉[Google Photos Library API(V1)] JavaでPhotos Library APIを使う
を参照ください。

No 目次
1 アルバム一覧を取得
1 スコープ
2 実行
3 レスポンスの内容
2 アルバムを取得
1 スコープ
2 実行
3 レスポンスの内容

1. アルバム一覧を取得

アルバム一覧を取得します。

1.1. スコープ

このAPIを実行するには、以下のいずれかのスコープを指定してください。

https://www.googleapis.com/auth/photoslibrary
https://www.googleapis.com/auth/photoslibrary.readonly
https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata

⧉[Google Photos Library API(V1)] JavaでPhotos Library APIを使う(2.2. PhotosLibraryClientインスタンスを取得)
でスコープを指定してください。

1.2. 実行

public static void main(String[] args) throws Exception{
    try(PhotosLibraryClient client = getPhotosLibraryClient()){
        ListAlbumsRequest.Builder builder = ListAlbumsRequest.newBuilder();
        builder.setPageSize(10);
        
        ListAlbumsPagedResponse  response = client.listAlbums(builder.build());
        for (Album album : response.iterateAll()) {
            System.out.println(album);
        }
    }
}

1.2.1. HTTPリクエスト

GET: https://photoslibrary.googleapis.com/v1/albums
が実行されます。

1.2.2. クエリパラメータ

ListAlbumsRequest.Builderのsetメソッドにより、クエリパラメータを追加できます。
build()メソッドでListAlbumsRequestインスタンスを取得し、
listAlbums()に渡します。

メソッド 引数 説明
setPageSize Integer レスポンスで返すアルバムの最大数
デフォルトは20、最大は50
setPageToken String 結果の次のページを取得するための連続トークン
setExcludeNonAppCreatedData Boolean このアプリ以外で作成されたメディアアイテムを除外するか

1.3. レスポンスの内容

ListAlbumsPagedResponse

メソッド 戻り値 説明
iterateAll Iterable<Album> アルバムリスト
getNextPageToken String アルバムの次のセットを取得するためのトークン

Album

メソッド 戻り値 説明
getId String アルバムID
getTitle String アルバムの名前
getProductUrl String アルバムのGoogleフォトURL
getIsWriteable Boolean アルバム内にメディアアイテムを作成できるか
getShareInfo ShareInfo 共有アルバムに関する情報
getMediaItemsCount Long アルバム内のメディアアイテム数
getCoverPhotoBaseUrl String カバー写真のバイトへのURL
getCoverPhotoMediaItemId String カバー写真に関連付けられているメディアアイテムID

ShareInfo

メソッド 戻り値 説明
getSharedAlbumOptions SharedAlbumOptions メディアアイテムの追加やコメントを許可するか
getShareableUrl String 共有のGoogleフォトアルバムへのリンク
getShareToken String 共有アルバムへの参加、共有アルバムからの退出、または詳細の取得に使用するトークン
getIsJoined Boolean ユーザーがアルバムに参加しているか
getIsOwned Boolean ユーザーがアルバムを所有しているか
getIsJoinable Boolean ユーザーがアルバムに参加できるか

SharedAlbumOptions

メソッド 戻り値 説明
getIsCollaborative Boolean 共有アルバムで共同編集者にメディアアイテムの追加を許可するか
getIsCommentable Boolean 共有アルバムで共同編集者がアルバムにコメントを追加できるようにするか

2. アルバムを取得

指定したアルバムの情報を取得します。

2.1. スコープ

このAPIを実行するには、以下のいずれかのスコープを指定してください。

https://www.googleapis.com/auth/photoslibrary
https://www.googleapis.com/auth/photoslibrary.readonly
https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata

⧉[Google Photos Library API(V1)] JavaでPhotos Library APIを使う(2.2. PhotosLibraryClientインスタンスを取得)
でスコープを指定してください。

2.2. 実行

public static void main(String[] args) throws Exception{
    try(PhotosLibraryClient client = getPhotosLibraryClient()){
        Album album = client.getAlbum("アルバムID");
        System.out.println(album);
    }
}

2.2.1. HTTPリクエスト

GET: https://photoslibrary.googleapis.com/v1/albums/{アルバムID}
が実行されます。

2.2.2. クエリパラメータ

クエリパラメータはありません。

2.3. レスポンスの内容

Album

1.3. レスポンスの内容(Album)と同じです。



おしまい。。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1