authorization - 来自服务器的 OAuth2 "Invalid Grant"响应

标签 authorization google-api in-app-purchase oauth-2.0

我要求我的客户使用他创建 google api 项目的授权 gmail 帐户访问此 URL。

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=http://www.XXXXXXXX.com/oauth2callback&client_id=XXXXXX.apps.googleusercontent.com&state=profile&approval_prompt=force

然后让他向我提供重定向 URL 中的代码参数
http://www.XXXXXXXX.com/oauth2callback?code=4/jUxc2MdX0xmF-b4_I6v2SLMQMuxO.cvQLVEpcJMUXOl05ti8ZT3ZvsT9ddwI

然后我自己发布了带有以下信息的表格。
<form action="https://accounts.google.com/o/oauth2/token" method="post" >

<input type="hidden" name="grant_type" value="authorization_code" >
<input type="text" name="code" value="**is the one i recieved from previous step**">
<input type="hidden" name="client_id" value="XXXXXXX.apps.googleusercontent.com" >
<input type="hidden" name="client_secret" value="XXXXXXXXXXXX" >
<input type="hidden" name="redirect_uri" value="http://www.XXXXXX.com/oauth2callback" >
<input type="submit" value="Submit">

</form>

然后我收到以下错误
{
    "error" : "invalid_grant"
}

当我自己生成代码 url 参数并执行下一步时。我成功收到以下回复
{
  "access_token" : "XXXXXXStBkRnGyZ2mUYOLgls7QVBxOg82XhBCFo8UIT5gM",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "XXXXXX3SEBX7F2cfrHcqJEa3KoAHYeXES6nmho"
}

但是,如果客户端生成 url 参数“代码”,那么我会看到无效授权错误。

我的客户在英国,而我在另一个国家。任何人都可以确认这是否是错误,因为客户正在另一个国家/地区生成代码参数,而我正在另一个国家/地区使用该代码?

提前致谢。

最佳答案

我对 invalid_grant 错误感到恼火,而不是相同的代码有时让我获得正确的访问 token 这一事实。

谢赫的回答引导我走向正确的方向。

首先,我们尝试从以下位置获取访问代码:

https://accounts.google.com/o/oauth2/auth

用户被定向到“允许权限”屏幕,然后我们的应用程序接收访问代码。

使用该访问代码,我们尝试从以下位置获取访问 token :

https://accounts.google.com/o/oauth2/token

在第一次尝试时,它会返回带有grant_type=authorization_code 的access_token,但是一旦提供了access_token,它就不再期望再次收到grant_type=authorization,而是喜欢收到grant_type=refresh_token

对于 android 开发人员,代码如下:

String accessToken = null, refreshToken = null;
HttpPost httppost = new HttpPost(https://accounts.google.com/o/oauth2/token);
HttpParams myParams = new BasicHttpParams();
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("client_id", BLOGGER_CLIENT_ID));
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
String bloggerAccessToken = prefs.getString(PREFERENCES_KEY_BLOGGER_ACCESS_TOKEN, null);

if(bloggerAccessToken != null && bloggerAccessToken.length() > 0){
    nameValuePairs.add(new BasicNameValuePair("refresh_token",  prefs.getString(PREFERENCES_KEY_BLOGGER_REFRESH_TOKEN, null)));
    nameValuePairs.add(new BasicNameValuePair("grant_type",    "refresh_token"));
} else{
    nameValuePairs.add(new BasicNameValuePair("code",  prefs.getString(PREFERENCES_KEY_BLOGGER_ACCESS_CODE, null)));
    nameValuePairs.add(new BasicNameValuePair("grant_type",    "authorization_code"));
    nameValuePairs.add(new BasicNameValuePair("redirect_uri",  "http://localhost"));
}

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpClient = new DefaultHttpClient(myParams);
response = httpClient.execute(httppost);

String returnedJsonStr = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(returnedJsonStr);
accessToken = jsonObject.getString("access_token");
if(jsonObject.has("refresh_token"))
    refreshToken = jsonObject.getString("refresh_token");

关于authorization - 来自服务器的 OAuth2 "Invalid Grant"响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941012/

相关文章:

google-api - 为客户端检索 Google API 授予的范围

google-api - 谷歌是否允许企业使用 "Did you Mean"功能作为 api?我想使用它,但我什么也没得到

javascript - 不安全的 JavaScript 试图访问 URL 为 :blank from frame with URL 的框架

ios in app 购买收据

authentication - 在多策略场景中设置默认授权策略

c# - 从 web.config 授权角色 MVC web

performance - Spring安全访问控制列表数十亿行

ios storekit 未收到消耗品或非消耗品

ios - Unibill,测试 iOS 应用内购买和 Unity

authorization - 如何在 Azure 资源管理器部署模板中为网站创建角色分配?