Search code examples
c#google-apiuwpgoogle-oauth

Google OAuth 403 when refreshing access token


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.


Solution

  • 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....