如何在没有登录按钮的情况下初始化 Google 登录?

2024-02-16

下面的代码作为一个简单的测试页面,使用谷歌登录 https://developers.google.com/identity/sign-in/web/:

<html lang="en">
  <head>

    <script type="text/javascript">

        function onGapiLoaded() {
            auth = gapi.auth2.init({
                client_id: "REPLACE_WITH_YOUR_ID",
                scope: "profile email"

            });

            console.log( "signed in: " + auth.isSignedIn.get() );

            auth.isSignedIn.listen( function(signedIn){ console.log( "signedin: " + signedIn ) } );

            gapi.signin2.render( "signInButton", {
                'width': 230,
                'height': 50,
                'longtitle': true,
                'theme': 'dark',
                'onsuccess': onSignIn
            } );
        }

          function onSignIn(googleUser) {
            // Useful data for your client-side scripts:
            var profile = googleUser.getBasicProfile();
            console.log("Name: " + profile.getName());
          };


    </script>

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

  </head>
  <body>
    <div id="signInButton" class="g-signin2"></div>
  </body>
</html>

但是如果我删除class="g-signin2"从登录按钮,或完全删除按钮不仅按钮消失,而且整个登录库停止工作 - 我得到一个未捕获的异常:

无法读取未定义的属性“init”

当我尝试打电话时gapi.auth2.init并且用户未登录。 auth 库似乎依赖于 dom 中存在的按钮来初始化。

我计划将其合并到我的 Angular 应用程序中,该应用程序将在任何按钮出现在舞台上之前初始化服务中的身份验证库。如果用户已经登录,则不会出现登录按钮。 根据这里的行为,如果 DOM 中没有一个按钮,我将无法使用这个库,这看起来相当有限。


为了使用.auth2命名空间,您需要先加载它。合并它并进行一些调整,这是我推荐的代码。

<html lang="en">
  <head>
    <script src="https://apis.google.com/js/api.js"></script>    
    <script type="text/javascript">
        function onSignIn(googleUser) {
            console.log( "signedin");
            // Useful data for your client-side scripts:
            var profile = googleUser.getBasicProfile();
            console.log("Name: " + profile.getName());
        };

        gapi.load('auth2', function() {
            gapi.auth2.init({
                client_id: "REPLACE_WITH_YOUR_ID",
                scope: "profile email" // this isn't required
            }).then(function(auth2) {
                console.log( "signed in: " + auth2.isSignedIn.get() );  
                auth2.isSignedIn.listen(onSignIn);
                var button = document.querySelector('#signInButton');
                button.addEventListener('click', function() {
                  auth2.signIn();
                });
            });
        });
    </script>
  </head>
  <body>
    <div id="signInButton"><img src="IMAGE_FILE" /></div>
  </body>
</html>

有几点需要注意:

  • load api.js代替platform.js
  • 按钮资源不会自动加载,带上你的 https://developers.google.com/identity/branding-guidelines
  • 如果你想利用小部件,你必须使用platform.js and .signin2命名空间,但这是另一种方法,我在这里不会讨论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在没有登录按钮的情况下初始化 Google 登录? 的相关文章

随机推荐

  • 如何注入模拟程序集以与 Moq 一起使用

    我的控制器中有一个方法 它将属性数据从当前执行的程序集返回到分部视图中 在这个例子中 我只是拉动标题 但我需要用它做更多的事情 控制器 var title var asm Assembly GetExecutingAssembly var
  • jQuery - 通过文本描述设置选择控件的选定值

    我有一个选择控件 在 JavaScript 变量中我有一个文本字符串 使用 jQuery 我想将选择控件的选定元素设置为具有我拥有的文本描述的项目 而不是我没有的值 我知道按值设置它非常简单 例如 my select val myVal 但
  • 获取那些有子子 ul 的 li

    如何获得那些li有孩子的ul 我想将 CSS 设置为那些li 我无法设置班级 因为li是动态打印的 当我如下设置 CSS 时 它设置了所有父级li来加 ul width 200px position relative ul li posit
  • 贝塞尔曲线和画布

    如何在画布上绘制贝塞尔曲线 我只有起点和终点 我想从起点到终点画一条线 我怎样才能做到这一点 您可以使用Path quadTo or Path cubicTo 为了那个原因 示例可以在 SDK 示例 FingerPaint 中找到 在你的情
  • backbone.js 解析 1 个元素(Id)

    对于骨干模型上的 id 来说 它只是id并且全部小写 如果我在服务器上的Id被调用怎么办UserId 在主干的解析方法中 我该如何更改UserId to id并对所有其他属性使用相同的名称 For eg window User Backbo
  • JTable 的页脚

    JTable 不支持显示包含每列聚合数据的页脚 受到建议解决方案的启发Oracle Suns 错误数据库 https bugs java com bugdatabase view bug bug id 4242646看起来很有希望 我从用滚
  • Python + 正则表达式:如何在Python中提取两个下划线之间的值?

    我正在尝试提取两个下划线之间的值 为此我写了这段代码 patient ids for file in files print file patient id re findall file patient ids append patien
  • componentWillReceiveProps 与 getDerivedStateFromProps

    componentWillReceiveProps 和 getDerivedStateFromProps 到底是什么对我来说是个微妙的问题 因为 我在使用 getDerivedStateFromProps 时遇到了一个问题 Componen
  • python 请求和 cx_freeze

    我试图冻结一个依赖于请求的 python 应用程序 但出现以下错误 Traceback most recent call last File c Python33 lib site packages requests packages ur
  • 找不到 SPSite 名称空间

    我无法找到名称空间SPSite 到目前为止我已经导入了这些 using System using System Collections Generic using System Linq using System Text using Sy
  • Solr 创建核心时出错:在架构中找不到 fieldType [x]

    我正在尝试让 Solr 核心与我自己的一起运行schema xml 但是Solr 版本5 2 1 一直抱怨丢失fieldType甚至不在我的元素中fields定义 org apache solr common SolrException f
  • 我的股票市场计划可以使用什么数据源? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我想为 Linux 和 Windows 制作一个免费的开源 C 应用程序 它将创建实时股票市场图表 即
  • 使用 Windbg 调试 .NET OutOfMemoryException

    我需要帮助调试 net dll 中的 OutOfMemoryException 该 dll 将 rtf 文本转换为原始文本或 html 这是转换代码 http matthewmanela com blog converting rtf to
  • 永久删除机器范围的包源

    我最近从我的计算机上卸载了 Service Fabric 及其 SDK 然而 它留下了一个机器范围的包源 微软 Azure Service Fabric SDKC Program Files Microsoft SDKs Service F
  • 递归地用innerHTML替换shadowRoot[重复]

    这个问题在这里已经有答案了 我想获得提取了shadowRoot 元素的纯HTML 网站上有很多 ShadowRoot 元素 并且嵌套很深 我用那段代码来实现它 但它只提取了部分元素 const getShadowDomHtml shadow
  • 如何在 AngularJS 中丢弃预检响应

    当向我的服务发送 http post 请求时 我首先发送一个 Option 请求 根据 Cors 的要求 然而我已经意识到我的OPTIONS 飞行前 请求返回否response data在其回应中 但我的POST请求是 这带来了一个问题 因
  • 如何更改 Chart.JS 中标签的字体(系列)?

    我想将 Chart JS 水平条形图中的字体更改为更时尚的字体 我尝试过以下方法 但都不起作用 var optionsBar fontFamily Candara Calibri Courier serif bodyFontFamily C
  • 如何在 C# 中为用户获取 AD LDS 条目的有效权限?

    我有第 3 方应用程序使用 AD LDS 来存储其分层数据 并且我需要在 ASP NET MVC 中为此系统提供 Web UI 它在目录条目上使用 DACL 来控制用户对各个条目的访问权限 我发现了一些解释如何阅读的好文章ActiveDir
  • Xcode 总是显示“FBSDKShareKit/FBSDKShareKit”.h 文件未找到

    无论我做什么 我可能做错了 当我尝试构建 Unity IOS 应用程序进行测试时 Xcode 总是显示 FBSDKShareKit FBSDKShareKit h 文件未找到 我已在 Xcode 项目中添加了 iOS 快速入门中指示的 Fa
  • 如何在没有登录按钮的情况下初始化 Google 登录?

    下面的代码作为一个简单的测试页面 使用谷歌登录 https developers google com identity sign in web