简体   繁体   English

通过 Java 访问谷歌照片 API

[英]Access google photos API via Java

I am very new to google API and I am having troubles with it.我对谷歌 API 很陌生,我遇到了麻烦。 I red documentation Google photos API for Java , then I created OAuth credentials in google API console and downloaded it (credentials.json file).为 Java编写了Google 照片 API文档,然后在 google API 控制台中创建了 OAuth 凭据并下载了它(credentials.json 文件)。 After that I tried to access google photos.之后我尝试访问谷歌照片。 Here is code from documentation:这是文档中的代码:

// 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
 }

But I don't understand how to create Credentials object to pass it to the FixedCredentialsProvider.create() method但我不明白如何创建Credentials对象以将其传递给FixedCredentialsProvider.create()方法

Could you please provide me with some explanation/links about it?你能给我提供一些关于它的解释/链接吗?

You can create a UserCredentials Object and pass it您可以创建一个 UserCredentials 对象并传递它

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

Go through this answer https://stackoverflow.com/a/54533855/6153171通过这个答案https://stackoverflow.com/a/54533855/6153171

Or check out this complete project on github https://github.com/erickogi/AndroidGooglePhotosApi或者在 github https://github.com/erickogi/AndroidGooglePhotosApi上查看这个完整的项目

The FixedCredentialsProvider.create(..) call takes in a com.google.auth.Credentials object . FixedCredentialsProvider.create(..)调用接受com.google.auth.Credentials对象 For the Google Photos Library API, this should be a UserCredentials object, that you can create UserCredentials.Builder that is part of the Google OAuth library .对于 Google Photos Library API,这应该是一个UserCredentials对象,您可以创建UserCredentials.Builder作为Google OAuth 库的一部分 There you set the refresh token, client ID, client secret, etc. to initialise the credentials.您可以在那里设置刷新令牌、客户端 ID、客户端机密等以初始化凭据。 Getting a refresh token requires your app to complete the GoogleAuthorizationCodeFlow that prompts the user for authorization and approval.获取刷新令牌需要您的应用程序完成提示用户进行授权和批准的GoogleAuthorizationCodeFlow

You can check out the sample implementation on GitHub , but this is the relevant code:您可以在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();

There are a few moving parts involved, but the Google Photos Library API client library works with the Google Authentication library to handle OAuth authentication.涉及到一些活动部分,但 Google 照片库 API 客户端库与Google 身份验证库一起处理 OAuth 身份验证。

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

Then you can look this answer https://stackoverflow.com/a/54533855/6153171然后你可以看看这个答案https://stackoverflow.com/a/54533855/6153171

P/s: 1. Grant Type: Authorization Code Google developer console : credential is web client. P/s: 1. 授权类型:授权码谷歌开发者控制台:凭证是网络客户端。

GoogleApiClient -> addScope, requestServerCode -> grand permission -> get account -> getServerAuthenCode -> get AccessToken ( https://stackoverflow.com/a/54533855/6153171 ) -> I tested and it works well. GoogleApiClient -> addScope, requestServerCode -> 大权限 -> 获取帐户 -> getServerAuthenCode -> 获取 AccessToken ( https://stackoverflow.com/a/54533855/6153171 ) -> 我测试过,效果很好。 You can follow this way你可以按照这个方式

  1. Grant Type: Implicit Google developer console : credential is android or ios app.授予类型:隐式 Google 开发者控制台:凭据是 android 或 ios 应用程序。

GoogleApiClient -> addScope, requestTokenID -> grand permission -> get account -> getTokenID -> get AccessToken. GoogleApiClient -> addScope, requestTokenID -> 大权限 -> 获取账号 -> getTokenID -> 获取 AccessToken。 I didn't try successfully to grant authorization for google photo api.我没有尝试成功授予 google photo api 的授权。 But with firebase authentication, we can use this way because firebase support class util for us.但是通过 firebase 身份验证,我们可以使用这种方式,因为 firebase 为我们支持类 util。

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

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