java google photos api

Solutions on MaxInterview for java google photos api by the best coders in the world

showing results for - "java google photos api"
Melvyn
05 Mar 2020
1import com.google.api.client.auth.oauth2.Credential;
2import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
3import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
4import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
5import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
6import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
7import com.google.api.client.json.JsonFactory;
8import com.google.api.client.json.jackson2.JacksonFactory;
9import com.google.api.client.util.store.FileDataStoreFactory;
10import com.google.api.gax.core.FixedCredentialsProvider;
11import com.google.auth.Credentials;
12import com.google.auth.oauth2.UserCredentials;
13import com.google.photos.library.v1.PhotosLibraryClient;
14import com.google.photos.library.v1.PhotosLibrarySettings;
15import com.google.photos.types.proto.Album;
16
17import java.io.FileInputStream;
18import java.io.IOException;
19import java.io.InputStreamReader;
20import java.security.GeneralSecurityException;
21import java.util.Collections;
22import java.util.List;
23
24/** A factory class that helps initialize a {@link PhotosLibraryClient} instance. */
25public class PhotosQuickstart {
26  private static final java.io.File DATA_STORE_DIR = new java.io.File(PhotosQuickstart.class.getResource("/").getPath(), "tokens");
27  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
28  private static final int LOCAL_RECEIVER_PORT = 8888;
29  private static final List<String> SCOPES = Collections.singletonList("https://www.googleapis.com/auth/photoslibrary.appendonly https://www.googleapis.com/auth/photoslibrary.readonly");
30  private static final String CREDS_PATH = "src/main/resources/Photo_Creds.json";
31
32  private PhotosQuickstart() {}
33
34  /** Creates a new {@link PhotosLibraryClient} instance with credentials and scopes. */
35  public static PhotosLibraryClient createClient(String credentialsPath, List<String> selectedScopes) throws IOException, GeneralSecurityException {
36	  PhotosLibrarySettings settings =
37        PhotosLibrarySettings.newBuilder()
38            .setCredentialsProvider(
39                FixedCredentialsProvider.create(
40                    getUserCredentials(credentialsPath, selectedScopes)))
41            .build();
42	  return PhotosLibraryClient.initialize(settings);
43  }
44
45  private static Credentials getUserCredentials(String credentialsPath, List<String> selectedScopes) throws IOException, GeneralSecurityException {
46    GoogleClientSecrets clientSecrets =
47        GoogleClientSecrets.load(
48            JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsPath)));
49    String clientId = clientSecrets.getDetails().getClientId();
50    String clientSecret = clientSecrets.getDetails().getClientSecret();
51
52    GoogleAuthorizationCodeFlow flow =
53        new GoogleAuthorizationCodeFlow.Builder(
54                GoogleNetHttpTransport.newTrustedTransport(),
55                JSON_FACTORY,
56                clientSecrets,
57                selectedScopes)
58            .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR))
59            .setAccessType("offline")
60            .build();
61    LocalServerReceiver receiver =
62        new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build();
63    Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
64    return UserCredentials.newBuilder()
65        .setClientId(clientId)
66        .setClientSecret(clientSecret)
67        .setRefreshToken(credential.getRefreshToken())
68        .build();
69  }
70
71  public static void main(String... args) throws IOException, GeneralSecurityException {
72	// Set up the Photos Library Client that interacts with the API
73	  PhotosLibraryClient service = createClient(CREDS_PATH,SCOPES);
74
75	  
76      // Create a new Album  with at title
77      Album createdAlbum = service.createAlbum("My Album");
78
79      // Get some properties from the album, such as its ID and product URL
80      String id = createdAlbum.getId();
81//      String url = createdAlbum.getProductUrl();
82      
83      System.out.printf("Album created: (%s)",id);
84  }
85}