MegaTracker cloud service authentication
This article introduces how to specify authentication methods when using MegaTracker cloud services.
Before you begin
Using API Key and API Secret authentication
This method applies to traditional key-pair verification. You need to use APIKeyAccessData to construct the access object in MegaTrackerConfigs.
const apiKeyAccess = new mega.APIKeyAccessData(
settings.MegaTrackerAppID, // Mega localization service AppID
settings.MegaTrackerServerAddress, // Mega localization service address
settings.EasyARAPIKey, // APIKey string
settings.EasyARAPISecret // APISecret string
);
const megaTrackerConfigs: easyar.MegaTrackerConfigs = {
access: apiKeyAccess
};
const sessionConfigs: easyar.SessionConfigs = {
megaTrackerConfigs: megaTrackerConfigs,
licenseKey: settings.EasyARLicenseKey
};
session = megaComponent.createSession(sessionConfigs);
This example first creates APIKeyAccessData using the configured cloud localization library
appId, cloud serviceserverAddress, cloud serviceapiKeyandapiSecret.Then uses the created APIKeyAccessData to create MegaTrackerConfigs. This indicates that API Key and API Secret authentication will be used.
Using API Token authentication
If you can use a server to regularly update and distribute APIToken (every few minutes or hours), this method avoids directly using APISecret to sign localization requests, providing higher security. For APIToken update methods, refer to Token creation and usage methods.
You can set a timer on the frontend to update it based on the Token's validity period.
First create TokenAccessData using the configured Mega localization library AppID and localization service address.
Then use the created TokenAccessData to create MegaTrackerConfigs.
Then create SessionConfigs using MegaTrackerConfigs and the configured licenseKey.
Finally, use the createSession(sessionConfigs) method of the EasyARMegaComponent mounted in the xr-frame scene to create the session.
When the Token expires, you must call updateToken(apiToken) to update it. Otherwise, the Mega service will be unavailable, and the localization result status will always be ApiTokenExpired.
const tokenAccess = new mega.TokenAccessData(
settings.MegaTrackerAppID, // Mega localization service AppID
settings.MegaTrackerServerAddress, // Mega localization service address
"your_api_token" // APIToken string
);
const megaTrackerConfigs: easyar.MegaTrackerConfigs = {
access: tokenAccess
};
const sessionConfigs: easyar.SessionConfigs = {
megaTrackerConfigs: megaTrackerConfigs,
licenseKey: settings.EasyARLicenseKey
};
session = megaComponent.createSession(sessionConfigs);
This example demonstrates how to use TokenAccessData to create MegaTrackerConfigs, and use this MegaTrackerConfigs to create a session for
APITokenauthentication.