uni-app中使用旧版Google登录API提示已被弃用并计划于2025年移除

uni-app中使用旧版Google登录API提示已被弃用并计划于2025年移除

从 Google 登录 API 迁移到 Credential Manager

您使用的是旧版 Google 登录 API,这种 API 已被弃用并计划于 2025 年移除。如需详细了解如何改而通过 Credential Manager 使用 Google 账号登录,请阅读我们的迁移指南。

迁移地址:https://developer.android.com/identity/sign-in/legacy-gsi-migration?hl=zh-cn

image


更多关于uni-app中使用旧版Google登录API提示已被弃用并计划于2025年移除的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app中使用旧版Google登录API提示已被弃用并计划于2025年移除的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中使用Google登录API时,如果收到提示该旧版API已被弃用并计划于2025年移除,你应该尽快迁移到新版Google登录API。下面是一个使用新版Google登录API的示例代码,该示例展示了如何在uni-app中实现Google登录功能。

首先,确保你已经在Google Cloud Console中创建了OAuth 2.0客户端ID,并配置了重定向URI。然后,你需要下载Google Identity Services的JavaScript库。

以下是实现步骤:

  1. 引入Google Identity Services库

在你的uni-app项目中,你可以在需要显示Google登录按钮的页面中引入Google Identity Services库。由于uni-app支持使用HTML/CSS/JavaScript,你可以直接在页面的<script>标签中引入。

<script src="https://accounts.google.com/gsi/client" async defer></script>
  1. 配置并显示Google登录按钮

在你的页面中,添加一个用于显示Google登录按钮的HTML元素,并配置Google Identity Services。

<template>
  <view>
    <div id="g_id_onload"
         data-client_id="YOUR_CLIENT_ID.apps.googleusercontent.com"
         data-auto_prompt="false">
    </div>
    <div class="g_id_signin"
         data-size="wide"
         data-theme="outline"
         data-text="sign_in_with"
         data-logo_alignment="left">
    </div>
  </view>
</template>

<script>
export default {
  mounted() {
    window.google.accounts.id.initialize({
      client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
      callback: this.handleCredentialResponse
    });
    window.google.accounts.id.prompt();
  },
  methods: {
    handleCredentialResponse(credential) {
      // 处理从Google返回的凭证
      const idToken = credential.credential;
      // 发送idToken到你的服务器进行验证
      uni.request({
        url: 'YOUR_SERVER_URL/verify-token',
        data: { idToken },
        method: 'POST',
        success: (res) => {
          // 处理服务器响应
          console.log('Google login successful:', res.data);
        },
        fail: (err) => {
          console.error('Google login failed:', err);
        }
      });
    }
  }
}
</script>
  1. 服务器端验证

在你的服务器端,使用Google提供的API验证从客户端收到的idToken。这通常涉及向Google的tokeninfo端点发送一个请求,并检查返回的响应。

注意:上面的代码示例是一个基本的实现,实际项目中你可能需要处理更多的边缘情况和错误处理。此外,确保你的应用遵循Google的身份验证和授权最佳实践。

回到顶部