首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebApplication谷歌登录

WebApplication谷歌登录
EN

Stack Overflow用户
提问于 2017-03-13 23:21:30
回答 1查看 3.1K关注 0票数 1

我正在尝试这样做:https://developers.google.com/identity/sign-in/web/sign-in,所以我在我的index.html中添加了以下内容:

代码语言:javascript
复制
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="changedkey.apps.googleusercontent.com ">

在login.html上,我尝试使用以下命令:

代码语言:javascript
复制
<div class="g-signin2" data-onsuccess="onSignIn"></div>

还有这个:

代码语言:javascript
复制
<div id="my-signin2">Google </div>
                      <script>
                        function onSuccess(googleUser) {
                          console.log('Logged in as: ' + googleUser.getBasicProfile().getName());
                        }
                        function onFailure(error) {
                          console.log(error);
                        }
                        function renderButton() {
                          gapi.signin2.render('my-signin2', {
                            'scope': 'profile email',
                            'width': 240,
                            'height': 50,
                            'longtitle': true,
                            'theme': 'dark',
                            'onsuccess': onSuccess,
                            'onfailure': onFailure
                          });
                        }
                      </script>

但它们都不工作,我按下按钮,什么也没有发生。我做错了什么?

编辑:我更改了代码,如下所示:

代码语言:javascript
复制
<script>
                  var googleUser = {};
                  var startApp = function() {
                    gapi.load('auth2', function(){
                      // Retrieve the singleton for the GoogleAuth library and set up the client.
                      auth2 = gapi.auth2.init({
                        client_id: 'MYKEY.apps.googleusercontent.com',
                        cookiepolicy: 'single_host_origin',
                        // Request scopes in addition to 'profile' and 'email'
                        //scope: 'additional_scope'
                      });
                      attachSignin(document.getElementById('google'));
                    });
                  };

                  function attachSignin(element) {
                    console.log(element.id);
                    auth2.attachClickHandler(element, {},
                        function(googleUser) {
                          document.getElementById('name').innerText = "Signed in: " +
                              googleUser.getBasicProfile().getName();
                        }, function(error) {
                          alert(JSON.stringify(error, undefined, 2));
                        });
                  }
                  </script>

按钮如下所示:

代码语言:javascript
复制
  <div id="google" type="button"><img src="assets/login/google-logo.png"><span class="google-title">GOOGLE</span></div>

它进入函数,为Google Log in创建窗口,但总是返回一个弹出窗口,上面写着:"error":"popup_closed_by_user“为什么会这样?我还在控制台中获得了以下内容:https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/nGKpPpPQ1vlvC27/upload.png

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-15 17:40:51

我在这里找到了答案:Google API authentication: Not valid origin for the client我的问题是我使用的是服务器创建的cliendID,而不是新创建的,当我这样做时,我已经设置了源,现在它可以工作了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42767275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档