简体   繁体   English

Google登录服务器端

[英]Google sign-in for server-side

I followed the tutorial by google but I'm having a problem. 我遵循了google教程,但是遇到了问题。 The callback function is never called. 永远不会调用回调函数。 The code is ran in localhost This is my code: 该代码在localhost中运行这是我的代码:

<!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>

It's as mentioned in the tutorial, but I don't get the document.write I put in the callback function. 正如本教程中提到的那样,但是我没有得到我放在回调函数中的document.write Also, is there a way a can have the user's username after the sign in was successful? 另外,是否有办法在登录成功后获取用户名?

Looks good. 看起来不错。 Make sure you assigned http://localhost:8080 to the JavaScript Origins in the Developers Console , and that you removed the Redirect URL . 确保在开发人员控制台中将http://localhost:8080分配给了JavaScript Origins ,并删除了Redirect URL

Once you get that working, all you need to do to get the userProfile is the following request: 一旦工作成功,您需要做的就是以下请求:

var request = gapi.client.plus.people.get({
   'userId': 'me'
 });
 request.execute(function(resp) {
   console.log('Retrieved profile for:' + resp.displayName);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM