Google OAuth 2授权:使用ajax p

时间:2017-05-03 06:37:32

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

我已经在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>

1 个答案:

答案 0 :(得分:0)

您的应用中的重定向网址以及您在API控制台中配置的重定向网址必须是完全符合字符的匹配。请记住,您可以在APi控制台中配置多个重定向网址,因此请不要害羞并添加所有可能的变体,例如同时使用http和https。您应该真正使用https作为重定向网址,如果将来的更改禁止使用明文网址,我不会感到惊讶。