2

I'm using the gapi to get googleUser.getAuthResponse().id_token in JavaScript via OAuth2 that I send to my server. I then want to use the Java API to access the Gmail API to list the messages on the account, but I'm getting the following error when I try to access it:

[error] com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
[error] {
[error]   "code" : 401,
[error]   "errors" : [ {
[error]     "domain" : "global",
[error]     "location" : "Authorization",
[error]     "locationType" : "header",
[error]     "message" : "Invalid Credentials",
[error]     "reason" : "authError"
[error]   } ],
[error]   "message" : "Invalid Credentials"
[error] }

Forgive the Scala code, but this is what I'm invoking:

val gmail = new Gmail.Builder(transport, jsonFactory, credential)
        .setApplicationName("myapp")
        .build()
val response = gmail.users().messages().list(userId).execute()

The userId I'm getting from the Java API's profile lookup (which works great), it's just trying to access the Gmail instance that is giving me trouble. I've searched but am having a hard time finding any information on this specific situation.

1 Answer 1

3

After much digging and bad Google documentation, I finally figured out that I was using the user.getAuthResponse().id_token when I should have been using the user.getAuthResponse().access_token. Annoying that it doesn't seem to be documented anywhere, but hopefully this will help someone else in the future.

Building the GoogleCredential was as simple as:

private val credential = new GoogleCredential.Builder().build().setAccessToken(accessToken)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.