Google OAuth 403 when refreshing access token

早过忘川 提交于 2019-12-13 18:20:42

问题


In a UWP app, I first get a refresh token and an access token using the following endpoint :

string tokenRequestBody = string.Format("code={0}&redirect_uri={1}&client_id={2}&scope=&grant_type=authorization_code",
                code,
                System.Uri.EscapeDataString(redirectURI),
                clientID
                );
StringContent content = new StringContent(tokenRequestBody, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = new HttpClient().PostAsync("https://www.googleapis.com/oauth2/v4/token", content).Result;

At this point in the response, i have a refresh token and a fully functional 1 hour access token. This is working fine.

Now i want to use the refresh token to renew the access token :

string tokenRequestBody = string.Format("client_id={0}&refresh_token={1}&grant_type=refresh_token", clientID, _refreshToken);
StringContent body = new StringContent(tokenRequestBody, Encoding.UTF8, "application/x-www-form-urlencoded");

HttpResponseMessage tokenResponse = new HttpClient().PostAsync("https://www.googleapis.com/oauth2/v4/token", body).Result;

Instead of getting a new access token, i have the following error :

[{"domain":"usageLimits","reason":"dailyLimitExceededUnreg","message":"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.","extendedHelp":"https://code.google.com/apis/console"}],"code":403,"message":"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."}

What am i missing here ?

Thanks for the help.


回答1:


You need to include the appropriate scopes for Google Drive. You are not including any as seen in your code below. This will prevent the issuing of access tokens.

string tokenRequestBody = string.Format(...&scope=...);

I would also recommend checking out the OAuth Playground. Very useful for developers building applications.




回答2:


Well, i'm amazingly stupid.

Nothing at all is wrong with the above code. I just parsed the wrong response.

Basically i did :

var dataResponse = getSomeStuffFromRestApi();
if (api authorization fails)
{
  var tokenResponse = getATokenFromRestApi();
  lookForTokenInResponse(dataResponse); // should've been tokenResponse.............
}

Sorry for the waste of time. At least there is some working code for reference now....



来源:https://stackoverflow.com/questions/48775759/google-oauth-403-when-refreshing-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!