目录

获取用户标识

功能介绍

使用限制

当前接口仅支持Android游戏调用,不支持HarmonyOS游戏调用。

场景描述

应用已经获取到Access Token,要对其进行解析鉴权,通过Access Token到华为服务器上获取用户的unionId、openId、expire等信息。

接口原型

承载协议

HTTPS POST

接口方向

开发者服务器->华为游戏服务器

接口URL

https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi

注意:
  • 此接口当前不支持SSL双向认证,在发送HTTPS请求时需要先关闭SSL认证。
  • 请使用TLS 1.2协议或以上版本。

数据格式

请求:Content-Type: application/x-www-form-urlencoded(表单方式)

响应:Content-Type: application/json

请求参数

参数

是否必选

参数类型

描述

method

是

String

固定传入“external.hms.gs.getTokenInfo”。

accessToken

是

String

取自通过AuthorizationCode获取Access Token中获取到的Access Token。

说明:

该参数需经过UrlEncode处理后,再添加进请求消息中。

reqPlayerId

否

Integer

是否需要获取playerId。

  • 0:不获取,不传值时默认为0
  • 1:获取
注意:

仅当进行playerId替换为openId时需要获取playerId,新游戏接入时不需要获取。

请求示例

POST /gameservice/api/gbClientApi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: PostmanRuntime/7.24.0
Accept: */*
Host: jos-open-api.cloud.huawei.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 717
method=external.hms.gs.getTokenInfo&accessToken=******&reqPlayerId=1

响应参数

参数

是否必选

参数类型

描述

rtnCode

是

int

服务端结果说明:

  • 0:成功
  • -1:accessToken获取失败
  • -2:playerId获取失败
  • 2:accessToken鉴权失败

unionId

否

String

玩家的unionId。

openId

否

String

玩家的openId。

expire

否

int

accessToken的剩余过期时间,单位:秒。

clientId

否

String

应用的AppID。

playerId

否

String

玩家的playerId。

响应示例

HTTP/1.1 200 OK
Date: Tue, 19 May 2020 06:28:02 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip
Server: elb

{"rtnCode":0,"unionId":"*****","openId":"*****","expire":3600,"clientId":"*****","playerId":"*****"}

调用示例

[h2]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.getTokenInfo"; // 固定传入
        String accessToken= "xxxx"; // 通过AuthorizationCode获取的AccessToken 
        Integer reqPlayerId = new Integer(0); // 是否需要获取playerId,0表示不获取,1表示获取
        getTokenInfoService(method, accessToken, reqPlayerId);
    }
    /**
     * 根据AccessToken获取玩家信息
     *
     * @param method 固定传入“external.hms.gs.getTokenInfo”
     * @param accessToken 客户端Player对象中的AccessToken
     * @param reqPlayerId 是否需要获取playerId,0表示不获取,1表示获取
     */
    private static void getTokenInfoService(String method, String accessToken, Integer reqPlayerId) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        RequestBody mFormBody = new FormBody.Builder().add("method", method)
            .add("accessToken", accessToken)
            .add("reqPlayerId", String.valueOf(reqPlayerId))
            .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"))) {
                    System.out.println("openId: " + object.get("openId"));
                    System.out.println("unionId: " + object.get("unionId"));
                    System.out.println("clientId: " + object.get("clientId"));
                } else {
                    System.out.println("rtnCode: " + object.get("rtnCode"));
                    System.out.println("rtnMsg: " + object.get("errMsg"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

[h2]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.getTokenInfo”
            string method = "external.hms.gs.getTokenInfo";
            // 通过AuthorizationCode获取的AccessToken。
            string accessToken = "xxxxx";
            // 是否需要获取playerId。0表示不获取,1表示获取
            int reqPlayerId = 0;
            // 请求接口
            requestgameInfo(method, accessToken, reqPlayerId);
        }
        static void requestgameInfo(string method, string accessToken, int reqPlayerId) 
        {
            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));
            data.Append("&reqPlayerId=" + HttpUtility.UrlEncode(reqPlayerId.ToString()));
            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();
        }
    }
}

[h2]PHP

class get_token_info
{
    /**
     * 根据AccessToken获取玩家信息
     *
     * @param string $method 固定传入“external.hms.gs.getTokenInfo”
     * @param string $accessToken 请使用通过AuthorizationCode获取的AccessToken
     * @param int $reqPlayerId 是否需要获取playerId,0表示不获取,1表示获取
     */
    public function call_https(string $method, string $accessToken, int $reqPlayerId): void
    {
        $data = array("method" => $method, "accessToken" => $accessToken, "reqPlayerId" => $reqPlayerId);
        $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.getTokenInfo"; // 固定传入
$accessToken = "xxxxx"; // 请使用客户端Player对象中的AccessToken
$reqPlayerId = 0; // 是否需要获取playerId,0表示不获取,1表示获取
$get_token_info->call_https($method, $accessToken, $reqPlayerId);

[h2]Python

from typing import Any
import requests
import urllib
class GetTokenInfoSolution:
    def get_token_info(self, method, accessToken, reqPlayerId):
        url = "https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi"
        params: dict[str, Any] = {
            'method': method,
            'accessToken': accessToken,
            'reqPlayerId': reqPlayerId
        }
        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.getTokenInfo”
    input_method = 'external.hms.gs.getTokenInfo'
    # 请使用通过AuthorizationCode获取的AccessToken
    input_accessToken = 'xxx'
    # 是否需要获取playerId,0表示不获取,1表示获取
    input_reqPlayerId = 0
    function = GetTokenInfoSolution()
    function.get_token_info(input_method, input_accessToken, input_reqPlayerId)