使用Google进行身份验证,无需重定向而不是弹出窗口

时间:2020-04-03 06:39:24

标签: html authentication google-login

我创建了一个Google登录按钮:

<!DOCTYPE html>
<html>

<head>
  <title>Google Auth Demo</title>
  <meta name="google-signin-client_id" content="xxxx.apps.googleusercontent.com">
  <script src="https://apis.google.com/js/platform.js" async defer></script>
  <script>
    function signOut() {
      gapi.auth2.getAuthInstance().signOut().then(function() {
      console.log('user signed out')
    })};
    function onSignIn(googleUser) {
      console.log(googleUser.getBasicProfile());
    } 
  </script>
</head>
<body>
  <h1>Welcome to the Demo</h1>
  <div class="g-signin2" data-onsuccess="onSignIn" data-ux_mode="redirect" ></div>
  <button onclick="signOut()" >Sign out</button>
</body>
</html>

我将参数从弹出窗口更改为data-ux_mode="redirect"。如何配置字段Authorized redirect URIs或在https://console.developer.google上为我的应用程序更改其他内容,以便可以在本地主机上使用它?

我在这里发现了一个紧密的问题:https://github.com/google/google-api-javascript-client/issues/288#issuecomment-289064472。因此,可以在没有打开弹出窗口的情况下实现代码验证按钮Google示例吗?

1 个答案:

答案 0 :(得分:1)

您需要添加授权域,并使用此URL将URI重定向到您的客户端ID或API密钥

https://console.cloud.google.com/apis/credentials?project= {YOUR-GOOGLE-CLOUD-PROJECT-NAME}

基本上在GCP控制台的左侧导航栏上,转到API和服务。在那寻找您在项目中使用的oAuth2.0客户端ID。单击它后,您将拥有一个界面,用于配置授权的重定向URI和JavaScript的原始来源

编辑:经过链接的github问题(该问题仍在打开中)后,无法使用重定向UX将令牌获取到本地计算机。他们有计划在将来支持它,但目前仅适用于弹出方法。

您好,@ Jeevsxp,这不可能获得授权码 没有弹出窗口。这是一个安全限制:脱机代码将 允许您在服务器中获取refresh_token,这使您 您随时可以获取新的access_token的可能性。为了那个原因, 用户需要明确的同意

相关问题