【问题标题】:Google OAuth 2 authorization: got Error redirect_uri_mismatch using ajax pGoogle OAuth 2 授权:使用 ajax p 得到错误 redirect_uri_mismatch
【发布时间】:2017-05-03 19:05:00
【问题描述】:

我已经在 Google API Concole 上设置了授权重定向 URI,如下所示。 Google console API Setting

我使用 localhost:64420/index 获取代码,并将代码发送到 localhost:64420/Auth 以使用 ajax post 参数尝试获取访问令牌. 可悲的是,我收到了错误消息:

{error: "redirect_uri_mismatch", error_description: "Bad Request"}

这是脚本:

        <script>
        var code = code;
        var clientID = client_ID;
        var clientSecret = client_Secret;
        var redirect_uri = "http://localhost:64420/Report.aspx";
        var searchurl = "https://www.googleapis.com/oauth2/v4/token";
        $.ajax({
            dataType: "json",
            url: searchurl,
            data: { code: code, client_id: clientID, client_secret: clientSecret, redirect_uri: redirect_uri, grant_type: 'authorization_code' },
            type: "POST",
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            crossDomain: true,
            cache: true,
            success: function (data) {
                alert(data);
            },
            error: function (jqXHR, exception, errorstr) {
                console.log(jqXHR);
                alert(errorstr);
            }
        });
    </script>

【问题讨论】:

标签: google-api google-oauth google-search-console


【解决方案1】:

您的应用程序中的重定向 URL 和您在 API 控制台中配置的重定向 URL 必须是完全匹配的字符。请记住,您可以在 APi 控制台中配置多个重定向 URL,因此不要害羞并添加所有可能的变体,例如同时使用 http 和 https。你真的应该使用 https 作为重定向 URL,如果未来的更改不允许纯文本 URL,我不会感到惊讶。

【讨论】:

  • 感谢您的回复。我确信这两个重定向 URI 都是完全匹配的字符,我尝试使用 https,但仍然出现相同的错误。
猜你喜欢
  • 2012-07-14
  • 1970-01-01
  • 2021-10-14
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多