Javascript 谷歌服务器端登录
我遵循了规则,但我遇到了一个问题。永远不会调用回调函数。 代码在localhost中运行 这是我的代码:Javascript 谷歌服务器端登录,javascript,google-api,google-signin,Javascript,Google Api,Google Signin,我遵循了规则,但我遇到了一个问题。永远不会调用回调函数。 代码在localhost中运行 这是我的代码: <!DOCTYPE html> <html lang="en"> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script src=&
<!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>
函数start(){
load('auth2',function(){
auth2=gapi.auth2.init({
客户端id:“”,
范围:['https://www.googleapis.com/auth/calendar']
});
});
}
函数signInCallback(authResult){
文件。填写(“此处”);
if(authResult['code']){
编写(“代码是好的”);
}否则{
文件。写入(“错误”);
}
}
使用谷歌登录
$(“#登录按钮”)。单击(函数(){
auth2.grantoflineaccess({'redirect_uri':'postmessage'});
});
正如教程中提到的,但是我没有得到文档。写我放在回调函数中。
另外,是否有一种方法可以在登录成功后获得用户的用户名?看起来不错。确保您已分配http://localhost:8080
到中的JavaScript源文件
,并且您删除了重定向URL
一旦开始工作,您只需执行以下请求即可获得userProfile:
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
console.log('Retrieved profile for:' + resp.displayName);
});
改为尝试记录结果,console.log(authResult)编码>,并检查开发工具中的输出(按F12)。有回应吗?试过了,什么都没有。就好像它根本就没有达到那个功能一样,嗯,令人沮丧。您确定已分配http://localhost:8080
转到开发者控制台中的JavaScript源代码
,并删除重定向URL?这就是问题所在!我错过了港口!谢谢好,我就是按照这个,