5

I have java code to connect to google drive using a service account, this works:

JsonFactory JSON_FACTORY = new GsonFactory();

GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("sa.json"))
                .createScoped(List.of(DriveScopes.DRIVE))
                .createDelegated("foo@bar.com");

return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credential)
                .setApplicationName("my-drive-app")
                .build();

However i'm getting a deprecation warning that GoogleCredential is deprecated and that i should use GoogleCredentials (note the extra s at the end) from the google-auth-library instead.

I can initialise GoogleCredentials like this:

GoogleCredentials credential = GoogleCredentials.fromStream(new FileInputStream("sa.json"))
                .createScoped(List.of(DriveScopes.DRIVE))
                .createDelegated("foo@bar.com");

However I can't seem to figure out how to initialise the drive service using this new GoogleCredentials object. Passing it in in place of the credential object gives a compiler error and all of the official google drive java documentation refers the now deprecated method.

Can someone tell me how to initialise google drive using the new credentials object?

Versions I'm using:

    <dependency>
      <groupId>com.google.api-client</groupId>
      <artifactId>google-api-client</artifactId>
      <version>1.32.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.oauth-client</groupId>
      <artifactId>google-oauth-client-jetty</artifactId>
      <version>1.32.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-drive</artifactId>
      <version>v3-rev20210919-1.32.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.auth</groupId>
      <artifactId>google-auth-library-oauth2-http</artifactId>
      <version>1.2.0</version>
    </dependency>
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

1 Answers1

4

To connect to Google Drive use the HttpCredentialsAdapter to make the connection from GoogleCredentials to GoogleCredential.

Sample:

return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credential))
                .setApplicationName("my-drive-app")
                .build();
Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Your example shows me how to initialise a google storage connection to a bucket. That I already knew. However this method doesn't work when connecting to google drive. (where my google docs, sheets, slides, etc live). Everything GCP related is well documented, however I'm looking on how to initialise a connection to google drive. I looked for a "DriveOptions" class, but that one doesn't exist as far as i can tell. – Constantijn Visinescu Oct 15 '21 at 07:38
  • Also, i have the `GoogleCredential` method working. However that one is clearly marked as deprecated. Is using a deprecated API the official recommended way to do this? – Constantijn Visinescu Oct 15 '21 at 07:41
  • To be more specific, the Builder in the java api for drive needs a `com.google.api.client.http.HttpRequestInitializer httpRequestInitializer` to build, i can't figure out how to turn `GoogleCredentials` into one. – Constantijn Visinescu Oct 15 '21 at 07:47
  • I think I understand your issue now. The best in your situation is to use the [HttpCredentialsAdapter](https://github.com/googleapis/google-auth-library-java/blob/main/oauth2_http/java/com/google/auth/http/HttpCredentialsAdapter.java) – ziganotschka Oct 15 '21 at 08:15
  • That was my missing piece, thanks! – Constantijn Visinescu Oct 15 '21 at 16:33