爱游戏开放平台的数字签名级别分三种,安全级别由低到高依次为:无签名;基本签名;业务签名。每个接口都会根据数据的安全要求,对数字签名级别进行限制,在服务请求过程中,如果签名的级别与要求的不一致,或者签名信息与请求参数不匹配,则会被认定为非法请求并阻止调用。所以请您在开发过程中,确保每个接口的签名都与接口所要求的级别、算法一致。
1、基本签名
基本签名只对固定的基本参数进行签名,是CP身份的证明。必须进行签名的字段:统一为client_id、sign_method、version、timestamp、client_secret
具体算法如下:
在调用接口时,如果需要使用基本签名,需要在https请求参数中加入如下参数, 且参数名必须保持一致:client_id、sign_method、version、timestamp、sign_sort、signature。
client_id: 应用向开放平台注册时分配的应用编号
sign_method: 数字签名的编码算法,如MD5,HmacSha1,HmacMD5等,SDK1.0版本暂时只支持MD5算法
version: 所使用的SDK版本,目前版本为1.0
timestamp: 请求发起时的时间戳,如:1385345938378
sign_sort:用于指定请求参数在进行数字签名时的排列顺序。把client_id、sign_method、version、timestamp、client_secret的参数名,按照任意顺序排列,然后拼接起来,参数名之间以“&”符号连接。例如:sign_sort=client_id&sign_method&
version×tamp&client_secret
signature:数字签名。计算步骤如下:
示例:某应用(client=1001;client_secret=a1b2c3),请求某接口
业务参数:token= aaaaaaaa,
签名级别:基本签名
则加入签名信息后最终请求参数如下:
token=aaaaaaaa
client_id=1001
sign_method=MD5
version=1.0
timestamp=1385345938378
sign_sort= client_id&version&sign_method&client_secret×tamp
signature= bytesToHexString (MD5.encode(“10011.0MD5a1b2c31385345938378”))
2、业务签名
业务签名需要对固定的基本参数和接口业务参数进行签名。不仅能验证CP身份,而且能够提高数据安全性,防止在请求过程中业务参数被第三方篡改。所以采用业务签名的接口,会在接口清单中明确约定需要签名的字段。必须进行签名的字段:具体接口指定。
具体算法如下:
在调用接口时,如果需要使用基本签名,需要在https请求参数中加入如下参数, 且参数名必须保持一致:client_id、sign_method、version、timestamp、sign_sort、signature
client_id: 应用向开放平台注册时分配的应用编号
sign_method: 数字签名的编码算法,如MD5,HmacSha1,HmacMD5等,SDK1.0版本暂
时只支持MD5算法
version: 所使用的SDK版本,目前版本为1.0
timestamp: 请求发起时的时间戳,如:1385345938378
sign_sort:用于指定请求参数在进行数字签名时的排列顺序。把接口要求进行签名的参数,按照任意顺序排列,然后拼接起来,参数名之间以“&”符号连接。例如:某接口约定使用业务签名,签名字段为client_id、sign_method、version、timestamp、client_secret、username、password,则可以使sign_sort=password&username&client_secret×tamp&version&
sign_method&client_id
signature:数字签名。计算步骤如下:
示例:某应用(client=12;client_secret=cs),请求某接口
业务参数:username=open; password=123; imsi=189;
签名级别:业务签名
签名字段:client_id、sign_method、version、timestamp、client_secret、username、password、imsi
则加入签名信息后最终请求参数如下:
username=open
password=123
imsi=189
client_id=12
sign_method=MD5
version=1.0
timestamp=1385345938378
sign_sort=client_id&sign_method&version×tamp&client_secret&username&password&imsi
signature=bytesToHexString (MD5.encode(“12MD51.01385345938378csopen123189”))
登陆流程:
1.用户触发登陆功能
2.判断用户是否满足自动登陆条件,如果不满足,引导用户进入登陆界面。
3.用户身份通过验证后,爱游戏服务端会生成待登陆应用的授权码,并把请求重定向至“CP服务器端授权码兑换授权令牌”接口。(该接口CP服务器端必须实现,并在客户端触发SDK用户登陆功能时作为重定向地址参数传入,详见3.2.2.1)
4.CP服务器端调用爱游戏服务器端“授权码兑换token”接口(详见3.2.1.1)
5.爱游戏服务器端校验授权码通过后,生成授权令牌并返回给CP服务器端。
6.CP服务器端收到令牌后,确认用户信息,并进行自己的业务逻辑(如绑定账号等)。然后把令牌信息返回给爱游戏SDK。
备注:出于安全考虑,CP端的接口建议用https协议。
3.1.1授权码兑换令牌
服务地址:https://open.play.cn /oauth/token
签名级别:2
提交方式:GET/POST
功能说明:获取访令牌
请求参数:
编号 | 参数名 | 必填 | 类型/长度 | 备注 |
---|---|---|---|---|
1 | client_id | yes | String | 对应于应用注册时返回的client_id |
2 | client_secret | yes | String | 对应于应用注册时返回的client_secret |
3 | code | yes | String | 用户授权凭证 |
4 | grant_type | yes | String | 此值固定为authorization_code |
5 | redirect_uri | yes | String | 此值仅用来做校验,不做转向, 必须和应用注册时填写的转向URI域名一致 |
6 | scope | no | String | 访问请求的作用域 |
7 | state | no | String | 维护请求和响应的状态,传入值与返回值保持一致 |
返回结果:
编号 | 参数名 | 类型/长度 | 备注 |
---|---|---|---|
1 | access_token | String | 访问令牌 |
2 | token_type | String | 令牌类型,目前只有“Bearer” |
3 | refresh_token | String | 刷新令牌 |
4 | expires_in | String | 访问令牌过期时间 |
5 | re_expires_in | String | 刷新令牌过期时间 |
6 | scope | String | 授权范围 |
7 | user_id | String | 爱游戏用户Id |
可能的错误码:
错误码 | 错误描述 |
---|---|
access_denied | 请求被拒绝 |
unsupported_response_type | 非法的response_type |
invalid_scope | 非法的权限范围 |
invalid_grant | 非法的grant_type类型 |
expired_token | 令牌过期 |
insufficient_scope | 超出授权范围 |
scope limits | 不合法授权范围 |
app status is invalid | 应用状态不可用 |
appkey is overdue | appkey 超出有效时间 |
app is overdue | 应用超出有效时间 |
authorize_code is overdue | authorize_code超出有效时间 |
access_token is overdue | access_token超出有效时间 |
refresh_token is overdue | refresh_token超出有效时间 |
request method must be get | 请求方式必须为get |
request method must be post | 请求方式必须为post |
client_id is empty | client_id 为空 |
response_type is empty | response_type 为空 |
redirect_uri is empty | redirect_uri为空 |
grant type is empty | grant_type为空 |
authorize code is empty | 授权码为空 |
client_secret is invalidate | 应用的client_secret无效 |
app business exception occured | CP私有业务处理异常 |
结果示例
成功响应:
{
"scope": "all",
"re_expires_in": 15552000,
"user_id": 956877,
"token_type": "Bearer",
"expires_in": 5184000,
"refresh_token": "2c639e8c1cbfeee5fb07e968163d0343",
"access_token": "2cd0a6f9c8ce81ada335f1989413ca08"
}
错误响应:
{
"error_uri":null,
"error":"invalid_request",
"state":null,
"error_description":"Missing grant_type parameter value"
}
3.1.2访问令牌鉴权
服务地址:https://open.play.cn/oauth/token/validator
签名级别:2
提交方式:GET/POST
功能说明:访问令牌鉴权(此接口主要用于鉴定用户身份,当应用客户端需要操作用户针对于应用的数据时,如游戏道具,游戏金币等,应用服务器可以调用此接口鉴定用户身份,从而确定用户是否有权限操作相关数据)
请求参数:
编号 | 参数名 | 必填 | 类型/长度 | 备注 |
---|---|---|---|---|
1 | client_id | yes | String | 对应于应用注册时返回的client_id |
2 | access_token | yes | String | 访问令牌 |
返回结果:
编号 | 参数名 | 类型 | 备注 |
---|---|---|---|
1 | code | String | 返回码,0表示成功 |
2 | text | String | 返回码描述信息 |
3 | ext | String | 业务数据 |
4 | ext->user_id | String | 爱游戏用户编码 |
5 | ext->access_token | String | 访问令牌 |
6 | ext->expires_in | String | 令牌有效时间 |
可能的错误码:
-5,-260,-261,-262,-263,-264,-265
结果示例
成功响应:
{
"ext": {
"access_tpken": "6e87e2dd56dd92a9e5d0322ec9ecbaca",
"expires_in": 4945807,
"user_id": 317506
},
"text": "success"
}
错误响应:
{
"code":-261,
"text":"ErrorCode:-261 / Message:access_Token is invalid",
"ext":null
}
3.2.1授权码兑换授权令牌(重要)
服务地址:CP自定义(建议使用https协议)
提交方式:GET/POST
功能说明:授权码兑换应授权令牌(如果您的应用需要进行服务器端对接,此接口必须实现,并在客户端代码调用SDK登陆功能时,作为重定向地址参数传入,不然将无法实现登陆功能)
请求参数:CP自定义,但是必须包含以下参数
编号 | 参数名 | 必填 | 类型/长度 | 备注 |
---|---|---|---|---|
1 | code | yes | String | 用户授权凭码 |
返回结果:
编号 | 参数名 | 类型 | 备注 |
---|---|---|---|
1 | access_token | String | 访问令牌 |
2 | token_type | String | 令牌类型 |
3 | refresh_token | String | 刷新令牌 |
4 | expires_in | String | 访问令牌过期时间 |
5 | re_expires_in | String | 刷新令牌过期时间 |
6 | scope | String | 授权范围 |
7 | user_id | String | 爱游戏用户Id |
结果示例
成功响应:
{
"scope": "all",
"re_expires_in": 15552000,
"user_id": 956877,
"token_type": "Bearer",
"expires_in": 5184000,
"refresh_token": "2c639e8c1cbfeee5fb07e968163d0343",
"access_token": "2cd0a6f9c8ce81ada335f1989413ca08"
}
错误响应:
{
"error_code": "-5",
"error_description": "ErrorCode:-5 / Message:authorize_code is invalid"
}
备注:此接口可以在调用“爱游戏授权码兑换令牌接口”后,做一些CP私有业务逻辑(如绑定账号信息等),然后直接把“爱游戏接口的返回结果”返回给SDK,参见demo
错误代码 | 错误信息 |
---|---|
-5 | 请求参数异常 |
-260 | oauth 访问令牌accessToken过期 |
-261 | oauth 访问令牌accessToken无效 |
-262 | oauth 访问令牌accessToken失效 |
-263 | 应用授权过期 |
-264 | 应用访问受限 |
-265 | 应用不存在 |
签名级别代码 | 含义 |
---|---|
1 | 无签名 |
2 | 基本签名 |
3 | 业务签名 |