简体   繁体   English

如何解决本地主机的“idpiframe_initialization_failed”?

[英]How to solve "idpiframe_initialization_failed" for localhost?

Here's my initialization code:这是我的初始化代码:

            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
                    }
                });
            }

Which gives me the following error message:这给了我以下错误消息:

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"

My app is not in production yet, so for development I'm using localhost.我的应用程序尚未投入生产,因此我使用 localhost 进行开发。 Thing is, on every stack overflow / reference I've found they've specified that on Authorised JavaScript origins and Authorised redirect URIs I must use http://localhost instead of http://127.0.0.1 .问题是,在每个堆栈溢出/引用上,我发现他们已经指定在Authorised JavaScript originsAuthorised redirect URIs上,我必须使用http://localhost而不是http://127.0.0.1 If I do this, I get that error and a Permission denied to generate login hint for target domain.如果我这样做,我会收到该错误和Permission denied to generate login hint for target domain. if I try to login.如果我尝试登录。

If I use http://127.0.0.1 on both, I get no idpiframe_initialization_failed on initialization, but get the same permission denied when I try to login.如果我在两者上都使用http://127.0.0.1 ,我在初始化时没有得到idpiframe_initialization_failed ,但是当我尝试登录时得到相同的权限被拒绝。

It makes no sense, there's no other way to specify the localhost, and I just can't use the API.没有意义,没有其他方法可以指定localhost,我就是不能使用API。

In my case it was two-side problem.就我而言,这是两个方面的问题。

  1. Need to add origin to google console.需要将原点添加到谷歌控制台。

    How does origin look like?起源如何? http://localhost http://本地主机

  2. Clear cache.清除缓存。

    This sometimes does not work and you may start thinking that this is not a cache problem.这有时不起作用,您可能会开始认为这不是缓存问题。 Details below.详情如下。


1. How to add your origin to google console? 1.如何将您的来源添加到谷歌控制台?

  • Go here: https://console.cloud.google.com/apis/credentials去这里: https : //console.cloud.google.com/apis/credentials
  • Select a Project you need at the top.在顶部选择您需要的项目。
  • Find OAuth 2.0 client IDs and in this section select (or create) a client ID.查找OAuth 2.0 client IDs并在此部分中选择(或创建)一个客户端 ID。
  • In the opened window find the section Restrictions -> Authorized JavaScript origins and add either http://localhost or http://localhost:8000 (specifying the port you need).在打开的窗口中找到Restrictions -> Authorized JavaScript origins并添加http://localhosthttp://localhost:8000 (指定您需要的端口)。

2. Clear cache. 2.清除缓存。

If the step #1 did not solve the problem, you need to clear cache.如果步骤#1 没有解决问题,您需要清除缓存。

If you want to remove just localhost cache, in Chrome you can do it in the next way:如果您只想删除localhost缓存,在Chrome您可以通过以下方式进行:

  • Open Settings -> More tools -> Developer tools;打开设置->更多工具->开发者工具;
  • Right click on Reload button -> Empty Cache and Hard Reload.右键单击重新加载按钮 -> 清空缓存和硬重新加载。

Sometimes cache clearing does not work.有时缓存清除不起作用。 Just to be sure, that it is still a cache problem, open a browser using Incognito mode and check your site again.为了确定它仍然是缓存问题,请使用隐身模式打开浏览器并再次检查您的站点。

Just to save future readers, I've managed to authenticate from the localhost by typing exactly http://www.localhost on "Authorized Javascript Origins" in the Client ID creation (Credentials / Developer Console).为了节省未来的读者,我设法通过在客户端 ID 创建(凭据/开发人员控制台)中的“Authorized Javascript Origins”上准确键入http://www.localhost来从本地主机进行身份验证。 Totally not obvious and anti-user, but neither http://localhost or http://127.0.0.1 worked.完全不明显和反用户,但http://localhosthttp://127.0.0.1都不起作用。

对我来说,问题是通过禁用 AdBlocker 解决的

I had this issue on Version 100.0.4896.127 (Official Build) snap (64-bit) on Ubuntu 20.04 LTS.我在 Ubuntu 20.04 LTS 上的版本 100.0.4896.127(官方构建)快照(64 位)上遇到了这个问题。 Allowing third-party cookies under Settings -> Privacy and Security -> Cookies and other site data solved it.在设置 -> 隐私和安全 -> Cookies 和其他站点数据下允许第三方 cookies 解决了它。 The moment I disabled the third party cookies, the issue was back.在我禁用第三方 cookies 的那一刻,问题又回来了。 So, for my case at least, it seems the issue is third party cookies not enabled.因此,至少就我而言,问题似乎是第三方 cookies 未启用。 Note that I had the URL of the application as http://localhost:some-port请注意,我将应用程序的 URL 作为 http://localhost:some-port

This is the problem i faced, after trying many things this is what worked for me:这是我面临的问题,在尝试了很多事情之后,这对我有用:
Step 1: use gapi-script in the react project for that第 1 步:为此在 react 项目中使用 gapi-script
npm i gapi-script npm i gapi-script
Step 2: import it in the project第二步:在项目中导入
import { gapi } from "gapi-script";从“gapi-script”导入 { gapi };
Step 3: write a useEffect function第三步:写一个useEffect function
useEffect(() => { const clientId = "enter your client id here"; function start() { gapi.client.init({ clientId: clientId, scope: "", }); } gapi.load("client:auth2", start); }); useEffect(() => { const clientId = "在此处输入您的客户 ID"; function start() { gapi.client.init({ clientId: clientId, scope: "", }); } gapi.load("client: auth2", 开始); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM