question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GoogleCredentials Deprecated but is still used as an example for authentication

See original GitHub issue

Hi Admin,

GoogleCredentials has already been deprecated but is still being used as an example in the Authentication using OAuth2. Would like to use the more updated version. Hope there will be some update. Thanks! 😃

If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case, the access token will not be automatically refreshed):

Storage storage = StorageOptions.newBuilder()
    .setCredentials(new GoogleCredentials(new AccessToken(accessToken, expirationTime)))
    .build()
    .getService();

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

11reactions
AndroidDeveloperLBcommented, Sep 19, 2019

@adelbenhamadi Actually, replacing this:

    implementation 'com.google.api-client:google-api-client:1.30.2' exclude module: 'httpclient'
    implementation 'com.google.api-client:google-api-client-android:1.30.2' exclude module: 'httpclient'

With this:

implementation 'com.google.auth:google-auth-library-oauth2-http:0.17.1'
  • has made the deprecation warnings about using GoogleCredential to go away. How come? The only one that seem deprecated now is ContactsService.setOAuth2Credentials(GoogleCredential).

I don’t see it can easily be replaced. My current code is very similar to this one:

https://github.com/simformsolutions/Google-People-API-Sample/blob/master/app/src/main/java/com/simform/peoples/PeopleHelper.java

Meaning:

    public static Services setUp(final String serverAuthCode) throws IOException {
        final HttpTransport httpTransport = new NetHttpTransport();
        final JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        // Redirect URL for web based applications.
        // Can be empty too.
        final String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
        // Exchange auth code for access token
        final GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
                httpTransport,
                jsonFactory,
                context.getString(R.string.clientID),
                context.getString(R.string.clientSecret),
                serverAuthCode,
                redirectUrl).execute();
        // Then, create a GoogleCredential object using the tokens from GoogleTokenResponse
        final GoogleCredential credential = new GoogleCredential.Builder()
                .setClientSecrets(context.getString(R.string.clientID), context.getString(R.string.clientSecret))
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .build();
        credential.setFromTokenResponse(tokenResponse);
        final Services services = new Services();
        services.peopleService = new People.Builder(httpTransport, jsonFactory, credential)
                .setApplicationName(context.getPackageName())
                .build();
        final ContactsService contactsService = new ContactsService("contacts");
        contactsService.setOAuth2Credentials(credential);
        services.contactsService = contactsService;
        // credential can then be used to access Google services
        return services;
    }

How could I change it? Even replacing GoogleCredential with GoogleCredentials produces various issues…

0reactions
AndroidDeveloperLBcommented, May 12, 2021

@cyrilfr Is it now suitable even for the code snippet I wrote?

Can you please share the code you’ve used? How did you find “getTokenValue” for GoogleTokenResponse instance, for example? I don’t see it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the alternative to the deprecated 'GoogleCredential'?
After a while of looking around, I managed to fix it, using GoogleCredentials and HttpRequestInitializer . The code changes are as follows.
Read more >
GoogleCredential deprecated, new example please
But the Class GoogleCredential (from com.google.api.client.googleapis.auth.oauth2.GoogleCredential) is deprecated since a couple of months.
Read more >
Class GoogleCredential (2.1.1) | Java client library
Deprecated. Please use google-auth-library for handling Application Default Credentials and other non-OAuth2 based authentication.
Read more >
oauth2client deprecation — google-auth 1.30.0 documentation
Clear purpose and goals: google-auth is explicitly focused on Google-specific authentication, especially the server-to-server (service account) use case.
Read more >
GoogleCredential (google-api-client 1.33.2) - javadoc.io
Deprecated. Please use google-auth-library for handling Application Default Credentials and other non-OAuth2 based authentication.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found