Token refresh after: 'Getting 401 Token invalid - Invalid token: Stateless token expired' fails

1,578 views
Skip to first unread message

James Jones

unread,
Apr 21, 2012, 2:02:37 PM4/21/12
to oauth...@googlegroups.com
Trying to refresh a token to access Google Contacts where user has approved access_type=offline.


This works and I get an access token that works for a while.  At some point that token expires and I need to refresh.  It appears that my refresh token is good (ya29.AHES6ZRhvyWAx4z_8_uJnBuI6tEELZqBDdh54Ti6ydvb5f0), but the access.getAccessToken() returns null after a call to access.refresh().   Here is the code:

HttpTransport TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(parent.accessTokenGC,
TRANSPORT, JSON_FACTORY, GCGlobals.clientID, GCGlobals.clientSecret, parent.refreshTokenGC);
HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);

GenericUrl contactURL = new GenericUrl(GCGlobals.baseScope + "contacts/default/full?max-results=5000&alt=json");
HttpRequest req = rf.buildGetRequest(contactURL);

GoogleHeaders header = new GoogleHeaders();
header.setContentType("application/json");
header.set("GData-Version", "3");
req.setHeaders(header);
try {
res = req.execute();
} catch (com.google.api.client.http.HttpResponseException ee) {
// try to refresh the token
try {
System.out.println(ee.getMessage());   // prints  401 Token invalid - Invalid token: Stateless token expired
access.refreshToken();
parent.accessTokenGC = access.getAccessToken();   // returns null thus I cannot proceed

Any help greatly appreciated!

Sreenath K

unread,
May 5, 2012, 9:03:15 AM5/5/12
to oauth...@googlegroups.com
Hi,

I am facing the same problem... Did you got the solution. If you got any solution for this please help me out.

Thanks

Yaniv Inbar (יניב ענבר)

unread,
May 9, 2012, 7:28:07 AM5/9/12
to oauth...@googlegroups.com
Would you kindly upgrade to 1.8.0-beta and try again?  It won't fix the problem, but it may make it slightly easier to debug.

Another thing you can try is to enable logging and look if there is anything suspicious going on:

Since it says "Stateless token expired", I suspect it is a problem with the refresh token.

Yaniv Inbar
Senior Software Engineer
Google Inc.

Caroline McLean

unread,
May 17, 2012, 1:43:36 PM5/17/12
to oauth...@googlegroups.com
The refresh token is only issued once on the first request.  If you requested the token once and already issued approval the second request for a code for token exchange will not return the refresh token.  You can set the approval_prompt=force to force re-approval and hence reissue of the refresh token or you can revoke the token from the following UI, https://accounts.google.com/IssuedAuthSubTokens and then reissue the request.
Reply all
Reply to author
Forward
0 new messages