Table of Contents

MegaTracker cloud service authentication

This article describes how to specify the authentication method used when MegaTracker accesses cloud services.

Before you start

Use API Key and API Secret authentication

This method is suitable for traditional key-pair authentication. You need to use APIKeyAccessData to construct the access object in MegaTrackerConfigs.

const apiKeyAccess = new mega.APIKeyAccessData(
    settings.MegaTrackerAppID, // Mega 定位服务 AppID
    settings.MegaTrackerServerAddress, // Mega 定位服务地址
    settings.EasyARAPIKey, // APIKey 字符串
    settings.EasyARAPISecret // APISecret 字符串
);
const megaTrackerConfigs: easyar.MegaTrackerConfigs = {
    access: apiKeyAccess
};
const sessionConfigs: easyar.SessionConfigs = {
    megaTrackerConfigs: megaTrackerConfigs,
    licenseKey: settings.EasyARLicenseKey
};
session = megaComponent.createSession(sessionConfigs);

In this example, the cloud localization map library appId, cloud service serverAddress, cloud service apiKey, and apiSecret in the configuration are first used to create APIKeyAccessData.

Then the created APIKeyAccessData is used to create MegaTrackerConfigs. This means API Key and API Secret authentication will be used.

Use API Token authentication

If a server can periodically update and deliver APIToken (every few minutes or hours), this method avoids directly using APISecret to sign localization requests and is more secure. For how to update APIToken, refer to Token creation and usage.

You can set a timer on the frontend to update the Token according to its validity period.

First, use the Mega localization map library AppID and localization service address in the settings to create TokenAccessData.

Then use the created TokenAccessData to create MegaTrackerConfigs.

Then use MegaTrackerConfigs and the licenseKey in the configuration to create SessionConfigs.

Finally, use the createSession(sessionConfigs) method of EasyARMegaComponent mounted in the xr-frame scene to create session.

When the Token expires, you must call updateToken(apiToken) to update it. Otherwise, the Mega service will be unavailable, and the status in localization results will always be ApiTokenExpired.

const tokenAccess = new mega.TokenAccessData(
    settings.MegaTrackerAppID, // Mega 定位服务 AppID
    settings.MegaTrackerServerAddress, // Mega 定位服务地址
    "your_api_token" // APIToken 字符串
);
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 session for APIToken authentication.