问题
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.
回答1:
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.
回答2:
gapi.client.load('plus', 'v1', function() {
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(response) {
console.log(response);
});
});
回答3:
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).
回答4:
Here are two of the reasons I've gotten his error and I'll update in the future if/when I encounter any more.
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.
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.
来源:https://stackoverflow.com/questions/19476332/getting-gapi-client-is-undefined-when-trying-to-retrieve-an-authenticated-goog