【问题标题】:Google OAuth 2.0 returns invalid redirect_uri_mismatch when getting access_tokenGoogle OAuth 2.0 在获取 access_token 时返回无效的 redirect_uri_mismatch
【发布时间】:2013-08-09 05:40:26
【问题描述】:

我正在尝试将从客户端应用程序获得的 OAuth 一次性使用代码交换为服务器上的访问令牌和刷新令牌。我得到的回应是:

{
    "error" : "redirect_uri_mismatch"
}

我的 POST 请求是:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code={My Code}&
client_id={My Client ID}&
client_secret={My Client Secret}&
grant_type=authorization_code

我已根据 API 控制台中的内容检查了我的客户端 ID 和客户端密码,并且它们匹配。

我通过以下 Java 代码在我的客户端上获得一次性使用代码:

static final List<String> SCOPES = Arrays.asList(new String[]{"https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/userinfo.email"});
String scope = String.format("oauth2:server:client_id:%s:api_scope:%s", SERVER_CLIENT_ID, TextUtils.join(" ", SCOPES));
final String token = GoogleAuthUtil.getToken(c, email, scope); 

我的 API 控制台中有一个 redirect_uri,但由于我尝试使用跨客户端授权(如 here 所述),因此我故意将其按要求排除在 POST 请求之外:

当它为令牌交换代码时,它不应在 POST 中包含“redirect_uri”参数。

知道我做错了什么吗?

【问题讨论】:

    标签: access-token google-oauth


    【解决方案1】:

    事实证明,“不应在 POST 中包含 'redirect_uri' 参数”并不意味着完全省略 redirect_uri 字段。相反,这意味着 redirect_uri 字段应该有一个空值。

    我的新工作帖子是:

    POST /o/oauth2/token HTTP/1.1
    Host: accounts.google.com
    Content-Type: application/x-www-form-urlencoded
    
    code={My Code}&
    client_id={My Client ID}&
    client_secret={My Client Secret}&
    redirect_uri=''&
    grant_type=authorization_code
    

    【讨论】:

    • 你在服务器端使用什么库?我的猜测是库需要设置 redirect_uri,如果你省略它,那么它会发送一些默认值。当您显式设置一个空值时,然后将其发送出去,但在 OAuth 2 端点上,空值被视为完全没有参数。
    • 这是我遗漏的最后一块拼图。谢谢@quantumwannabe。
    【解决方案2】:

    在我的情况下 - https://stackoverflow.com/a/25184995/775359 - 答案很有帮助:

    var post_data = {
      code : code,
      client_id : google_client_id,
      client_secret : google_client_secret,
      redirect_uri : 'postmessage',
      grant_type : grant_type,
    };
    

    将 redirect_uri 设置为 postmessage 解决了我的 redirect_uri 不匹配问题。

    编辑:引用帖子的另一个答案:https://stackoverflow.com/a/18990268/775359

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-03
      • 2014-02-09
      • 2015-04-03
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2015-12-19
      相关资源
      最近更新 更多