OAuth 2.0 for Installed Applications - where do I

2019-06-06 04:33发布

问题:

I'm working through the instructions to use OAuth2 to get access to Gmail in an installed (Windows) application, on the page https://developers.google.com/identity/protocols/OAuth2InstalledApp. It all seems straightforward until I get to the part about "making a token request". One of the parameters to the POST I have to do is redirect_uri, which it says is "The redirect URI you obtained from the Developers Console."

I have obtained my ClientID and ClientSecret from the Developers Console, but can't see anywhere that I get a redirect_uri from it.

What am I missing? Thanks.

回答1:

Authentication is baslicy a three or four step process

Step one is just getting the authentication code. this link can be placed in any browser window its a HTTP GET

https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code

Once the user has accepted access you will be given an authentication code.

That code is sent back to the server to get a refresh token and the first access token. this is a HTTP POST.

https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code

The response will be something like this

{
"access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
}

The access token will only work for one hour and it will expire after that you will need to use the refresh token to get a new access token.

This is also a HTTP Post

https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token

response

{
"access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
"token_type" : "Bearer",
"expires_in" : 3600
}

code has been ripped from my tutorial Google 3 legged oauth2 it has more explanation than this.