我正在尝试对用户进行身份验证并获取名称和电子邮件等用户数据。我正在关注google的文档。这是我的身份验证代码:
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function initClient() {
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS =
["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/gmail.readonly';
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function (data) {
}, function(error) {
});
}
当用户单击按钮时:
if(!gapi.auth2.getAuthInstance().isSignedIn.get()){
gapi.auth2.getAuthInstance().signIn();
}
// user IS authenticated here
var profile = gapi.auth2.getAuthInstance().currentUser.get();
console.log('Email: ' + profile.getEmail());
当我console.log(gapi.auth2)
,我看到我有功能BasicProfile()
,但如果我使用gapi.auth2.BasicProfile().getEmail()
我收到此错误:
cb = gapi.loaded_0:219 Uncaught TypeError:无法在Object.PG [as BasicProfile]中读取null的属性'sub'(cb = gapi.loaded_0:219)
一个解决方案
// The user is authenticated here
gapi.client.gmail.users.getProfile({
'userId': 'me'
}).then(function(response) {
console.log(response);
});