为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

九州编程 2023-10-30 15:37:30
我创建了一个 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>我将参数从 popup 更改为data-ux_mode="redirect". 如何在https://console.developer.googleAuthorized redirect URIs上配置该字段或更改我的应用程序的其他内容,以便我可以在本地主机上使用它?我在这里找到了一个关闭的问题:https://github.com/google/google-api-javascript-client/issues/288#issuecomment-289064472。那么它可以用于实现代码验证按钮 Google 示例而无需打开弹出窗口吗?
查看完整描述

1 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

您需要添加授权域并使用此 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。为此,需要用户明确同意


查看完整回答
反对 回复 2023-10-30
  • 1 回答
  • 0 关注
  • 57 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信