Location>code7788 >text

Easily build game login capabilities to create a smooth player experience

Popularity:568 ℃/2024-10-12 15:19:28

Game login is an important step for players to enter the game world, and it is the first interface that players can interact with after entering the game, which seems to be simple but is of great significance. Game login is not only a necessary step to enter the game, but also a key link to ensure game experience, data security and community interaction.

HarmonyOS SDKgaming service(The Game Service Kit (GSK) mainly provides services for building basic game capabilities and optimizing game scenarios in a fast and low-cost manner, which effectively improves the efficiency of game development and helps you carry out game operations. The Game Service Kit provides game login capabilities, allowing users to log in to the game with their Huawei accounts, thus rapidly promoting the game and sharing the value of Huawei's huge user base.

Game LoginIncludes the use ofHuawei Account LoginUse ofGame official account login scene.. After accessing the game login, the game will be initialized at startup and the player will be presented with the joint login panel, and the player can choose any one of the ways to enter the game.

image
image

Scene Introduction

Sign in with your Huawei account

change scene

Transfer the player's game progress from the HarmonyOS/EMUI system channel package to the HarmonyOS NEXT system package body, i.e., transfer the value of the player identification ID of this Huawei account that corresponds to the channel package under the HarmonyOS/EMUI system of the game, to the player identification ID of this Huawei account that corresponds to the HarmonyOS NEXT system package body of the game. The value of the player ID will be transferred to the player ID of the Huawei account in the HarmonyOS NEXT system package. After the transfer is successful, the player can use the Huawei account to enter the HarmonyOS NEXT game, but can no longer enter the HarmonyOS/EMUI game through that Huawei account.
image
image
image

Associated Scenarios (Quick Login)

Users are authorized to provide their cell phone number to enable them to quickly create or associate an official game account and enter the game through their Huawei account.

image
image
image

Login with official game account

Log in to the game directly through the game's official account, keeping the user experience consistent with the game's official package.

development step

Note: For the specific steps involved in logging in to the game, please refer toAccess GuideThis paper describes only the key development steps.

joint login

After successful initialization, the game can callunionLogininterface for co-logging, Game Service Kit shows the co-logging pop-up box to the player.

let context = getContext(this) as ;
let thirdAccountInfo1: = {
  'accountName': 'testName1', // The name of the type of account the game is open for players to access,for example"official account"、"xxusername"et al. (and other authors),Not a specific player.IDor developersID。
  'accountIcon': $r('') // 游戏official account图标资源信息
};
let request: = {
  showLoginDialog: true,
  thirdAccountInfos: [
    thirdAccountInfo1
  ]
};
try {
  (context, request).then((result: ) => {
    (0x0000, 'testTag', `Succeeded in logining: ${result?.accountName}`);
  }).catch((error: BusinessError) => {
    (0x0000, 'testTag', `Failed to login. Code: ${}, message: ${}`);
  });
} catch (error) {
  let err = error as BusinessError;
  (0x0000, 'testTag', `Failed to login. Code: ${}, message: ${}`);
}

Huawei Account Authentication and Authorization

When the game determines that the accountName is "hw_account", you can choose to create an authorization/login request interface corresponding to the Account Kit according to the value of needBinding to get the Authorization Code information for server verification.

Requires Huawei Player ID to be bound to the official game account (needBinding is true)

1. CallcreateAuthorizationWithHuaweiIDRequestCreate an authorization request and set the parameters.

// Create the authorization request and set the parameters
let authRequest = new ().createAuthorizationWithHuaweiIDRequest();
// Getting authorization for avatars, nicknames, and phone numbers requires passing the following scope
 = ['profile', 'phone']; // If the developer needs to do a server-side authorization request, pass the following scope.
// If the developer needs to do server-side development, pass the following permission to get the authorizationCode
 = ['serviceauthcode']; // If the developer is doing server-side development, pass the following permission to get the authorization code.
// Whether the user needs to log in for authorization. If this value is true and the user is not logged in or authorized, the user login or authorization page will be pulled up.
 = true; // Pull up the user login or authorization page if the user is not logged in or not authorized.
 = true; = ();

2. CallAuthenticationControllertargetexecuteRequestmethod performs the authorization request and processes the authorization result in the Callback, parsing the avatar nickname from the authorization result.

// Execution of authorization requests
try {
  let controller = new (getContext(this));
  (authRequest, (err, data) => {
    if (err) {
      (0x0000, 'testTag', `Failed to authenticate. Code: ${}, message: ${}`);
      return;
    }
    let authorizationWithHuaweiIDResponse = data as ;
    let state = ;
    if (state != undefined && != state) {
      (0x0000, 'testTag', `Failed to authenticate. State is different.`);
      return;
    }
    (0x0000, 'testTag', `Succeeded in authenticating.`);
    let authorizationWithHuaweiIDCredential = !;
    let avatarUri = ;
    let nickName = ;
    let authorizationCode = ;
    // developer processingvatarUri, nickName, authorizationCodetext
  });
} catch (error) {
  let err = error as BusinessError;
  (0x0000, 'testTag', `Failed to authenticate. Code: ${}, message: ${}`);
}

No need for Huawei player logo to be bound to the official game account (i.e. needBinding is false)

1. CallcreateLoginWithHuaweiIDRequestCreate a login request and set the parameters.

// Create the login request and set the parameters
let loginRequest = new ().createLoginWithHuaweiIDRequest();
// Whether to force the Huawei account login screen to be pulled up when the user is not logged in to their Huawei account.
 = true; // If the user is not logged in to the Huawei account, force the Huawei account login screen to pull up.
 = ();

2. CallAuthenticationControllertargetThe executeRequest method executes the login request and processes the login result in a Callback to get the Authorization Code.

// Execute login requests
try {
  let controller = new (getContext(this));
  (loginRequest, (err, data) => {
    if (err) {
      (0x0000, 'testTag', `Failed to login. Code: ${}, message: ${}`);
      return;
    }
    let loginWithHuaweiIDResponse = data as ;
    let state = ;
    if (state != undefined && != state) {
      (0x0000, 'testTag', `Failed to login. State is different.`);
      return;
    }
    (0x0000, 'testTag', `Succeeded in logining.`);

    let loginWithHuaweiIDCredential = !;
    let authorizationCode = ;
    // developer processingauthorizationCode
  });
} catch (error) {
  let err = error as BusinessError;
  (0x0000, 'testTag', `Failed to login. Code: ${}, message: ${}`);
}

Affiliated Games Official Accounts

When the needBinding value obtained by the federated login interface is true, the game can call thebindPlayerThe interface binds the Huawei player identification teamPlayerId to the official game account.

let context = getContext(this) as ;
let thirdOpenId = '123xxxx'; // thirdOpenIdIndicates an official game accountID
let teamPlayerId = '456xxx'; // teamPlayerIdIndicates that the player's Huawei account corresponds to theteamPlayerId
try {
  (context, thirdOpenId, teamPlayerId).then(() => {
    (0x0000, 'testTag', `Succeeded in binding.`);
  }).catch((error: BusinessError) => {
    (0x0000, 'testTag', `Failed to bind. Code: ${}, message: ${}`);
  });
} catch (error) {
  let err = error as BusinessError;
  (0x0000, 'testTag', `Failed to bind. Code: ${}, message: ${}`);
}

addiction prevention for minors

call (programming)verifyLocalPlayerThe interface performs account real-name authentication and game anti-addiction control compliance verification.

let context = getContext(this) as ;
let request: = {
  thirdOpenId: '123xxxx', // Game Official AccountID
  isRealName: true // Whether the player has a real name,The value istrueWhen you indicate that you have a real name,because offalse(a) When a person indicates that he or she does not have his or her real name
};
try {
  (context, request).then(() => {
    (0x0000, 'testTag', `Succeeded in verifying.`);
  }).catch((error: BusinessError) => {
    (0x0000, 'testTag', `Failed to verify. Code: ${}, message: ${}`);
  });
} catch (error) {
  let err = error as BusinessError;
  (0x0000, 'testTag', `Failed to verify. Code: ${}, message: ${}`);
}

Submit Player Character Information

After the player successfully logs into the game and selects a character and district, the game needs to invoke thesavePlayerRoleinterface to report player character information to the Huawei server.

let context = getContext(this) as ;
let request: = {
  roleId: '123', // The player's role id, if the game doesn't have a role system, pass "0", make sure not to pass "" or null.
  roleName: 'Jason', // The name of the player's role, if the game doesn't have a role system, please pass "default", make sure not to pass "" and null.
  serverId: '456', // Player's character name.
  
  gamePlayerId: '789', // If you are transferring scenarios, please pass in the gamePlayerId according to the actual one you got.
  teamPlayerId: '345', // If this is an association scenario, please pass the teamPlayerId according to the actual one you got.
  thirdOpenId: '123' // If you are logged in with an official game account, pass the thirdOpenId as you got it.
}
try {
  (context, request).then(() => {
     (0x0000, 'testTag', `Succeeded in saving.)
  });
} catch (error) {
  let err = error as BusinessError; { (0x0000, 'testTag', `Succeeded in saving.
  (0x0000, 'testTag', `Failed to save. Code: ${}, message: ${}`); }); } catch (error) { let err = error as BusinessError; (0x0000, 'testTag', `Failed to save.
}

Learn more >>

interviewsGame Service Alliance Official Website

gainBasic Game Service Functionality Development Guidance Document