0

I'm able to get Credentials using AuthorizationCodeFlow in Java running in Tomcat.

Now I'm trying to pass the access token I get on the server back to the client so that we are able to use gapi javascript client to do work in the browser without having to go through our server.

I set the access token in gapi using the following:

function setAccessToken() {
    gapi.auth.setToken({
        access_token: '<access_token from server>'
    });
}

I then call the gapi.client.load to get the drive client code:

gapi.client.load('drive', 'v2', onDriveClientLoaded);

I then call a function to get or create a folder in Google Drive (This has been tested using gapi.auth.authorize(), so I know the code as it is should work.).

function onDriveClientLoaded() {
    getOrCreateFolder();
}

But in getOrCreateFolder when I make a call to gapi.client.drive.children.list(...) the response is

{
    code: 401,
    message: "Invalid Credentials",
    ...
}

I've seen multiple places where people indicate that they have done this. But so far I cannot get this working.

In fiddler I am getting the following response:

HTTP/1.1 401 Unauthorized
Vary: Origin
Vary: X-Origin
WWW-Authenticate: Bearer realm="https://accounts.google.com/", error=invalid_token
Content-Type: application/json; charset=UTF-8
Date: Fri, 01 May 2015 20:16:22 GMT
Expires: Fri, 01 May 2015 20:16:22 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic,p=1
Content-Length: 249

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

Here is the slightly edited request:

GET https://content.googleapis.com/drive/v2/files/root/children?q=title%20%3D%20%27TestFolder%27 HTTP/1.1
Host: content.googleapis.com
Connection: keep-alive
Authorization: Bearer <access_token from server>
X-Goog-Encode-Response-If-Executable: base64
X-Origin: http://localhost:8080
X-ClientDetails: appVersion=5.0%20(Windows%20NT%206.1)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F42.0.2311.90%20Safari%2F537.36&platform=Win32&userAgent=Mozilla%2F5.0%20(Windows%20NT%206.1)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F42.0.2311.90%20Safari%2F537.36
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
X-JavaScript-User-Agent: google-api-javascript-client/1.1.0-beta
X-Referer: http://localhost:8080
Accept: */*
X-Chrome-UMA-Enabled: 1
X-Client-Data: CKu1yQEIhLbJAQijtskBCKm2yQEIxLbJAQiiicoBCImSygE=
Referer: https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.OuOrzZ8GQcU.O%2Fm%3D__features__%2Fam%3DIQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCO9uGNmv6wlfXBJwCbYRCdcqx94WQ
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
If-None-Match: "dM4Z0GasI3ekQlrgb3F8B4ytx24/sCBysa-CxrZGnYdsTvea7yHHhJ0"

1 Answer 1

1

Turns out everything is working as expected. I need to look at the refresh of tokens, as the access_token I was using was not valid, hence the "Invalid Credentials" message.

I tested the access_token by putting the following in postman:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=<access_token>

When using the incorrect access_token I was getting the invalid credentials response, but when I refreshed the token I got the following valid response:

{
 "issued_to": "<some_id>.apps.googleusercontent.com",
 "audience": "<some_id>.apps.googleusercontent.com",
 "scope": "https://www.googleapis.com/auth/drive",
 "expires_in": 3470,
 "access_type": "offline"
}
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.