GoogleCredentials Deprecated but is still used as an example for authentication
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();
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.
See https://github.com/googleapis/google-api-java-client/commit/45825c4157eee2a7254817b1aae9aa63ba726726, check out the link after deprecated annotation: https://github.com/googleapis/google-auth-library-java
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)
@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
GoogleCredentialto go away. How come? The only one that seem deprecated now isContactsService.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...
When GoogleCredential works with the Firebase HTTP API, GoogleCredentials doesn't. It seems that they aren't exactly equivalent...
@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?
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.
@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...