To secure game sign-ins via OAuth 2.0, it is recommended that playerId be replaced with openId gradually. If you just completed step 3 in phase 1 of Solution to Replace PlayerIDs with OpenIDs, you can call this API to query OpenIDs corresponding to player IDs in batches for establishing mappings.
Protocol | HTTPS POST |
|---|---|
Direction | Your server -> Huawei server |
URL | Select the URL of the site nearest to the location where your server is deployed. China: https://connect-api.cloud.huawei.com/api/jas/user/player/convert/openid Europe: https://connect-api-dre.cloud.huawei.com/api/jas/user/player/convert/openid Asia Pacific: https://connect-api-dra.cloud.huawei.com/api/jas/user/player/convert/openid Russia: https://connect-api-drru.cloud.huawei.com/api/jas/user/player/convert/openid CAUTION: When you call the API for obtaining a token, the domain name must be the same as that of this API. For example, if the domain name is connect-api-dre.cloud.huawei.com for this API, the URL https://connect-api-dre.cloud.huawei.com/api/oauth2/v1/token must be used to call the API for obtaining a token. |
Data Format | Request: Content-Type: application/json Response: Content-Type: application/json |
Parameter | Mandatory | Type | Description |
|---|---|---|---|
client_id | Yes | String | |
Authorization | Yes | String | Authentication information in Authorization: Bearer ${access_token} format. In the format, access_token is obtained in Obtaining a Token. |
The request body carries related information in JSON format. The following table describes the parameters.
Parameter | Mandatory | Type | Description |
|---|---|---|---|
playerIds | Yes | List<String> | Player ID list that needs to be queried. If the query request contains duplicate player IDs, this ID is returned only once. CAUTION: The value of playerId cannot contain 50 IDs. If so, error code 3001 is returned. For better API performance, it is recommended that no more than 20 player IDs be passed at a time. |
appId | Yes | String |
POST HTTP/1.1
Host: connect-api.cloud.huawei.com
client_id: 41******7168
Content-Type: application/json
Authorization: Bearer ******
{
"playerIds": [
"****",
"****"
],
"appId": "*****"
} Parameter | Mandatory | Type | Description |
|---|---|---|---|
ret | Yes | JSON string containing the result code and description in {"code":retcode, "msg": "description"} format, in which retcode indicates the result code and description indicates the result code description. | |
data | Yes | List<ConvertOpenIdItem> | Query results. |
JosRet
Parameter | Mandatory | Type | Description |
|---|---|---|---|
code | Yes | Integer | Return code.
|
msg | No | String | Error description when the operation fails. |
ConvertOpenIdItem
Parameter | Mandatory | Type | Description |
|---|---|---|---|
playerId | Yes | String | Player ID to be queried. |
openId | No | String | OpenID corresponding to player ID. This parameter is returned when the player ID is valid. |
errCode | Yes | Integer | Query result.
|
{
"ret": {
"code": 0
},
"data": [
{
"playerId": "*******",
"openId":"*******",
"errCode": 0
},
{
"playerId": "*******",
"errCode": -1
}
]
} /**
* Obtain OpenIDs in batches.
*
* @param domain Select the URL of the nearest site based on the server deployment location. The URL must be the same as the domain name used for obtaining the token.
* @param client_id Client ID used for authentication.
* @param token Token used for a POST authentication request.
* @param playerIds Player ID list to be queried.
* @param appId App ID.
*/
private static void batchGetOpenIds(String domain, String client_id, String token, List<String> playerIds,
String appId) {
try {
// API URL. Select the URL of the nearest site based on the server deployment location and ensure that the URL is the same as the domain name used for obtaining the token.
HttpPost post = new HttpPost(domain + "/api/jas/user/player/convert/openid");
// Construct a message header.
post.setHeader("Authorization", "Bearer " + token);
post.setHeader("client_id", client_id);
// Construct a message entity.
JSONObject keyString = new JSONObject();
keyString.put("playerIds", playerIds);
keyString.put("appId", appId);
StringEntity entity = new StringEntity(keyString.toString(), Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
// Send a data request in JSON format.
entity.setContentType("application/json");
post.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), Consts.UTF_8));
String result = br.readLine();
System.out.println(result);
JSONObject object = JSON.parseObject(result);
}
post.releaseConnection();
httpClient.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
} /**
* Obtain OpenIDs in batches.
*
* @param domain Select the URL of the nearest site based on the server deployment location. The URL must be the same as the domain name used for obtaining the token.
* @param clientId Client ID used for authentication.
* @param token Token used for a POST authentication request.
* @param playerIds Player ID list to be queried.
* @param appId App ID.
*/
static void RequestOpenId(string domain, string clientId, string token, List<string> playerIds, string appId)
{
try
{
// API URL. Select the URL of the nearest site based on the server deployment location and ensure that the URL is the same as the domain name used for obtaining the token.
var requestUrl = domain + "/api/jas/user/player/convert/openid";
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "post";
request.ContentType = "application/json";
request.Headers.Add("client_id", clientId);
request.Headers.Add("Authorization", "Bearer " + token);
Dictionary<string, object> dic = new Dictionary<string, object>
{
{"appId", appId},
{"playerIds", playerIds}
};
string sendData = JsonConvert.SerializeObject(dic, Formatting.Indented);
Console.WriteLine(sendData);
byte[] byteData = Encoding.GetEncoding("utf-8").GetBytes(sendData);
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();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
} /**
* Obtain OpenIDs in batches.
*
* @param string $domain Select the URL of the nearest site based on the server deployment location. The URL must be the same as the domain name used for obtaining the token.
* @param string $client_id Client ID used for authentication.
* @param string $token Token used for a POST authentication request.
* @param array $playerIds Player ID list to be queried.
* @param string $appId App ID.
* @throws Exception
*/
public function batch_get_openIds(string $domain, string $client_id, string $token, array $playerIds, string $appId)
{
$curl = curl_init();
$data = array("playerIds" => $playerIds, "appId" => $appId);
curl_setopt_array($curl, array(
CURLOPT_URL => $domain . '/api/jas/user/player/convert/openid',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
'client_id: ' . $client_id,
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
),
));
$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);
} from getToken import *
class BatchGetOpenIdsSolution:
@staticmethod
def batch_get_open_ids(domain, client_id, access_token, player_Id, app_id):
url = domain + '/api/jas/user/player/convert/openid'
data = {"playerIds": player_Id, "appId": app_id}
payload = json.dumps(data)
headers = {
'client_id': client_id,
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
if __name__ == "__main__":
# Select the URL of the nearest site based on your server deployment location. A domain name of the China site is used as an example here.
input_domain = 'https://connect-api.cloud.huawei.com'
# Client ID.
input_client_id = 'xxxxxxx'
# Client secret.
input_client_secret = 'xxxxxxxx'
# Obtain a token.
token_function = GetToken()
input_access_token = token_function.get_token(input_domain, input_client_id, input_client_secret)
# App ID.
input_app_id = 'xxxxx'
# Player ID list to be queried. A maximum of 50 player IDs can be queried at a time. The following example uses two player IDs.
input_player_Ids = ['xxxxxxx', 'xxxxxx']
batch_function = BatchGetOpenIdsSolution()
batch_function.batch_get_open_ids(input_domain, input_client_id, input_access_token, input_player_Ids, input_app_id)