【问题标题】:Why is Google App Engine OAuth2 returning Invalid Credentials using Javascript为什么 Google App Engine OAuth2 使用 Javascript 返回无效凭据
【发布时间】:2014-06-28 14:56:09
【问题描述】:

我正在构建一个 Google App Engine 应用并运行以下代码

<!DOCTYPE html>
<html manifest="cache.appcache">
<head>
    <title>Eat Fresh</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <script src="scripts/whitelist.js" type="text/javascript"></script>
    <script src="https://apis.google.com/js/client.js?onload=init"></script>
</head>
<body>
</body>

</html>

 

function init() {
    CLIENT_ID = "my client ID"
    SCOPES = ["https://www.googleapis.com/auth/userinfo.email"]
    gapi.client.load('oauth2', 'v2', signin)
}

function signin() {
    gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: true}, userAuthed)
}

function userAuthed() {
    gapi.client.oauth2.userinfo.get().execute(function(resp) {
    checkEmail(resp)
    })
}

function checkEmail(user) {
    console.log(user)
    var validEmail = (whiteList.indexOf(user.email) !== -1)
    if (!user.code && validEmail) {
        startApp()
    } else {
        displayError(user.email)
    }
}

这是返回

Object {code: 401, message: "Invalid Credentials", data: Array[1], error: Object}

我已经用谷歌搜索和搜索,但找不到可能发生这种情况的原因。上面使用的代码直接取自我的另一个 Google App Engine 项目,它在那里完美运行。另外,如果我记得的话,我在项目中收到了类似的消息,我从中获取了此代码,并且通过更改 https://appengine.google.com/https://console.developers.google.com/project 上的设置来解决此问题。

我看了又看,找不到第一次帮助我修复它的网页。我对设置的猜测可能完全不正确,但这是我的预感。

【问题讨论】:

    标签: javascript html google-app-engine web-applications oauth-2.0


    【解决方案1】:

    发生这种情况的原因是因为访问令牌来自

    gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: true}, userAuthed)
    

    返回未定义。这是由于该应用无权授权该应用查看 google+ 帐户数据。更改以下内容

    immediate: false
    

    将提示用户授予应用程序权限,从而允许授权访问令牌和其余代码工作。

    【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2017-06-12
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多