google-auth-library-java icon indicating copy to clipboard operation
google-auth-library-java copied to clipboard

How do I use the accessToken given after a user gives the app permissions?

Open dmdum opened this issue 3 years ago • 1 comments

I'm taking over this feature that creates an event on a user's calendar. Our system was previously able to do this using com.google.api.client.googleapis.auth.oauth2.GoogleCredential, but since that's been deprecated, I'm looking for another way to replicate this feature.

This is how we previously did it (on groovy):

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.services.calendar.Calendar
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.services.calendar.model.Event

. . .

GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken)
Calendar service = new Calendar.Builder(
    GoogleNetHttpTransport.newTrustedTransport(),
    JacksonFactory.getDefaultInstance(),
    credential)
    .setApplicationName("applicationName")
    .build();
    
Event event = new Event()

. . .
service.events().insert("primary", event).execute()

With the com.google.api.client.googleapis.auth.oauth2.GoogleCredential deprecated, would it be correct to assume that the com.google.auth.oauth2.GoogleCredentials will be taking its place?

I tried using the new .GoogleCredentials (we're now in java) but I can't seem to re-use the same code flow in the previous implementation since the 3rd argument requires an httRequestInitializer

Is there another way to use the accessToken given after a user gives the app permissions? I'd like to lessen the changes in the code as much as possible.

dmdum avatar Nov 22 '22 11:11 dmdum

I would appreciate having an example to retrieve the User's email with the same earlier I was using following code

GoogleCredential credential = new GoogleCredential().setAccessToken(token);
 Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new GsonFactory(), credential)
                .setApplicationName("Oauth2")
                .build();
UserInfo  userinfo = oauth2.userinfo().get().execute();

whats the correct way after GoogleCredential deprecation

DineshSolanki avatar Jan 12 '23 13:01 DineshSolanki