繁体   English   中英

通过 Java 访问谷歌照片 API

[英]Access google photos API via Java

我对谷歌 API 很陌生,我遇到了麻烦。 为 Java编写了Google 照片 API文档,然后在 google API 控制台中创建了 OAuth 凭据并下载了它(credentials.json 文件)。 之后我尝试访问谷歌照片。 这是文档中的代码:

// Set up the Photos Library Client that interacts with the API
PhotosLibrarySettings settings =
   PhotosLibrarySettings.newBuilder()
  .setCredentialsProvider(
      FixedCredentialsProvider.create(/* Add credentials here. */)) 
  .build();

try (PhotosLibraryClient photosLibraryClient =
    PhotosLibraryClient.initialize(settings)) {

  // Create a new Album  with at title
  Album createdAlbum = photosLibraryClient.createAlbum("My Album");

  // Get some properties from the album, such as its ID and product URL
  String id = album.getId();
  String url = album.getProductUrl();

 } catch (ApiException e) {
    // Error during album creation
 }

但我不明白如何创建Credentials对象以将其传递给FixedCredentialsProvider.create()方法

你能给我提供一些关于它的解释/链接吗?

您可以创建一个 UserCredentials 对象并传递它

UserCredentials.newBuilder()
        .setClientId("your client id")
        .setClientSecret("your client secret")
        .setAccessToken("Access Token")
        .build()

通过这个答案https://stackoverflow.com/a/54533855/6153171

或者在 github https://github.com/erickogi/AndroidGooglePhotosApi上查看这个完整的项目

FixedCredentialsProvider.create(..)调用接受com.google.auth.Credentials对象 对于 Google Photos Library API,这应该是一个UserCredentials对象,您可以创建UserCredentials.Builder作为Google OAuth 库的一部分 您可以在那里设置刷新令牌、客户端 ID、客户端机密等以初始化凭据。 获取刷新令牌需要您的应用程序完成提示用户进行授权和批准的GoogleAuthorizationCodeFlow

您可以在GitHub 上查看示例实现,但这是相关代码:

GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(
                GoogleNetHttpTransport.newTrustedTransport(),
                JSON_FACTORY,
                clientSecrets,
                selectedScopes)
            .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver =
        new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    return UserCredentials.newBuilder()
        .setClientId(clientId)
        .setClientSecret(clientSecret)
        .setRefreshToken(credential.getRefreshToken())
        .build();

涉及到一些活动部分,但 Google 照片库 API 客户端库与Google 身份验证库一起处理 OAuth 身份验证。

您需要先了解 OAuth 2: https : //www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2

然后你可以看看这个答案https://stackoverflow.com/a/54533855/6153171

P/s: 1. 授权类型:授权码谷歌开发者控制台:凭证是网络客户端。

GoogleApiClient -> addScope, requestServerCode -> 大权限 -> 获取帐户 -> getServerAuthenCode -> 获取 AccessToken ( https://stackoverflow.com/a/54533855/6153171 ) -> 我测试过,效果很好。 你可以按照这个方式

  1. 授予类型:隐式 Google 开发者控制台:凭据是 android 或 ios 应用程序。

GoogleApiClient -> addScope, requestTokenID -> 大权限 -> 获取账号 -> getTokenID -> 获取 AccessToken。 我没有尝试成功授予 google photo api 的授权。 但是通过 firebase 身份验证,我们可以使用这种方式,因为 firebase 为我们支持类 util。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM