javascript 尝试检索经过身份验证的 Google+ 用户的电子邮件地址时出现“gapi.client 未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19476332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:43:04  来源:igfitidea点击:

Getting "gapi.client is undefined" when trying to retrieve an authenticated Google+ user's email address

javascriptapigoogle-apigoogle-plus

提问by dannepanne

I'm trying to retrieve user data from a visitor who has signed in to my site using google + sign in button, but I just end up with the message "TypeError: gapi.client is undefined" in the javascript console.

我正在尝试从使用 google + 登录按钮登录到我的网站的访问者那里检索用户数据,但我最终在 javascript 控制台中收到消息“TypeError: gapi.client is undefined”。

Since I could not identify the error I finally copy/pasted the example code from Google, which can be found here: https://developers.google.com/+/web/people/#retrieve_an_authenticated_users_email_address

由于我无法识别错误,我最终从 Google 复制/粘贴了示例代码,可在此处找到:https: //developers.google.com/+/web/people/#retrieve_an_authenticated_users_email_address

The only thing I changed was the Client ID (for web applications).

我唯一改变的是客户端 ID(用于 Web 应用程序)。

And I STILL get the same error!

我仍然遇到同样的错误!

Since the example code does not work for me, I'm thinking, maybe my settings in the Google API Console are wrong. I have tried to follow the instructions:

由于示例代码对我不起作用,我在想,也许我在 Google API 控制台中的设置是错误的。我试图按照说明进行操作:

  • Google+ API Service is active
  • Redirect URIs: none
  • JavaScript origins are set to localhost and the actual production domain.
  • Google+ API 服务已启用
  • 重定向 URI:无
  • JavaScript 源设置为 localhost 和实际生产域。

Have also tried to upload the example code to the production server, just to see if this was a local environment issue, but I still get the same error.

也尝试将示例代码上传到生产服务器,只是为了看看这是否是本地环境问题,但我仍然遇到相同的错误。

回答by BrettJ

You must include the script and include the clientAPI in addition to the plusonescript:

client除了plusone脚本之外,您还必须包含脚本并包含API :

<script src="https://apis.google.com/js/client:plusone.js" type="application/javascript"></script>

The demo page was doing it incorrectly, which I've fixed.

演示页面做错了,我已经修复了。

回答by Purvesh

gapi.client.load('plus', 'v1', function() {

    var request = gapi.client.plus.people.get({
        'userId': 'me'
    });

    request.execute(function(response) {
        console.log(response);   
    });
});

回答by jj_

This may sound stupid.. but sometimes you just need to close and reopen the browser...

这听起来可能很愚蠢......但有时你只需要关闭并重新打开浏览器......

I tried almost all solutions among almost all relevant SO questions, nothing worked.

我在几乎所有相关的 SO 问题中尝试了几乎所有的解决方案,但没有任何效果。

Finally I decided to see if it was working with another browser, and it did... hence I just closed and reopened the browser I was working in.. and voilà.

最后我决定看看它是否可以与另一个浏览器一起工作,并且确实如此……因此我关闭并重新打开了我正在使用的浏览器……瞧。

I am not sure why it was not working. I was not caching requests, I was reloading the page each time with ctrl-F5 and had also disabled caching within dev tools (for when dev tools are in use).

我不确定为什么它不起作用。我没有缓存请求,我每次都使用 ctrl-F5 重新加载页面,并且还禁用了开发工具中的缓存(在使用开发工具时)。

回答by John

Here are two of the reasons I've gotten his error and I'll update in the future if/when I encounter any more.

这是我收到他的错误的两个原因,如果/当我遇到更多错误时,我会在将来更新。

  1. One of the errors I encountered was that the variables were not being loaded. I use Waterfox by default though I felt like I wasn't getting any useful information. I used Chrome and it gave me a useful error message about the authorize button being null. I had to move the authorizeButton(which I made proper by removing camelCase) directly in to the functions and everything worked.

  2. I also disable third party cookiesby default and again I wasn't getting any useful information. I literally slowly recreated the Waterfox profile until I got to the preferences file and eventually determined that the preference was for third partycookies, enabled them and it all worked again.

  1. 我遇到的错误之一是未加载变量。我默认使用 Waterfox,虽然我觉得我没有得到任何有用的信息。我使用了 Chrome,它给了我一条关于授权按钮为空的有用错误消息。我不得不将authorizeButton(我通过删除camelCase使其正确)直接移动到函数中,一切正常。

  2. 默认情况下,我还禁用了第三方 cookie,我也没有得到任何有用的信息。我从字面上慢慢地重新创建了 Waterfox 配置文件,直到我到达首选项文件并最终确定该首选项是第三方cookie,启用它们,然后一切又恢复了。