2015-04-17 32 views
1

我正尝试创建一个Chrome扩展程序,用户可以使用其Google帐户登录。我已将其基于此示例代码:https://developers.google.com/+/quickstart/javascript,它得到的最多为gapi.auth2.init,但随后的Error: redirect_uri_mismatch发生了400个错误。客户端ID已被正确注册。使用G +登录的Chrome扩展程序 - redirect_uri_mismatch

由于redirect_uri错误,API调用失败,但是我为扩展设置了什么URI?该API调用时像这样:

gapi.auth2.init({ 
    fetch_basic_profile: false, 
    client_id: '1234_valid_id_1234', 
    cookie_policy: 'none', 
    redirect_uri: 'xxx', 
    scope:'https://www.googleapis.com/auth/plus.login' 
    }).then(

“XXX”作为URI仅仅是一个例子,我试过的众多选择在这里,无济于事。

该扩展将加载一个对话,就好像它将开始认证过程,但随后出现400错误。我不明白什么URI扩展应该提供作为回调在这里,当然这不适用?

客户端ID等是好的 - 如果我只是在一个标准的本地主机网站上运行示例JS代码它一切正常,它只是当试图在它打破的扩展中使用它,因为缺乏回调URI 。

+0

您必须放置'postmessage'而不是实际的URI。请参阅:http://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch和http://stackoverflow.com/questions/17954086/google-oauth2-authorizing-oauth -token-error-redirect-uri-mismatch/17976227#17976227 – gui47

+0

这没有什么区别。无论我改变redirect_uri,我总会看到redirect_uri设置为“storagerelay:// chrome-extension/appidinhere?id = auth963268”时返回的错误。我似乎无法覆盖这一点,我不知道什么sotragerelay或它来自哪里。 – artparks

+0

你有没有找到相同的解决方案? – pd176

回答

1

https://developers.google.com/identity/sign-in/web/reference gapi.auth2.init不采用redirect_uri参数。

只有使用redirect_uri的函数是grantOfflineAccess。 redirect_uri =“postmessage”应该始终有效。

var auth2 = gapi.auth2.init({ 
    fetch_basic_profile: false, 
    client_id: '1234_valid_id_1234', 
    cookie_policy: 'none', 
    scope:'https://www.googleapis.com/auth/plus.login' 
}); 

// on user click 
auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(
    function(resp) { 
    var auth_code = resp.code; 
    }); 
+0

我不明白的是,错误显示了重定向uri:redirect_uri = storagerelay:// chrome-extension/12345?id = auth82093即使我在auth2.init中调用的对象中指定postmessage - 无论我改变它的价值仍然显示这个redirect_uri在失败?! – artparks

+0

这是Google方面的一个错误。我已经报道过,我会告诉你什么时候它会被修复。抱歉,添麻烦了。 –

+0

谢谢。请让我知道你在哪里举办票。 – artparks