使用 JavaScript 启动 Google+ 登录流程不起作用

Initiating the Google+ Sign-In flow with JavaScript not working

本文关键字:不起作用 登录 Google+ JavaScript 启动 使用      更新时间:2023-09-26

我正在按照当前的教程进行操作,出于某种原因,每当我单击"使用 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>

知道我哪里出错了吗?

看起来教程中的一个错误。在具有 src 属性的脚本标记中嵌入 Javascript 在 HTML5 中无效。您需要将代码移动到单独的脚本标记中。

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

看这里 - http://jsfiddle.net/7umb41z2/