Authenticate requests
Updated at:
This topic describes how to authenticate requests for the HarmonyOS SDK.
Authentication configuration
Authentication adds a security signature to resolution requests to prevent unauthorized calls. For more information, see Developer guide. You must also get a secret key.
Enable authentication for the SDK
If you activate the HTTPDNS service in the Alibaba Cloud HTTPDNS console and enable the Signed Access option, you must enable authentication by configuring the HTTPDNS instance with the secret key from the console during initialization. The following example shows how:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { httpdns } from '@aliyun/httpdns';
const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console';
const SECRET_KEY = 'Replace this with the secret key from the Alibaba Cloud HTTPDNS console';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
// Configure HTTPDNS
httpdns.configService(ACCOUNT_ID, {
context: this.context,
// ************* Start: Initialize and configure the authentication secretKey *************
secretKey: SECRET_KEY,
// ************* End: Initialize and configure the authentication secretKey *************
});
}
// Omit other code
}For subsequent resolution requests, the SDK signs the request using the configured secret key. The server then verifies the signature. If signature verification fails, the resolution request fails.
Is this page helpful?