google

来自于:APICloud立即使用

showSignInButton hasAuth getProfileImage signInSilently getScopes setScopes getTokens refreshTokens signOut disconnect signIn

概述

谷歌登录简介

谷歌登录包括很多种,详情参考谷歌官方文档———谷歌 OAuth 2.0 指南

Basic steps

All applications follow a basic pattern when accessing a Google API using OAuth 2.0. At a high level, you follow four steps:

  1. Obtain OAuth 2.0 credentials from the Google API Console.

Visit the Google API Console to obtain OAuth 2.0 credentials such as a client ID and client secret that are known to both Google and your application. The set of values varies based on what type of application you are building. For example, a JavaScript application does not require a secret, but a web server application does.

  1. Obtain an access token from the Google Authorization Server.

Before your application can access private data using a Google API, it must obtain an access token that grants access to that API. A single access token can grant varying degrees of access to multiple APIs. A variable parameter called scope controls the set of resources and operations that an access token permits. During the access-token request, your application sends one or more values in the scope parameter.

There are several ways to make this request, and they vary based on the type of application you are building. For example, a JavaScript application might request an access token using a browser redirect to Google, while an application installed on a device that has no browser uses web service requests.

Some requests require an authentication step where the user logs in with their Google account. After logging in, the user is asked whether they are willing to grant the permissions that your application is requesting. This process is called user consent.

If the user grants the permission, the Google Authorization Server sends your application an access token (or an authorization code that your application can use to obtain an access token). If the user does not grant the permission, the server returns an error.

It is generally a best practice to request scopes incrementally, at the time access is required, rather than up front. For example, an app that wants to support purchases should not request Google Wallet access until the user presses the “buy” button; see Incremental authorization.

  1. Send the access token to an API.

After an application obtains an access token, it sends the token to a Google API in an HTTP authorization header. It is possible to send tokens as URI query-string parameters, but we don’t recommend it, because URI parameters can end up in log files that are not completely secure. Also, it is good REST practice to avoid creating unnecessary URI parameter names.

Access tokens are valid only for the set of operations and resources described in the scope of the token request. For example, if an access token is issued for the Google+ API, it does not grant access to the Google Contacts API. You can, however, send that access token to the Google+ API multiple times for similar operations.

  1. Refresh the access token, if necessary.

Access tokens have limited lifetimes. If your application needs access to a Google API beyond the lifetime of a single access token, it can obtain a refresh token. A refresh token allows your application to obtain new access tokens.

本模块封装了 Installed applications 方式的谷歌登录功能。

Installed applications

The Google OAuth 2.0 endpoint supports applications that are installed on devices such as computers, mobile devices, and tablets. When you create a client ID through the Google API Console, specify that this is an Installed application, then select Android, Chrome, iOS, or “Other” as the application type.

The process results in a client ID and, in some cases, a client secret, which you embed in the source code of your application. (In this context, the client secret is obviously not treated as a secret.)

The authorization sequence begins when your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested. Google handles the user authentication, session selection, and user consent. The result is an authorization code, which the application can exchange for an access token and a refresh token.

The application should store the refresh token for future use and use the access token to access a Google API. Once the access token expires, the application uses the refresh token to obtain a new one.

For details, see Using OAuth 2.0 for Installed Applications.

模块使用攻略

Get an OAuth client ID

If you haven’t already created an OAuth client ID, click the button below to do so.

CREAT AN OAUTH CLIENT ID

After you create the OAuth client ID, take note of the client ID string, which you will need to configure Google Sign-in in your app. You can optionally download the configuration file, which contains your client ID and other configuration data, for future reference. 注意:iOS端这个过程需要输入包名(Bundle ID)例如:com.apicloud.apploader。

创建完成后会得到一个 configuration 文件(credentials.plist)和 Client ID。

If you already created an OAuth client ID, you can find your existing OAuth information by clicking the button below.

GET AN EXISTING OAUTH CLIENT ID

Add a URL scheme to your project(iOS端)

Google Sign-in requires a custom URL Scheme to be added to your project. 详见 config.xml 文件配置。

自定义附加模块(iOS端)

接下来下载 googleAppendix 模块 zip 包并解压,把 zip 包内的 GoogleService-Info.plist 文件替换为自己上一步创建下载的 .plist 文件。然后重新压缩为 zip 包文件上传自定义模块,云编译时勾选该模块。

配置 config.xml 文件

注意:配置完毕,需通过云端编译生效。

IOS 配置方法如下:

  • 名称:google
  • 参数:urlScheme、clientID
  • 配置示例:
  1. <feature name="google">
  2. <param name="urlScheme" value="googled0d84bbf23b4a0e4"/>
  3. <param name="ios_clientID" value="googled0d84bbf23b4a0e4"/>
  4. </feature>
  • 字段描述:

    urlScheme:(必须配置)用于实现应用间跳转及数据交换,本应用可以启动谷歌客户端(谷歌的任何一个移动端app),也可以跳回本应用。urlScheme 的 value 值:The reversed client ID is your client ID with the order of the dot-delimited fields reversed. For example:,如:com.googleusercontent.apps.1234567890-abcdefg

    clientID:(必须配置)该值是从上一步创建的 GoogleService-Info.plist 文件内的 CLIENT_ID 对应的参数,如:1234567890-abcdefg.apps.googleusercontent.com。

  1. 服务器端验证 token 的方法:https://developers.google.com/identity/sign-in/ios/backend-auth

谷歌登陆模块android平台使用

andorid 平台使用本模块需要注意:

【其他注意事项】:

android 模块使用配置方法如下:

  • 名称:google
  • 参数:clientID
  • 配置示例:
  1. <feature name="google">
  2. <param name="android_clientID" value="googled0d84bbf23b4a0e4"/>
  3. </feature>
  • 字段描述:

    clientID:(必须配置)该值是由开发者在https://console.developers.google.com 配置生成,填写在对应位置生效;样式如:465993872355-5p3shff1atoavn3d0bejpasknfqu6tm2.apps.googleusercontent.com。

  1. 服务器端验证 token 的方法:https://developers.google.com/identity/sign-in/android/backend-auth

模块接口

showSignInButton

添加显示登录按钮

showSignInButton({params},callback(ret, err))

params

rect:

  • 类型:JSON 对象
  • 描述:(可选项)模块的位置及尺寸
  • 内部字段:
  1. {
  2. x: 0, //(可选项)数字类型;模块左上角的 x 坐标(相对于所属的 Window 或 Frame);默认值:0
  3. y: 0, //(可选项)数字类型;模块左上角的 y 坐标(相对于所属的 Window 或 Frame);默认值:0
  4. w: 80, //(可选项)数字类型;模块的宽度;默认值:80
  5. h: 50 //(可选项)数字类型;模块的高度;默认值:50
  6. }

style:

  • 类型:字符串
  • 描述:(可选项)按钮类型
  • 默认:standard
  • 取值范围:
    • standard:
    • iconOnly:
    • wide:

colorScheme:

  • 类型:字符串
  • 描述:(可选项)按钮颜色
  • 默认:dark
  • 取值范围:
    • dark:
    • light:

fixedOn:

  • 类型:字符串
  • 描述:(可选项)模块视图添加到指定 frame 的名字(只指 frame,传 window 无效)
  • 默认:模块依附于当前 window

fixed:

  • 类型:布尔
  • 描述:(可选项)是否将模块视图固定到窗口上(不跟随窗口上下滚动)
  • 默认值:true

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. eventType: 'show', //字符串类型;交互事件类型,取值如下:
  3. //show:按钮打开并显示
  4. //didSignIn:登录成功
  5. //err:失败
  6. userInfo: { //JSON对象;用户信息
  7. userID: '', //字符串类型;用户id
  8. authentication: { //JSON对象;用户授权信息
  9. idToken: '', //字符串类型;
  10. idtExpiration:'', //字符串类型;idToken过期时间 【注意】:android端不支持该参数
  11. clientID: '', //字符串类型;
  12. accessToken:'', //字符串类型;【注意】:android端不支持该参数
  13. actExpiration:'', //字符串类型;accessToken过期时间 【注意】:android端不支持该参数
  14. refreshToken:'' //字符串类型;
  15. },
  16. profile: { //JSON对象;账号信息
  17. name: '', //字符串类型;
  18. givenName: '', //字符串类型;
  19. familyName: '', //字符串类型;
  20. email: '', //字符串类型;
  21. hasImage: //布尔类型;用户是否有照片
  22. },
  23. accessibleScopes:[] //数组类型;The API scopes requested by the app in an array of string
  24. }
  25. }

err:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. code: 0, //数字类型;错误码
  3. msg: '' //字符串类型;错误信息
  4. }

示例代码

  1. var google = api.require('google');
  2. google.showSignInButton({
  3. rect:{
  4. x:100,
  5. y:64,
  6. w:120,
  7. h:45
  8. },
  9. style: 'standard',
  10. colorScheme: 'light',
  11. fixedOn: api.frameName,
  12. fixed: true
  13. },function(ret, err) {
  14. if (err) {
  15. api.alert({msg:JSON.stringify(err)});
  16. } else {
  17. api.alert({msg:JSON.stringify(ret)});
  18. }
  19. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

getProfileImage

获取用户照片的 URL

getProfileImage({params}, callback(ret))

params

dimension:

  • 类型:数字
  • 描述:(可选项)照片的大小 【注意】:android端不支持该参数
  • 默认:100

callback(ret)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. imageUrl: '' //字符串类型;图片的地址
  3. }

示例代码

  1. var google = api.require('google');
  2. google.getProfileImage({
  3. dimension: 200
  4. }, function(ret) {
  5. api.alert({msg:ret.imageUrl});
  6. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

hasAuth

判断是否已登录

hasAuth(callback(ret))

callback(ret)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. status: true //布尔型;是否已登录,true||false
  3. }

示例代码

  1. var google = api.require('google');
  2. google.hasAuth(function(ret, err) {
  3. if (ret.status) {
  4. alert('已登录');
  5. } else {
  6. alert('未登录');
  7. }
  8. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

signInSilently

Attempts to sign in a previously authenticated user without interaction. The showSignInButton’s callback will be called at the end of this process indicating success or failure.

signInSilently(##callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. eventType: 'show', //字符串类型;交互事件类型,取值如下:
  3. //show:按钮打开并显示
  4. //didSignIn:登录成功
  5. //err:失败
  6. userInfo: { //JSON对象;用户信息
  7. userID: '', //字符串类型;用户id
  8. authentication: { //JSON对象;用户授权信息
  9. idToken: '', //字符串类型;
  10. idtExpiration:'', //字符串类型;idToken过期时间 【注意】:android端不支持该参数
  11. clientID: '', //字符串类型;
  12. accessToken:'', //字符串类型;【注意】:android端不支持该参数
  13. actExpiration:'', //字符串类型;accessToken过期时间 【注意】:android端不支持该参数
  14. refreshToken:'' //字符串类型;
  15. },
  16. profile: { //JSON对象;账号信息
  17. name: '', //字符串类型;
  18. givenName: '', //字符串类型;
  19. familyName: '', //字符串类型;
  20. email: '', //字符串类型;
  21. hasImage: //布尔类型;用户是否有照片
  22. },
  23. accessibleScopes:[] //数组类型;The API scopes requested by the app in an array of string
  24. }
  25. }

err:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. code: 0, //数字类型;错误码
  3. msg: '' //字符串类型;错误信息
  4. }

示例代码

  1. var google = api.require('google');
  2. google.signInSilently(function(ret,err){
  3. api.alert({msg:JSON.stringify(ret)});
  4. });

补充说明

可在 setScopes 接口后调用

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

getScopes

The API scopes requested by the app in an array of String

getScopes({params}, callback(ret))

callback(ret)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. scopes: [] //数组类型;
  3. }

示例代码

  1. var google = api.require('google');
  2. google.getScopes(function(ret, err) {
  3. if (ret) {
  4. api.alert({msg:JSON.stringify(ret)});
  5. }
  6. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

setScopes

设置权限范围 。

  • IOS调用本接口后调用 signInSilently;
  • android需要重新登陆后设的值才会生效

setScopes({params})

params

scopes:

“;

示例代码

  1. var google = api.require('google');
  2. google.setScopes({
  3. scopes: ''
  4. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

signOut

登出

signOut()

示例代码

  1. var google = api.require('google');
  2. google.signOut();

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

disconnect

Disconnects the current user from the app and revokes previous authentication. If the operation succeeds, the OAuth 2.0 token is also removed from module.

disconnect()

示例代码

  1. var google = api.require('google');
  2. google.disconnect();

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

getTokens

获取授权 token(需要登录授权成功

getTokens(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. status: true, //布尔型;true||false
  3. authentication: { //JSON对象;用户授权信息
  4. idToken: '', //字符串类型;
  5. idtExpiration:'', //字符串类型;idToken过期时间 【注意】:android端不支持该参数
  6. clientID: '', //字符串类型;
  7. accessToken:'', //字符串类型;【注意】:android端不支持该参数
  8. actExpiration:'', //字符串类型;accessToken过期时间 【注意】:android端不支持该参数
  9. refreshToken:'' //字符串类型;
  10. }
  11. }

err:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. code: 0, //数字类型;错误码
  3. msg: '' //字符串类型;错误信息
  4. }

示例代码

  1. var google = api.require('google');
  2. google.getTokens(function(ret, err) {
  3. if (ret.status) {
  4. alert(JSON.stringify(ret));
  5. } else {
  6. alert(err.code);
  7. }
  8. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

refreshTokens

刷新token

refreshTokens(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. status: true, //布尔型;true||false
  3. authentication: { //JSON对象;用户授权信息
  4. idToken: '', //字符串类型;
  5. idtExpiration:'', //字符串类型;idToken过期时间 【注意】:android端不支持该参数
  6. clientID: '', //字符串类型;
  7. accessToken:'', //字符串类型;【注意】:android端不支持该参数
  8. actExpiration:'', //字符串类型;accessToken过期时间 【注意】:android端不支持该参数
  9. refreshToken:'' //字符串类型;
  10. }
  11. }

err:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. code: 0, //数字类型;错误码
  3. msg: '' //字符串类型;错误信息
  4. }

示例代码

  1. var google = api.require('google');
  2. google.refreshTokens(function(ret, err) {
  3. if (ret.status) {
  4. alert(JSON.stringify(ret));
  5. } else {
  6. alert(err.code);
  7. }
  8. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本

signIn

直接登录

signIn(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. eventType: 'show', //字符串类型;交互事件类型,取值如下:
  3. //show:按钮打开并显示
  4. //didSignIn:登录成功
  5. //err:失败
  6. userInfo: { //JSON对象;用户信息
  7. userID: '', //字符串类型;用户id
  8. authentication: { //JSON对象;用户授权信息
  9. idToken: '', //字符串类型;
  10. idtExpiration:'', //字符串类型;idToken过期时间 【注意】:android端不支持该参数
  11. clientID: '', //字符串类型;
  12. accessToken:'', //字符串类型;【注意】:android端不支持该参数
  13. actExpiration:'', //字符串类型;accessToken过期时间 【注意】:android端不支持该参数
  14. refreshToken:'' //字符串类型;
  15. },
  16. profile: { //JSON对象;账号信息
  17. name: '', //字符串类型;
  18. givenName: '', //字符串类型;
  19. familyName: '', //字符串类型;
  20. email: '', //字符串类型;
  21. hasImage: //布尔类型;用户是否有照片
  22. },
  23. accessibleScopes:[] //数组类型;The API scopes requested by the app in an array of string
  24. }
  25. }

err:

  • 类型:JSON 对象
  • 内部字段:
  1. {
  2. code: 0, //数字类型;错误码
  3. msg: '' //字符串类型;错误信息
  4. }

示例代码

  1. var google = api.require('google');
  2. google.signIn(function(ret, err) {
  3. if (err) {
  4. api.alert({msg:JSON.stringify(err)});
  5. } else {
  6. api.alert({msg:JSON.stringify(ret)});
  7. }
  8. });

可用性

iOS系统,Android系统

可提供的1.0.0及更高版本