【问题标题】:Google Authentication JS API is redirecting to the wrong URLGoogle Authentication JS API 重定向到错误的 URL
【发布时间】:2018-10-30 13:05:30
【问题描述】:

我已经使用下面的代码几年了,直到一个月前它都可以正常工作。

MY_CUSTOM_URL 在 Google 授权重定向 URI 上声明和注册。

        function initClient() {   
                    gapi.client.init({
                        apiKey: APKEY,
                        clientId: CLIENTID,
                        discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"],
                        scope: "https://www.googleapis.com/auth/calendar",
                        redirect_uri: MY_CUSTOM_URL,
                        ux_mode: 'redirect',                
                    }).then(function () {                    
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);                        
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());

                    });
                }

当我尝试调用 gapi.auth2.getAuthInstance().signIn(); 时,它会忽略 MY_CUSTOM_URL 并重定向到默认网站根域,即使我使用的是 localhost(也在 Google 上注册),并且未进行身份验证。

如果我评论 ux_mode,它会打开一个 popup 并正常工作,但我不能为此目的使用弹出窗口。

谢谢

【问题讨论】:

    标签: javascript google-api google-calendar-api google-oauth google-api-client


    【解决方案1】:

    来自Doc

    必填。确定 API 服务器在 用户完成授权流程。 值必须完全匹配 在 API 中为您的项目列出的 redirect_uri 值之一 控制台。请注意,http 或 https 方案、大小写和尾部斜杠 ('/') 必须全部匹配。

    检查API Console 中的redirect_uri,使其匹配您的MY_CUSTOM_URL

    你可以在这里找到它

    选择 api => credintals =>选择 OAuth 2.0 客户端 ID => 授权重定向 URI [输入字段]

    【讨论】:

    • 谢谢@essam,例如,但我已经三遍检查了网址。
    • 请检查控制台日志,告诉我们是否有任何错误或警告
    • 没有错误,它只是重定向到默认的网站根域(即使我是本地主机,它也会将我重定向到生产域)
    • 感谢您的支持,我刚刚发现了问题,这真的很尴尬……我在页面顶部有一个自定义的 javascript 重定向,恰好与 google 回调 url 匹配。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2017-01-15
    • 2021-07-19
    • 2020-07-07
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多