为什么我不能让Amazon Cognito接受来自Google+的令牌?

时间:2015-05-12 17:37:11

标签: amazon-cognito

我是通过javascript在网页中完成的。

首先,我将用户发送给Google,以便他们可以授予访问权限:

    https://accounts.google.com/o/oauth2/auth?response_type=token&scope=profile&redirect_uri=https://example.com/&client_id=1414xxxxxxxx-{myclientid}.apps.googleusercontent.com

授予访问权限后,会将其重定向回我的网站,如下所示:

    https://example.com/#access_token={their_token}&token_type=Bearer&expires_in=3600

然后我将这些信息提供给亚马逊,如下所示:

    // Initialize the Amazon Cognito credentials provider
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: 'us-east-1:{my-pool-id}',
            Logins: {
                'accounts.google.com': "{their_token}"
            }
    });
    AWS.config.region = 'us-east-1';

    AWS.config.credentials.get(function(err) {
        if ( err ) {
            console.log(err);
        }
        else {
            console.log("ID: "+identityId:AWS.config.credentials.identityId);
        }
    }); 

我总是在控制台中出现错误:

"Error: Invalid login token."

标识池'us-east-1:{my-pool-id}'在我的亚马逊控制台中设置,其中包含身份验证提供商的“Google+”设置以及与Google网址中匹配的Google客户端ID(即。 1414xxxxxxxx- {myclientid} .apps.googleusercontent.com)。

我还尝试将参数中的AccountIdRoleArn设置为 CognitoIdentityCredentials ,但在这种情况下它没有帮助。 我正确使用Logins参数吗?我错过了什么?

如果我取出Logins参数或将其设置为空对象,我会得到一个identityId,我认为它与未经身份验证的角色相关联。 如何使用Google API作为令牌提供商获取经过身份验证的角色的身份?

1 个答案:

答案 0 :(得分:1)

您需要使用id_token而不是access_token。有关获取id_token的更多详细信息,请参阅此文档:OpenIDConnect