我们允许使用 oauth 和 google 进行身份验证。对于我们的绝大多数客户,显示的代码都可以正常工作。然而,对于少数人来说,它失败了。该问题与网络无关,因为我可以使用用户学校提供的谷歌电子邮件帐户导致问题,然后切换到个人帐户并且不会出现问题。如果问题确实发生,它会发生在整个学校,这让我相信它与他们的谷歌帐户中的设置相关,但似乎没有人知道这个问题。帮助!!!
问题出在 makeAPICall 函数中,对于大多数用户来说,我们可以在此之后访问电子邮件,但在问题学校中,电子邮件是未定义的。但是没有返回错误。
<script type="text/javascript" src="https://apis.google.com/js/api.js" "></script>
<script type="text/javascript">
var apiKey = 'MY API KEY';
var discoveryDocs = ["https://people.googleapis.com/$discovery/rest?version=v1"];
var clientId = 'MY CLIENT ID';
var scopes = 'profile';
function handleClientLoad() {
// Load the API client and auth2 library
gapi.load('client:auth2', initClient);
}
function initClient() {
gapi.client.init({
apiKey: apiKey,
discoveryDocs: discoveryDocs,
clientId: clientId,
scope: scopes,
'immediate': false
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
makeApiCall();
}
}
function handleAuthClick() {
var isSignedIn = gapi.auth2.getAuthInstance().isSignedIn.get();
if (isSignedIn) {
makeApiCall();
}
else {
gapi.auth2.getAuthInstance().signIn();
}
}
function makeApiCall() {
// Load the API and make an API call. Display the results on the screen.
// for most users this loads the api and allows me to access the email address.
// for certain schools the email addresss is not returned causing lots of problems
gapi.client.people.people.get({
'resourceName': 'people/me',
'requestMask.includeField': 'person.emailAddresses'
}).then(function (resp) {
//in the case of the email not being returned, the next line errors
var email = resp.result.emailAddresses[0].value;
$.ajax({
//do some application specific stuff using the email address
}
});
});
}
</script>
<script type="text/javascript" async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>