简体   繁体   中英

Google sign-in for server-side

I followed the tutorial by google but I'm having a problem. The callback function is never called. The code is ran in localhost This is my code:

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

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);
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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