目录

获取玩家标识

功能介绍

场景描述

游戏已经获取到Access Token,要对其进行解析鉴权,通过Access Token到华为服务器上获取玩家的gamePlayerId、teamPlayerId等信息。

接口原型

请求参数

参数 是否必选 类型 描述
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 异常场景下返回错误码的描述。

响应示例

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)