获取“Realm与redirect_uri / origin不匹配。错误代码:2”

时间:2016-05-14 01:19:23

标签: openid google-api-java-client openid-connect google-openid

当我尝试登录时,登录窗口会按预期弹出,但它会给我这个错误。

Error: invalid_request
Realm didn't match redirect_uri/origin. Error code: 2

Request Details:

openid.realm=localhost:8080/oauth2callback
scope=email profile openid https://www.googleapis.com/auth/userinfo.email
response_type=permission
redirect_uri=storagerelay://http/localhost:8080?id=auth435566
ss_domain=http://localhost:8080
client_id=SOME_STRING_ID.apps.googleusercontent.com
fetch_basic_profile=true

我通读了Open ID 2.0 documentation,根据我对它的理解,属性openid.realm应该与我的重定向uri相同。

以下是我在凭据部分的开发者控制台中的“授权重定向URI”:

https://myapp-1234.appspot.com/oauth2callback
http://myapp-1234.appspot.com/oauth2callback
http://localhost:8080/oauth2callback

我将openid.realm设置为localhost:8080/oauth2callback,因为目前我只测试我的应用程序,但我在部署中尝试过其他的但我仍然得到相同的结果,但错误代码不同。< / p>

这是我调用signIn()方法的地方:

var authConfig = {
    client_id: 'SOME_STRING_ID.apps.googleusercontent.com',
    cookie_policy: 'single_host_origin',
    scope: 'https://www.googleapis.com/auth/userinfo.email profile',
    openid_realm: 'localhost:8080/oauth2callback'
}

window.handleGoogleClientLoad = function () {
gapi.client.load('myapp_endpoints', 'v1', null, '//' + window.location.host + '/_ah/api');
console.log('API LOADED');
}

//=====================================================================================

function authorizeUser() {
    gapi.load('auth2', function() {
        gapi.auth2.init(authConfig)
            .then(function() {
                var auth2 = gapi.auth2.getAuthInstance();
                var user = auth2.signIn();
                console.log(user);
        });
    });
}

module.exports = {
    login: authorizeUser
}

这是index.html

<!DOCTYPE html>
<head>
<meta charset="utf-8">
    <title>My App</title>
</head>

<body>
    <div id="root"></div>
    <script src="lib/vendors.js"></script>
    <script src="build/bundle.js"></script>
</body> 

2 个答案:

答案 0 :(得分:1)

问题是我使用了错误的Google API库。我使用的是从npm获得的库。然后我导入了模块。

import GoogleApis from 'googleapis'

我打算做的是使用可以从他们自己的website下载的库。 Integrating这很方便。您所要做的就是将以下代码行添加到index.html

<script src="https://apis.google.com/js/platform.js" async defer></script>

该文件应该如下所示

<!DOCTYPE html>
<head>
<meta charset="utf-8">
    <title>Welcome to MyApp</title>
</head>

<body>
    <div id="root"></div>
    <script src="lib/vendors.js"></script>
    <script src="build/bundle.js"></script>
    <script src="https://apis.google.com/js/platform.js?onload=init" async defer></script> 
    <!-- init is the function that will get invoked when the page loads. -->
</body> 

然后我改变了我的javascript。您可以从code examples找到这些更改的参考。

const authConfig = {
    client_id: 'my_app.apps.googleusercontent.com',
    cookie_policy: 'single_host_origin',
    scope: 'https://www.googleapis.com/auth/userinfo.email profile',
    fetch_basic_profile: true
}

window.init = function() {
    gapi.load('auth2', function() {
        var auth2 = gapi.auth2.init(authConfig);
        auth2.signIn().then(function() {
            if(auth2.isSignedIn.get()) {
                var profile = auth2.currentUser.get().getBasicProfile();

                console.log('ID: ' + profile.getId());
                console.log('Full Name: ' + profile.getName());
                console.log('Email: ' + profile.getEmail());
            }
        });
    });
}

答案 1 :(得分:0)

openid.realm=response_type=permission。

id_token

redirect_uri=storagerelay://https/www.zillow.com?id=auth807591 ss_domain=https://www.zillow.com

client_id=238648973530.apps.googleusercontent.com

fetch_basic_profile=true

gsiwebsdk=

access_type=online

scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid