Wednesday, 19 May 2021

How to migrate from GoogleCredential to GoogleCredentials and still get access to People API?

Background

I've noticed that the class com.google.api.client.googleapis.auth.oauth2.GoogleCredential has become deprecated:

https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.html

The problem

The app has code that is based on some old G+ code (here) to reach contacts via the Google account. Here's a snippet of the most important part of it, which causes me trouble of migrating away from it:

    @WorkerThread
    public static Services setUp(@Nullable 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, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, serverAuthCode, redirectUrl)
                .execute();
        // Then, create a GoogleCredential object using the tokens from GoogleTokenResponse
        final GoogleCredential credential = new GoogleCredential.Builder()
                .setClientSecrets(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .build();
        final String accessToken = tokenResponse.getAccessToken();
        credential.setFromTokenResponse(tokenResponse);
        final PeopleService peopleService = new PeopleService.Builder(httpTransport, jsonFactory, credential)
                .setApplicationName(getCurrentappPackageName())
                .build();
        final Services result = new Services();
        result.peopleService = peopleService.people();
        result.otherContactsService = peopleService.otherContacts();
        result.contactGroups = peopleService.contactGroups();
        final ContactsService contactsService = new ContactsService("contacts");
        contactsService.setOAuth2Credentials(credential);
        result.contactsService = contactsService;
        return result;
    }

    public static class Services {
        @Nullable
        public PeopleService.People peopleService;
        @Deprecated
        @Nullable
        public ContactsService contactsService;
        @Nullable
        public PeopleService.OtherContacts otherContactsService;
        @Nullable
        public PeopleService.ContactGroups contactGroups;
    }

The problem is even from the very beginning:

The class GoogleCredentials doesn't seem to accept anything of what I got above it for GoogleCredential class.

What I've tried

Other than looking at the docs (and failing to see resemblance to what I have to handle), I tried to write about this here.

Sadly I can't find how to migrate away from the old code yet.

I tried to ask there how can I migrate (here and here) but didn't get an answer yet.

The questions

How can I migrate away from GoogleCredential to GoogleCredentials, while also still being able to use the various APIs such as People API?



from How to migrate from GoogleCredential to GoogleCredentials and still get access to People API?

No comments:

Post a Comment