Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleCredentials Deprecated but is still used as an example for authentication #3314

Closed
Kool-Koder opened this issue May 27, 2018 · 8 comments · Fixed by #3322
Closed

GoogleCredentials Deprecated but is still used as an example for authentication #3314

Kool-Koder opened this issue May 27, 2018 · 8 comments · Fixed by #3322
Assignees

Comments

@Kool-Koder
Copy link

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();
@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented Jun 27, 2019

Why is it closed? What are we supposed to use instead?
I've also noticed that com.google.api.services.people.v1.People.Builder was removed, and I can't find what should replace it.

@lastomato
Copy link

See googleapis/google-api-java-client@45825c4, check out the link after deprecated annotation: https://github.com/googleapis/google-auth-library-java

@adelbenhamadi
Copy link

adelbenhamadi commented Sep 18, 2019

add to your pom

<dependency>
		  <groupId>com.google.auth</groupId>
		  <artifactId>google-auth-library-oauth2-http</artifactId>
		  <version>0.17.1</version>
		</dependency>

and change GoogleCredential to GoogleCredentials (end with s)

@AndroidDeveloperLB
Copy link

@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...

@cyrilfr
Copy link

cyrilfr commented May 2, 2020

When GoogleCredential works with the Firebase HTTP API, GoogleCredentials doesn't. It seems that they aren't exactly equivalent...

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented May 6, 2021

@cyrilfr I don't understand. Is it a reply to what I wrote? Did you solve the issue that I can't find how to use it?

@cyrilfr
Copy link

cyrilfr commented May 6, 2021

I don't remember, this comment is almost exactly 1 year old...

I use GoogleCreditentials (with a s) in order to perform requests to the Frebase REST API. This is the replacement for the old OAuth library. For what I remember, the Java interface is slightly different but not that much. The main difference in my case was the getAccessToken() method that previously returns a String when it's an object now. So now we have to do getAccessToken().getTokenValue() in order to get a good Bearer token.

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented 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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants