使用JavaScript无法启动Google+登录流程

时间:2014-12-30 20:06:26

标签: javascript google-login google-plus-signin

我正在关注当前的tutorial,出于某种原因,当我点击“使用Google登录”按钮时似乎没有任何事情发生,我不完全确定原因。这是代码:

<html>
<head>
    <title></title>

    <script src="https://apis.google.com/js/client:platform.js" async defer></script>

</head>
<body>

    <meta name="google-signin-clientid" content="782332402251-os0n348u3v5vaq5kff87f5pc65ib6i19.apps.googleusercontent.com" />
    <meta name="google-signin-scope" content="https://www.googleapis.com/auth/plus.login" />
    <meta name="google-signin-requestvisibleactions" content="http://schema.org/AddAction" />
    <meta name="google-signin-cookiepolicy" content="single_host_origin" />

    <script src="https://apis.google.com/js/client:platform.js?onload=render" async defer>
         /* Executed when the APIs finish loading */
         function render() 
         {
           // Additional params including the callback, the rest of the params will
           // come from the page-level configuration.
           var additionalParams = {
             'callback': signinCallback
           };

           // Attach a click listener to a button to trigger the flow.
           var signinButton = document.getElementById('signinButton');

           signinButton.addEventListener('click', function() {
             gapi.auth.signIn(additionalParams); // Will use page level configuration
           });
         }


         function signinCallback(authResult) 
         {
              if (authResult['status']['signed_in']) {
                // Update the app to reflect a signed in user
                // Hide the sign-in button now that the user is authorized, for example:
                document.getElementById('signinButton').setAttribute('style', 'display: none');
              } else {
                // Update the app to reflect a signed out user
                // Possible error values:
                //   "user_signed_out" - User is signed-out
                //   "access_denied" - User denied access to your app
                //   "immediate_failed" - Could not automatically log in the user
                console.log('Sign-in state: ' + authResult['error']);
              }
         }
    </script>


    <button id="signinButton">Sign in with Google</button>

</body>
</html>

知道我哪里出错了?

1 个答案:

答案 0 :(得分:6)

在教程中看起来像是一个错误。在HTML5中,带有src属性的脚本标记中的Javascript是not valid。您需要将代码移动到单独的脚本标记中。

<script type="text/javascript">
// your code here
</script>

请参阅此处 - http://jsfiddle.net/7umb41z2/