【问题标题】:New google identity service change新的谷歌身份服务变更
【发布时间】:2021-10-26 10:10:16
【问题描述】:

我正在使用 google 身份服务 (https://developers.google.com/identity/gsi/web/reference/js-reference) 登录我的网络游戏应用程序。 使用以下代码完成初始化。

google.accounts.id.initialize({ client_id: 'YOUR_GOOGLE_CLIENT_ID', 回调:handleCredentialResponse });

我想通过单击按钮来调用 google promt 弹出窗口,因为我无法在游戏中使用默认按钮。我们遵循相同的准则来创建一个新的。 我尝试使用

google.accounts.id.prompt();

,但它只在活动会话的情况下有效。请在这里帮助我 提前致谢。

【问题讨论】:

    标签: single-sign-on google-signin


    【解决方案1】:

    如果您想呈现“使用 Google 登录”按钮、一键式或两者都完全来自 JavaScript,这可能会有所帮助:

    <html>
      <body>
        <script src="https://accounts.google.com/gsi/client" async defer></script>
        <script>
        function handleCredentialResponse() {
          console.log('hello, world');
        }
        window.onload = function () {
        
          google.accounts.id.initialize({
            client_id: 'YOUR_CLIENT_ID',
            callback: handleCredentialResponse
          });
          
          // Display the One Tap prompt
          google.accounts.id.prompt();
          
          // Display the Sign In With Google Button
          google.accounts.id.renderButton(
            document.getElementById("buttonDiv"),
            { theme: 'outline', size: 'large' }
          );
        }
        </script>
    
    
        <pre>One Tap renders a popup, button requires a div to position it on the page</pre>
        <div id='buttonDiv'></div>
      </body>
    </html>
    

    您只需有条件地调用id.promptid.renderButton 方法来控制它们何时显示给用户。

    如果用户有一个活跃的会话——他们已登录到他们的 Google 帐户,“使用 Google 登录”按钮将显示他们的姓名、电子邮件、个人资料照片......这是个性化的。如果他们未登录 Google 帐户,则该按钮将默认显示“使用 Google 登录”或 data-text 属性的值。

    仅供参考,一键提示可能不显示有几个正当的原因。您需要考虑使用PromptMomentNotification 进行相应的调试和调整。

    【讨论】:

    • 'id.prompt' 在我的情况下可以工作,但问题是应该有一个活动的谷歌会话,否则它将无法工作。
    • 对,这是设计使然。为了将具有用户身份的 JWT 共享到您的应用程序,用户必须首先登录到他们的 Google 帐户并获得共享其个人资料的权限。请注意,Google 帐户与用户之间的活动会话与用户与您的应用之间的会话不同。
    • 有没有办法手动调用谷歌登录按钮功能?正如我之前提到的,我不能使用 Google 默认按钮。
    猜你喜欢
    • 2022-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多