Google Credentials, Photos API

2

I'm trying to integrate an APP with Google Photos. 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 creating a photosLibraryClient object requires setting up a client. In the example, you only have one comment saying to enter the credentials. Going in google's GitHub where it is possible to 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 research this file is an application configuration JSON made in the Google Web Console, which registers the use of the APIs.

/** 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 appears in the following line:

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

Someone already authenticated in photos and can you help me? I'm not using APIREST from photos because I do not understand the settings needed to enable the service

    
asked by anonymous 19.09.2018 / 17:40

0 answers