承载协议:HTTPS POST
接口方向:开发者服务器->华为游戏服务器
接口URL:https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi
请使用TLS 1.2协议或以上版本。
数据格式:
| 参数 | 是否必选 | 类型 | 描述 |
|---|---|---|---|
| method | 是 | String | 固定传入“external.hms.gs.getPlayerInfo”。 |
| accessToken | 是 | String | 游戏调用华为账号的获取用户级凭证接口获取到的Access Token。 |
POST /gameservice/api/gbClientApi HTTP/1.1 Content-Type: application/x-www-form-urlencoded User-Agent: PostmanRuntime/7.24.0 Host: jos-open-api.cloud.huawei.com Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 717 // 所有请求参数值均需要urlencode编码后再进行拼接 method=external.hms.gs.getPlayerInfo&accessToken=******
| 参数 | 是否必选 | 类型 | 描述 |
|---|---|---|---|
| rtnCode | 是 | int | 服务端结果说明: 0:获取成功 -1:获取失败 2:accessToken无效 3001:参数错误 |
| acctInfo | 否 | LoginAcctInfo | 玩家标识信息,获取成功时返回。 |
| bindInfo | 是 | BindPlayerInfo | 与华为PlayerId绑定的游戏官方账号信息。 |
| errMsg | 否 | String | 异常场景下返回错误码的描述。 |
LoginAcctInfo
| 参数 | 是否必选 | 类型 | 描述 |
|---|---|---|---|
| gamePlayerAcct | 否 | PlayerAcct | gamePlayerId信息。 互通场景默认返回gamePlayerId。 |
| teamPlayerAcct | 否 | PlayerAcct | teamPlayerId信息。 绑定场景默认返回teamPlayerId。 |
| 参数 | 是否必选 | 类型 | 描述 |
|---|---|---|---|
| id | 是 | String | 玩家标识ID。 |
| compatibleType | 是 | int | 兼容类型。 0:gamePlayerId与openId、playerId不兼容,或teamPlayerId与unionId不兼容。 1:gamePlayerId兼容playerId,即HarmonyOS 4及以下游戏使用playerId作为玩家标识,HarmonyOS 5.0及以上游戏将playerId作为gamePlayerId。 2:gamePlayerId兼容openId,即HarmonyOS 4及以下游戏使用openId作为玩家标识,HarmonyOS 5.0及以上游戏将openId作为gamePlayerId。 4:teamPlayerId兼容unionId,即HarmonyOS 5.0及以上游戏将unionId作为teamPlayerId。 |
| idType | 是 | int | 玩家标识ID类型。 1:gamePlayerId 2:teamPlayerId |
| bindType | 是 | int | 是否可绑定游戏官方账号。 0:不可绑定 1:可绑定 |
BindPlayerInfo
| 参数 | 是否必选 | 类型 | 描述 |
|---|---|---|---|
| bindMode | 否 | int | 绑定模式: 0:开发者级别。 1:游戏级别。 |
| thirdOpenId | 否 | String | 游戏官方账号ID。 |
HTTP/1.1 200 OK
Date: Tue, 19 May 2023 06:28:02 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip
Server: elb
{
"acctInfo": {
"gamePlayerAcct": {
"id": "B7******5E",
"compatibleType": 0,
"idType": 1,
"bindType": 0
}
},
"bindInfo": {
"bindMode": 0,
"thirdOpenId": "0234***56895"
},
"rtnCode": 0
} Java
package okhttp.com.post;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import java.io.IOException;
public class GetTokenInfoTest {
private static Integer RETURN_CODE_SUCCEED = 0;
/**
* 接口本地调测使用
*/
public static void main(String[] args) {
String method = "external.hms.gs.getPlayerInfo"; // 固定传入
String accessToken= "xxxx"; // 请使用游戏客户端调用账号获取到的AccessToken
getPlayerInfoService(method, accessToken);
}
/**
* 根据AccessToken获取玩家标识信息
*
* @param method 固定传入“external.hms.gs.getPlayerInfo”
* @param accessToken 游戏客户端调用账号获取到的AccessToken
*/
private static void getPlayerInfoService(String method, String accessToken) {
OkHttpClient client = new OkHttpClient().newBuilder().build();
RequestBody mFormBody = new FormBody.Builder().add("method", method)
.add("accessToken", accessToken)
.build();
Request request = new Request.Builder().url("https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi")
.post(mFormBody)
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
JSONObject object = JSONObject.parseObject(response.body().string());
if (RETURN_CODE_SUCCEED.equals(object.get("rtnCode")) && object.get("acctInfo") != null) {
JSONObject acctInfo = JSONObject.parseObject(object.get("acctInfo").toString());
if (acctInfo != null){
if (acctInfo.get("gamePlayerAcct") != null) {
JSONObject gamePlayerAcct = JSONObject.parseObject(acctInfo.get("gamePlayerAcct").toString());
System.out.println("gamePlayerId: " + gamePlayerAcct.get("id"));
System.out.println("gamePlayer compatibleType: " + gamePlayerAcct.get("compatibleType"));
}
if (acctInfo.get("teamPlayerAcct") != null) {
JSONObject teamPlayerAcct = JSONObject.parseObject(acctInfo.get("teamPlayerAcct").toString());
System.out.println("teamPlayerId: " + teamPlayerAcct.get("id"));
System.out.println("teamPlayer compatibleType: " + teamPlayerAcct.get("compatibleType"));
}
}
JSONObject bindInfo = JSONObject.parseObject(object.get("bindInfo").toString());
if (bindInfo != null) {
System.out.println("bindMode: " + bindInfo.get("bindMode"));
System.out.println("thirdOpenId: " + bindInfo.get("thirdOpenId"));
}
} else {
System.out.println("rtnCode: " + object.get("rtnCode"));
System.out.println("rtnMsg: " + object.get("errMsg"));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} C#
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace cXdemo
{
class Program
{
static void Main(string[] args)
{
// 固定传入“external.hms.gs.getPlayerInfo”
string method = "external.hms.gs.getPlayerInfo";
// 取自当前玩家的Player对象中获取到的Access Token
string accessToken = "xxxxx";
// 请求接口
requestgameInfo(method, accessToken);
}
static void requestgameInfo(string method, string accessToken)
{
var requestUrl = "https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi";
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
StringBuilder data = new StringBuilder();
data.Append("method=" + HttpUtility.UrlEncode(method));
data.Append("&accessToken=" + HttpUtility.UrlEncode(accessToken));
byte[] byteData = Encoding.UTF8.GetBytes(data.ToString());
request.ContentLength = byteData.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(byteData, 0, byteData.Length);
postStream.Close();
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string strJson = reader.ReadToEnd();
Console.WriteLine(strJson);
reader.Close();
response.Close();
}
}
} PHP
class get_token_info
{
/**
* 根据AccessToken获取玩家信息
*
* @param string $method 固定传入“external.hms.gs.getPlayerInfo”
* @param string $accessToken 请使用客户端Player对象中的AccessToken
*/
public function call_https(string $method, string $accessToken): void
{
$data = array("method" => $method, "accessToken" => $accessToken);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
if (curl_error($curl)) {
throw new Exception(curl_error($curl));
}
curl_close($curl);
$result = json_decode($response, true);
var_dump($result);
}
}
$get_token_info = new get_token_info();
$method = "external.hms.gs.getPlayerInfo"; // 固定传入
$accessToken = "xxxxx"; // 请使用客户端Player对象中的AccessToken
$get_token_info->call_https($method, $accessToken); Python
from typing import Any
import requests
import urllib.parse
class GetTokenInfoSolution:
def get_token_info(self, method, accessToken):
url = "https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi"
params: dict[str, Any] = {
'method': method,
'accessToken': accessToken
}
encodedParams = urllib.parse.urlencode(params)
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(url, headers=headers, data=encodedParams)
print(response.text)
if __name__ == "__main__":
# 固定传入“external.hms.gs.getPlayerInfo”
input_method = 'external.hms.gs.getPlayerInfo'
# 请使用客户端Player对象中的AccessToken
input_accessToken = 'xxx'
function = GetTokenInfoSolution()
function.get_token_info(input_method, input_accessToken)