JavaScript Signing Guide
This section uses IntelliJ IDEA as an example to describe how to integrate the JavaScript SDK for API request signing. You can import the sample project in the code package, and integrate the signing SDK into your application by referring to the API calling example.
The descriptions in this section are provided based on the Node.js environment.
Preparing the Environment
- Download IntelliJ IDEA 2018.3.5 or later from the IntelliJ IDEA official website and install it.
- Download the Node.js 15.10.0 or later from the Node.js official website and install it.
After Node.js is installed, run the npm command to install the moment and moment-timezone modules.
npm install moment --save npm install moment-timezone --save
- Install the Node.js plug-in on IDEA.
Obtaining the SDK
Log in to the APIG console and choose Help Center > SDK Process Flow. Then download the SDK.
Decompress the downloaded package to the current folder. The following table shows the directory structure.
|
Name |
Description |
|---|---|
|
signer.js |
SDK code |
|
node_demo.js |
Node.js sample code |
|
test.js |
Test case |
|
licenses\license-crypto-js |
Third-party licenses |
|
licenses\license-node |
Creating a Project
- Start IDEA and choose File > New > Project.
In the New Project dialog box, choose Static Web and click Next.

- Click ..., select the directory where the SDK is decompressed, and click Finish.

- View the directory structure shown in the following figure.

- In the upper right corner of the IDEA window, click Edit Configurations or Add Configurations.

- Click + and select Node.js.

- Set JavaScript file to node_demo.js and click OK.

Calling APIs (Node.js)
After the calling information is modified, the sample code can be directly called. For details about the calling information, see Preparations.
- Run the npm command to install the moment and moment-timezone modules.
1 2
npm install moment --save npm install moment-timezone --save
- Import signer.js to your project.
1 2
var signer = require('./signer') var https = require('https')
- Generate a new signer and enter the AK and SK.
- In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
- Open the terminal and run the following command to open the environment variable configuration file:
- Set environment variables, save the file, and exit the editor.
export CLOUD_SDK_AK="Obtained AK" export CLOUD_SDK_SK="Obtained SK"
- Run the following command to apply the modification:
- Generate a new signer and enter the configured environment variables.
1 2 3
var sig = new signer.Signer() sig.Key = process.env.CLOUD_SDK_AK sig.Secret = process.env.CLOUD_SDK_SK
- In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
- Generate a new request, and specify the domain name, method, request URI, and body.
1 2 3 4 5
//The following example shows how to set the request URL and parameters to query a VPC list. var r = new signer.HttpRequest("GET", "service.region.example.com/v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limie=1"); //Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped. r.body = '';
- Add other headers required for request signing or other purposes. For example, add the X-Project-Id header in multi-project scenarios or the X-Domain-Id header for a global service.
1 2
//Add header parameters, for example, X-Domain-Id for invoking a global service and X-Project-Id for invoking a project-level service. r.headers = {"X-Project-Id": "xxx"};
- Execute the following function to generate HTTPS request parameters, and add the X-Sdk-Date and Authorization headers for signing the request:
1var opt = sig.Sign(r)
- Access the API and view the access result.
1 2 3 4 5 6 7 8 9 10 11
var req = https.request(opt, function(res){ console.log(res.statusCode) res.on("data", function(chunk){ console.log(chunk.toString()) }) }) req.on("error",function(err){ console.log(err.message) }) req.write(r.body) req.end()
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.
