Configure access credentials (AKSK) using environment variables
Note:
An AccessKey for an Alibaba Cloud account has permissions for all APIs, which poses a high security risk. We recommend that you create and use a Resource Access Management (RAM) user for API access or daily O&M. To create a RAM user, log on to the Resource Access Management (RAM) console.
Do not save your AccessKey ID and AccessKey secret in your code. This practice can lead to credential leakage. We recommend that you use environment variables to store and access your credentials.
System configuration for Linux and macOS
export NLP_AK_ENV=<access_key_id> export NLP_SK_ENV=<access_key_secret>Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey, see Step 2: Obtain an AccessKey.
Configuring a Windows system
Create a file for the environment variables. Add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables, and set their values to your AccessKey ID and AccessKey secret.Restart Windows.
Sample code
package main
import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"fmt"
"os"
)
func main() {
/**
* An AccessKey for an Alibaba Cloud account has permissions for all APIs. This poses a high security threat. Create and use a RAM user for API access or daily O&M. Log on to the RAM console to create a RAM user.
* This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
* Do not save your AccessKey ID and AccessKey secret in your code. This creates a risk of key leakage.
*/
AccessKeyId := os.Getenv("NLP_AK_ENV")
AccessKeySecret := os.Getenv("NLP_SK_ENV")
client, err := sdk.NewClientWithAccessKey("cn-hangzhou", AccessKeyId, AccessKeySecret)
if err != nil {
panic(err)
}
request := requests.NewCommonRequest()
request.Domain = "alinlp.cn-hangzhou.aliyuncs.com"
request.Version = "2020-06-29"
// Specify the ApiName (Action) because this is an RPC API.
request.ApiName = "GetNerChEcom"
request.QueryParams["ServiceCode"] = "alinlp"
request.QueryParams["Text"] = "These are a great pair of sneakers"
request.QueryParams["TokenizerId"] = "GENERAL_CHN"
response, err := client.ProcessCommonRequest(request)
if err != nil {
panic(err)
}
fmt.Print(response.GetHttpContentString())
}
Note: In the sample code, GetNerChEcom is the actionName of the algorithm. You must replace it with the value of the algorithm that you want to use. You can find this value in the API Reference document under Request Parameters > Action > Sample Value.
For example, to call the Basic Edition - Chinese Tokenization - General service, go to Chinese Tokenization (Basic Edition). Copy the sample value from the figure and replace GetNerChEcom with GetWsChGeneralRequest. After you make the replacement, you must update the request parameters to match the new algorithm. For more information, see the API document.
Troubleshoot call exceptions
If a call fails, see Troubleshoot call exceptions (error code summary) to find the cause of the error and the solution.