我正在尝试使用Google Client API来访问与Coffeescript竞赛的一些用户数据。
到目前为止,我使用此代码首次登录成功访问了数据:
initSignIn= () ->
gapi.load('client:auth2', initClient)
# Init click function for sign in button
$('#signin-button').on 'click', (e) ->
gapi.auth2.getAuthInstance().signIn()
return false
return
initClient= () ->
# Init the client API
$.when(
gapi.client.init({
apiKey: 'AIzxxx',
discoveryDocs: ["https://people.googleapis.com/$discovery/rest?version=v1"],
clientId: '31xxx.apps.googleusercontent.com',
scope: 'profile https://www.googleapis.com/auth/youtube.readonly'
})
).then(
# Add listeners for sign in changes
gapi.auth2.getAuthInstance().isSignedIn.listen( updateSigninStatus )
updateSigninStatus( gapi.auth2.getAuthInstance().isSignedIn.get() )
)
return
updateSigninStatus= (isSignedIn) ->
# If the user is signed in then make the call for getting the user data
if isSignedIn then makeApiCall()
return
makeApiCall= () ->
# Test logs
console.log "gapi.client"
console.log gapi.client
console.log "gapi.client.people"
console.log gapi.client.people
# Getting the user data
gapi.client.people.people.get({
resourceName: 'people/me',
'requestMask.includeField': 'person.names,person.email_addresses',
requestMask: {
includeField: 'person.names,person.email_addresses'
}
}).then(
(response) ->
apiCalled = false
console.log(response);
console.log('Hello, ' + response.result.names[0].givenName)
return
(reason) ->
apiCalled = false
console.log('Error: ' + reason.result.error.message)
return
)
return
当我刷新浏览器时,我仍然会在某些时候获取数据。但往往它会让人们接触到#34;财产无法加载。我打印出人员属性后,在控制台中打印出客户端对象。它向我展示了带有people对象的客户端对象,但下一行它无法再读取people对象:
我在这里做错了吗?这是我第一次使用客户端API。谢谢你的帮助!