Access google photos API via Java

匆匆过客 提交于 2020-01-01 19:33:11

问题


I am very new to google API and I am having troubles with it. I red documentation Google photos API for Java, then I created OAuth credentials in google API console and downloaded it (credentials.json file). 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

Could you please provide me with some explanation/links about it?


回答1:


You can create a UserCredentials Object and pass it

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

Go through this answer https://stackoverflow.com/a/54533855/6153171

Or check out this complete project on github https://github.com/erickogi/AndroidGooglePhotosApi




回答2:


The FixedCredentialsProvider.create(..) call takes in a com.google.auth.Credentials object. 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. There you set the refresh token, client ID, client secret, etc. to initialise the credentials. Getting a refresh token requires your app to complete the GoogleAuthorizationCodeFlow that prompts the user for authorization and approval.

You can check out the sample implementation on GitHub, but this is the relevant code:

  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.



来源:https://stackoverflow.com/questions/52762861/access-google-photos-api-via-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!