【问题标题】:Google OAuth 403 when refreshing access token刷新访问令牌时的 Google OAuth 403
【发布时间】:2018-07-24 08:26:19
【问题描述】:

在 UWP 应用中,我首先使用以下端点获取刷新令牌和访问令牌:

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;

在响应的这一点上,我有一个刷新令牌和一个功能齐全的 1 小时访问令牌。这工作正常。

现在我想使用刷新令牌来更新访问令牌:

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;

我没有获得新的访问令牌,而是出现以下错误:

[{"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."}

我在这里错过了什么?

感谢您的帮助。

【问题讨论】:

  • 您要使用什么 Google API 服务?
  • Google 云端硬盘 API。

标签: c# google-api uwp google-oauth


【解决方案1】:

好吧,我真是太愚蠢了。

上面的代码没有任何问题。我只是解析了错误的响应。

基本上我做到了:

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

抱歉浪费了时间。至少现在有一些工作代码可供参考....

【讨论】:

    【解决方案2】:

    您需要为 Google 云端硬盘添加适当的 scopes。您不包括以下代码中的任何内容。这将阻止颁发访问令牌。

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

    我还建议您查看OAuth Playground。对于构建应用程序的开发人员非常有用。

    【讨论】:

    • 首次重定向到 Google 登录页面时需要使用范围。然后交换令牌不需要它们,如文档 (developers.google.com/identity/protocols/…) 中所述。并且访问令牌确实有效,我只是无法使用访问令牌更新它。我会看看操场,它可能有助于诊断我的问题。
    • 它们不是必需的,因为它们是当时令牌的一部分。您使用刷新令牌而不是访问令牌来更新访问令牌。
    • 是的,抱歉打错了,我的意思是刷新令牌,就像在 OP 中一样。与操场混淆,我似乎有两个不同之处:我没有“secret_client”并且无处可寻(但它似乎是可选的stackoverflow.com/questions/11295661/…),当您通过操场更新访问令牌时,查询使用当前访问令牌授权。这似乎很愚蠢,因为此时我的令牌可能已过期。所以我不知道该怎么办。
    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 2019-03-16
    • 2015-11-26
    • 2014-07-15
    • 2021-11-21
    • 2015-04-01
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多