0

Currently, I am using the following implementation but the code says GoogleCredential is deprecated.

GoogleCredential credential = new GoogleCredential.Builder()
                    .setClientSecrets(clientId, clientSecret)
                    .setTransport(HTTP_TRANSPORT)
                    .setJsonFactory(JSON_FACTORY)
                    .build();
            credential.setRefreshToken(refreshToken);

I checked and GoogleCredentials or other google-auth-library classes are supposed to be used. However, all of them seem to require a service account. GoogleCredential is working for me without a service account. Just had to create oauth credentials. I have also generated the refresh tokens but not sure how to use them with the new library. What should I be using here? The goal is to just allow a single user(our backend code) to access google api.

I don't see any other questions for java where this was actually answered.

Edit - Posting my entire set up based on the comment updates-

public Credentials getCredentials() throws GeneralSecurityException, IOException {

        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

        try(InputStream in = getCredentialsAsInputStream()) {

            if (in == null) {
                throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
            }
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

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

            Credentials credential = UserCredentials.newBuilder()
                    .setClientId(clientId)
                    .setClientSecret(clientSecret)
                    .setRefreshToken(refreshToken)
                    .build();

            return credential;

And for setting up the drive

public Drive getDriveService() {
        try {
            Credentials credential = getCredentials();
            HttpRequestInitializer httpRequestInitializer = new HttpCredentialsAdapter(credential);
            final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
                    .setApplicationName(DRIVE_API_APPLICATION_NAME)
                    .setHttpRequestInitializer(httpRequest -> {

                        httpRequestInitializer.initialize(httpRequest);
                        httpRequest.setConnectTimeout(2 * 60000);  // 2 minutes connect timeout
                        httpRequest.setReadTimeout(2 * 60000);  // 2 minutes read timeout

                    })
                    .build();
        } catch (GeneralSecurityException | IOException e){
            log.error("Error creating drive service class : {}", e);
        }
        return null;
    }
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
fa gsc
  • 13
  • 3

1 Answers1

1

Try using the google-auth-library as there it defines the class GoogleCredentials:

Google Credentials = new GoogleCredentials.Builder()
            .setClientSecrets(clientId, clientSecret)
            .setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .build();
            credential.setRefreshToken(refreshToken);

You can as well check the Java documentation for GoogleCredentials

It seems that you are trying to use the Drive API so you may as well check this thread about connecting to the Drive service.

Gabriel Carballo
  • 1,278
  • 1
  • 3
  • 9
  • Apparently, builder has protected access so this method did not work. I also noticed a GoogleCredentials.newBuilder() however, that did not come with an option for ".setClientSecrets(clientId, clientSecret)". Appreciate the other thread as well. I will certainly try it out but as mentioned earlier, I am not using a service account. – fa gsc May 27 '22 at 04:15
  • Does it make sense to use UserCredentials class? I can see that it has an option for setting refresh tokens – fa gsc May 27 '22 at 04:23