最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting "gapi.client is undefined" when trying to retrieve an authenticated Google+ user'

programmeradmin0浏览0评论

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.

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

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

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 Service is active
  • Redirect URIs: none
  • JavaScript origins are set to localhost and the actual production domain.

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.

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.

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

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

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 Service is active
  • Redirect URIs: none
  • JavaScript origins are set to localhost and the actual production domain.

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.

Share Improve this question edited Oct 20, 2013 at 10:37 dannepanne asked Oct 20, 2013 at 10:29 dannepannedannepanne 4272 gold badges5 silver badges15 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

You must include the script and include the client API in addition to the plusone script:

<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.

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

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

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

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.

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).

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 cookies by 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 party cookies, enabled them and it all worked again.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论