Google Credentials, Photos API

Asked

Viewed 113 times

2

I’m trying to integrate an APP with Google Photos. By following Get Started you can see the following code:

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

Note that to create a photosLibraryClient object you need to configure a client. In the example only has a comment saying to enter the credentials. Going to Google’s Github where you can find some examples, I found a class that generates photosLibraryClient based on scopes that are defined in the class and one more file address. By my searches this file is a JSON of application configuration made in Google Web Console, where the use of Apis is recorded.

/** Creates a new {@link PhotosLibraryClient} instance with credentials and scopes. */
public static PhotosLibraryClient createClient(
        InputStream inputStream, List<String> selectedScopes)
        throws IOException, GeneralSecurityException {
    PhotosLibrarySettings settings =
            PhotosLibrarySettings.newBuilder()
                    .setCredentialsProvider(
                            FixedCredentialsProvider.create(
                                    getUserCredentials(inputStream, selectedScopes)
                            )).build();
    return PhotosLibraryClient.initialize(settings);
}

private static Credentials getUserCredentials(InputStream inputStream,   List<String> selectedScopes)
        throws IOException, GeneralSecurityException {
    GoogleClientSecrets clientSecrets =
            GoogleClientSecrets.load(
                    JSON_FACTORY, new InputStreamReader(inputStream));
    String clientId = clientSecrets.getDetails().getClientId();
    String clientSecret = clientSecrets.getDetails().getClientSecret();

    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();
}

The problem is that when reading the file an Illegalarguments error is presented in the following line:

String clientId = clientSecrets.getDetails().getClientId();

Has anyone ever authenticated the photos and can you help me? I am not using photos APIREST because I do not understand the settings necessary to enable the service

  • you have already made an account on https://developers.google.com/? you usually generate credentiais there for Apis.

  • Yes, I have an account and the app already has access to Apis, including Photos. The credential is for an android application, which gave me a key I saved, and a JSON with the application name and a lot of characters. I also created a client with API access that gave me another JSON called clientsecret. Edit: I tried to load another file, now clientsecret and got an error called "java.security.Nosuchalgorithmexception: JKS Keystore not available". The indicated line of the error is later than the post that had given the first exception, being it " Googlenethttptransport.newTrustedTransport()"

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.