Monday, 5 September 2022

Obtaining GoogleCredentials from One Tap Signin

I'm trying to upload a file to Google Drive and let the user choose which Google account is used for the upload.

I'm following this guide: https://developers.google.com/drive/api/guides/manage-uploads

As indicated, it demands some OAuth credentials to connect to the Drive API:

// Load pre-authorized user credentials from the environment.
// TODO(developer) - See https://developers.google.com/identity for
// guides on implementing OAuth2 for your application.

I've managed to get a SignInCredential object from the One Tap client, but now I'm trying to convert it to a GoogleCredentials object for Google Drive API.

Using the code inside the guide

GoogleCredentials.getApplicationDefault()
        .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE))

...ends up in a crash:

The Application Default Credentials are not available.

And if I try to create the credentials on my own

    val accessToken = AccessToken(token, Date(0L))
    val credentials: GoogleCredentials = GoogleCredentials.create(accessToken)

I also ends up in a crash:

java.lang.IllegalStateException: OAuth2Credentials instance does not support refreshing the access token. An instance with a new access token should be used, or a derived type that supports refreshing.

What are we supposed to do here?



from Obtaining GoogleCredentials from One Tap Signin

No comments:

Post a Comment