Platform
通过 Google 注册
9 分
使用 google 登录教程 介绍 使用 google 登录允许用户使用他们的 google 账户登录应用程序。 先决条件 要完成本教程,您需要: 在 back4app 创建的应用程序 查看 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 上创建应用程序。 为您的 back4app 应用设置子域名 查看 https //www back4app com/docs/platform/activating web hosting 以了解如何在 back4app 中创建子域名。 一个 https //developers google com/?hl=pt br 。 1 创建一个新的 back4app 应用 首先,您需要确保在 back4app 上创建了一个现有的应用程序。如果您是新用户,可以查看 https //www back4app com/docs/get started/new parse app 以了解如何创建一个 2 创建一个新的客户端标识符 登录到您的 https //developers google com/ 并转到 google api 控制台 google api 控制台 。点击 凭据 凭据 并选择 oauth 2 0 客户端 id oauth 2 0 客户端 id 如果您没有同意屏幕,google 会要求您创建一个。点击 配置同意屏幕 配置同意屏幕 ,您将被重定向到以下页面 完成屏幕同意配置并点击 保存 保存 选择您需要的平台。在这个例子中,我使用的是 javascript(web 应用程序),但您应该选择您将使用的那个 在 授权的 javascript 来源 授权的 javascript 来源 , 用您的子域名替换 url。 在 授权的重定向 uri 授权的重定向 uri , 插入您的子域名,后面跟上 /redirect /redirect 如下图所示 注意 如果您尚未启用子域名,请查看以下指南以了解如何操作 https //www back4app com/docs/platform/activating web hosting 之后,您应该拥有您的客户端 id 和密钥 3 获取您的代码 访问以下 url,替换 redirect uri redirect uri 和 client id client id 为您创建的值 https //accounts google com/o/oauth2/v2/auth?scope=https%3a//www googleapis com/auth/drive metadata readonly\&access type=offline\&include granted scopes=true\&response type=code\&state=state parameter passthrough value\&redirect uri=redirect url\&client id=client id 获取 auth token 和后来的 user id 所需的范围是 https //www googleapis com/auth/userinfo email https //www googleapis com/auth/plus me https //www googleapis com/auth/userinfo profile 使用您的 google 账户登录,重定向的网站将在 url 中包含您的代码 仅复制 url 的代码部分,并运行以下 curl 命令,替换值 your code your code , client id client id , client secret client secret , 和 redirect uri redirect uri 作为您应用程序的值 1 curl x post \\ 2 https //oauth2 googleapis com/token \\ 3 f 'grant type=authorization code' \\ 4 f 'code=your code' \\ 5 f 'client id=client id' \\ 6 f 'client secret=client secret' \\ 7 f 'redirect uri=redirect uri' 运行它,您应该能获取到您的访问令牌 记住:代码只能使用一次。如果您遇到错误或未使用您的令牌,您必须重新生成代码才能再次运行它。 现在是时候检索你的 google 用户 id google 用户 id 。这是一个数字字符串,你将在第 4 步中作为 id id 传递。 为此,运行以下命令,将 你的令牌 你的令牌 字符串替换为你在上一个命令中收到的令牌。 1 curl x get https //www googleapis com/userinfo/v2/me?access token=your token 4 开始开发 现在 google 登录已配置,你可以开始开发过程。 authdata 的格式是: 1 { 2 "google" { 3 "id" "user's google id (string)", 4 "id token" "an authorized google id token for the user (use when not using access token)", 5 "access token" "an authorized google access token for the user (use when not using id token)" 6 } 7 } 这是 ios sdk 的方法: 1 pfuser loginwithauthtype(inbackground "google", authdata \["access token"\ tokenstring, "id" user]) continuewith { task > any? in 2 3 } 这里是 android sdk 的方法: 1 map\<string, string> authdata = new hashmap\<string, string>(); 2 authdata put("access token", tokenstring); 3 authdata put("id", user); 4 parseuser loginwithinbackground("google", authdata){ 5 6 } 请记住,这必须在每个用户的每次登录时完成。