user1546209
user1546209

Reputation: 13

Not able to access access_token value using QueryString (Google OAuth 2.0)

I'm not able to fetch the access_token value from a QueryString in ASP.Net (C#).

Google is sending the authorization token, but I am not able to capture the token value by using

  1. QueryString (getting null)
  2. splitting string (not getting full url)

The RedirectUrl is: http://www.onfrnz.com/auth.aspx

http://www.onfrnz.com/auth.aspx#state=/profile
    &access_token={ACCESS_TOKEN}
    &token_type=Bearer
    &expires_in=3600 

Please help me on this. I don't want to use a library. Thanks in advance.

Upvotes: 0

Views: 1033

Answers (1)

Ryan Boyd
Ryan Boyd

Reputation: 3018

You're trying to access a hash fragment value in server-side code. The hash fragment value (after the # in the URL), does not get sent from the web browser to the web server, and thus will not be accessible in server-side code.

When you initially kick off the OAuth flow, you're likely using:

&response_type=token

This indicates you want to use the flow for client-side (JavaScript) applications (called the "implicit" flow in the spec).

Instead, you should be using:

&response_type=code

This will return an authorization code value in the query parameter, which you should be able to retrieve using your .NET code, and then exchange for an access token by making a server-to-server request to Google.

More information on Google's OAuth 2.0 flow for web server applications (authorization code flow) is available here:

https://developers.google.com/accounts/docs/OAuth2WebServer

Upvotes: 3

Related Questions