这是我的初始化代码:
function HandleGoogleApiLibrary() {
// Load "client" & "auth2" libraries
gapi.load('client:auth2', {
callback: function () {
// Initialize client & auth libraries
gapi.client.init({
apiKey: 'My API Key',
clientId: 'My Client ID',
scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me'
}).then(
function (success) {
console.log("Yaaay");
},
function (error) {
console.log("Nope");
console.log(error);
}
);
},
onerror: function () {
// Failed to load libraries
}
});
}
这给了我以下错误信息:
details: "Not a valid origin for the client: http://127.0.0.1 has not been whitelisted for client ID 388774754090-o7o913o81ldb2jcfu939cfu36kh9h0q6.apps.googleusercontent.com. Please go to https://console.developers.google.com/ and whitelist this origin for your project's client ID."
error: "idpiframe_initialization_failed"
我的应用程序尚未投入生产,因此为了开发,我正在使用本地主机。事情是,在 every 堆栈溢出/引用上,我发现他们已经指定在 Authorised JavaScript origins
和 Authorised redirect URIs
上我必须使用http://localhost
而不是 http://127.0.0.1
。如果我这样做,我会收到该错误和 Permission denied to generate login hint for target domain.
如果我尝试登录。
如果我在两者上都使用 http://127.0.0.1
,我在初始化时没有得到 idpiframe_initialization_failed
,但是当我尝试登录时得到相同的权限被拒绝。
没有意义,没有其他方法可以指定本地主机,而且我无法使用 API。
最佳答案
在我的例子中,这是双向问题。
需要将 origin 添加到 google 控制台。
原点是什么样的? http://localhost
清除缓存。
这有时不起作用,您可能开始认为这不是缓存问题。详情如下。
<强>1。如何将你的来源添加到谷歌控制台?
- 转到此处:https://console.cloud.google.com/apis/credentials
- 在顶部选择您需要的项目。
- 找到
OAuth 2.0 客户端 ID
并在本节中选择(或创建)一个客户端 ID。 - 在打开的窗口中找到部分
Restrictions -> Authorized JavaScript origins
并添加http://localhost
或http://localhost:8000
(指定您需要的端口)。
<强>2。清除缓存。
如果第 1 步没有解决问题,您需要清除缓存。
如果您只想删除 localhost
缓存,在 Chrome
中,您可以通过以下方式进行:
- 打开设置 -> 更多工具 -> 开发者工具;
- 右键单击重新加载按钮 -> 清空缓存和硬重新加载。
有时缓存清除不起作用。为了确定它仍然是缓存问题,请使用隐身模式打开浏览器并再次检查您的站点。
关于javascript - 如何解决本地主机的 "idpiframe_initialization_failed"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52529360/