The app has obtained the access token and needs to parse and authenticate it. The app uses the access token to obtain the unionId, openId, and expire information of the user from Huawei servers.
| Protocol | HTTPS POST |
|---|---|
| Direction | Your server -> Huawei game server |
| URL | https://jos-open-api.cloud.huawei.com/gameservice/api/gbClientApi CAUTION:
|
| Data Format | Request: Content-Type: application/x-www-form-urlencoded (form mode) Response: Content-Type: application/json |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| method | Yes | String | This parameter has a fixed value of external.hms.gs.getTokenInfo. |
| accessToken | Yes | String | Access token obtained by referring to Obtaining the Access Token Using the Authorization Code. NOTE: This parameter must be converted through URL encoding before being added to the request. |
| reqPlayerId | No | Integer | Indicates whether to obtain the player ID. Its value is 0 by default.
CAUTION: playerId needs to be obtained when you replace openId with playerId. It is not required for new games. |
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
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| rtnCode | Yes | int | Result from the server:
|
| unionId | No | String | unionId of the player. |
| openId | No | String | openId of the player. |
| expire | No | int | Remaining expiration time of the accessToken, in seconds. |
| clientId | No | String | App ID. |
| playerId | No | String | Player ID. |
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":"*****"} 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;
/**
* Used for local debugging.
*/
public static void main(String[] args) {
String method = "external.hms.gs.getTokenInfo"; // Fixed value.
String accessToken= "xxxx"; // Access token obtained through AuthorizationCode.
Integer reqPlayerId = new Integer(0); // Set whether to obtain the player ID. 0: no; 1: yes
getTokenInfoService(method, accessToken, reqPlayerId);
}
/**
* Obtain the player information based on the access token.
*
* @param method Pass external.hms.gs.getTokenInfo.
* @param accessToken Access token in the Player object of the client.
* @param reqPlayerId Indicates whether to obtain the player ID. 0: no; 1: yes
*/
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();
}
}
} using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace cXdemo
{
class Program
{
static void Main(string[] args)
{
// Pass external.hms.gs.getTokenInfo.
string method = "external.hms.gs.getTokenInfo";
// Access token obtained through AuthorizationCode.
string accessToken = "xxxxx";
// Indicates whether to obtain the player ID. 0: no; 1: yes
int reqPlayerId = 0;
// Request API.
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();
}
}
} class get_token_info
{
/**
* Obtain the player information based on the access token.
*
* @param string $method Pass external.hms.gs.getTokenInfo.
* @param string $accessToken Use the access token obtained through AuthorizationCode.
* @param int $reqPlayerId Indicates whether to obtain the player ID. 0: no; 1: yes
*/
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"; // Fixed value.
$accessToken = "xxxxx"; // Use the access token in the Player object of the client.
$reqPlayerId = 0; // Indicates whether to obtain the player ID. 0: no; 1: yes
$get_token_info->call_https($method, $accessToken, $reqPlayerId); 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__":
# Pass external.hms.gs.getTokenInfo.
input_method = 'external.hms.gs.getTokenInfo'
# Use the access token obtained through AuthorizationCode.
input_accessToken = 'xxx'
# Indicates whether to obtain the player ID. 0: no; 1: yes
input_reqPlayerId = 0
function = GetTokenInfoSolution()
function.get_token_info(input_method, input_accessToken, input_reqPlayerId)