问题
I am using Google plus API to get email id from viewers in my website.
Google reference:https://developers.google.com/+/web/signin/add-button
The problem is when i click signin button i getting error "TypeError: gapi.client.plus is undefined" in browser console.
How to solve it.
My code:
<script src="https://apis.google.com/js/client:plusone.js?onload=signinCallback" type="text/javascript"></script>
<span id="signinButton">
<span class="g-signin"
data-callback="signinCallback"
data-clientid="*****************.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin"
data-scope="profile">
</span>
</span>
function signinCallback(authResult) {
gapi.client.load('plus', 'v1',function(){});
if (authResult['status']['signed_in']) {
alert("login success");
document.getElementById('signinButton').setAttribute('style',
'display: none');
var request = gapi.client.plus.people.get({
'userId' : 'me'
});
request.execute(function(resp) {
var email = '';
if(resp['emails'])
{
for(var i = 0; i < resp['emails'].length; i++)
{
if(resp['emails'][i]['type'] == 'account')
{
email = resp['emails'][i]['value'];
}
}
}
alert("email ="+email);
console.log('ID: ' + resp.id);
console.log('Display Name: ' + resp.displayName);
console.log('Image URL: ' + resp.image.url);
console.log('Profile URL: ' + resp.url);
});
} else {
alert("login unsuccessful");
console.log('Sign-in state: ' + authResult['error']);
}
}
回答1:
I had this problem,
I believe the problem is here
gapi.client.load('plus', 'v1',function(){});
The third parameter is empty, which Google declares as optional, but it tends to error as undefined because it's not being used or reached by the functions that are trying to use it.
Instead, you can try to nest the functions that need the gapi.client within that third parameter, or reference an external function there.
Here is what I did:
Google signin callback - get name and email
回答2:
I belive the issue could be scope property in the params json passed as parameter for signIn API.
Scope parameter should contain below urls
"https://www.googleapis.com/auth/plus.login"
"https://www.googleapis.com/auth/plus.me"
and may be /userinfo.email, /userinfo.profile
来源:https://stackoverflow.com/questions/26419431/google-sign-in-error-gapi-client-plus-is-undefined