Howto test if an OAuth2 token is valid

5,190 views
Skip to first unread message

Davy De Waele

unread,
Aug 8, 2011, 1:34:40 AM8/8/11
to oauth...@googlegroups.com
Hi,

I have some logic in my application to check if a user has already authorized my application to access his data.
For that, I'm retrieving his Oauth2 token, and I'm doing an actual API call (using an API that was scoped for the OAuth token). 

If the API call works, I consider the token to be valid. 
If the API call fails, I consider the token to be invalid, and guide the user through the Oauth flow.

Is there another way to verify if the token is still valid without performing an actual API call ?

Regards,
Davy

Marius Scurtescu

unread,
Aug 8, 2011, 11:35:51 AM8/8/11
to oauth...@googlegroups.com
Hi again,

See comments below...

On Sun, Aug 7, 2011 at 10:34 PM, Davy De Waele <ddew...@gmail.com> wrote:
> Hi,
> I have some logic in my application to check if a user has already
> authorized my application to access his data.
> For that, I'm retrieving his Oauth2 token, and I'm doing an actual API call
> (using an API that was scoped for the OAuth token).

What flow are you using to retrieve the OAuth 2 token? Are you
retrieving both a refresh token and an access token?


> If the API call works, I consider the token to be valid.
> If the API call fails, I consider the token to be invalid, and guide the
> user through the Oauth flow.

Sort of. You have to check the HTTP status code you get back on the
failed API call. If it is 401 then most likely the token expired. For
any other code there is some other problem with the call.


> Is there another way to verify if the token is still valid without
> performing an actual API call ?

When the token is issued you also receive a hint with expires_in, you
can use that to track the approximate time when the token will expire.


Marius

Davy De Waele

unread,
Aug 8, 2011, 1:21:47 PM8/8/11
to oauth...@googlegroups.com
Hi,

I'm using a lot of Google APIs (Buzz, Fusion Tables, Latitude,...) all using Oauth 2.0.
Flows are working fine. 
Just thought if there was some built-in function (an isValid() or something) you could call on a token without actually performing a "dummy" API call.

I'm using the Google APIs client library for Java (http://code.google.com/p/google-api-java-client/) that does the refeshing for me automatically.

Marius Scurtescu

unread,
Aug 8, 2011, 2:53:26 PM8/8/11
to oauth...@googlegroups.com
Hi,

See comments below...

On Mon, Aug 8, 2011 at 10:21 AM, Davy De Waele <ddew...@gmail.com> wrote:
> Hi,
> I'm using a lot of Google APIs (Buzz, Fusion Tables, Latitude,...) all using
> Oauth 2.0.
> Flows are working fine.
> Just thought if there was some built-in function (an isValid() or something)
> you could call on a token without actually performing a "dummy" API call.

No, these tokens cannot be statically validated. You have to place an
API call to know. Also, keep in mind that end users can revoke them.

Marius

Davy De Waele

unread,
Aug 8, 2011, 3:24:37 PM8/8/11
to oauth...@googlegroups.com
ok great..... thx for the pointers !
Reply all
Reply to author
Forward
0 new messages