我遵循了谷歌的教程,但我有一个问题。回调函数永远不会被调用。代码在本地主机上运行这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '<my-client-id>',
scopes: ['https://www.googleapis.com/auth/calendar']
});
});
}
function signInCallback(authResult) {
document.write("here");
if (authResult['code']) {
document.write("code is good");
} else {
document.write("error");
}
}
</script>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button id="signinButton">Sign in with Google</button>
<div id="result"></div>
<script>
$('#signinButton').click(function() {
auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(signInCallback);
});
</script>
</body>
</html>
正如教程中提到的,但是我没有得到我放在回调函数中的document.write
。此外,是否有一种方法可以在登录成功后拥有用户的用户名?
看起来不错。确保您在开发人员控制台中将http://localhost:8080
分配给JavaScript Origins
,并删除Redirect URL
。
一旦你得到了工作,所有你需要做的是获得userProfile是以下请求:
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
console.log('Retrieved profile for:' + resp.displayName);
});