Updated on 2024-02-18 GMT+08:00

JavaScript

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 from the IntelliJ IDEA official website and install it.
  • Download the Node.js installation package 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

Download the SDK and demo.

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

  1. Start IDEA and choose File > New > Project.

    In the New Project dialog box, choose Static Web and click Next.

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

  3. View the directory structure shown in the following figure.

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

  5. Click + and select Node.js.

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

Calling APIs (Node.js)

  1. Run the npm command to install the moment and moment-timezone modules.

    1
    2
    npm install moment --save
    npm install moment-timezone --save
    

  2. Import signer.js to your project.

    1
    2
    var signer = require('./signer')
    var http = require('http')
    

  3. Generate a new signer and enter the key and secret.

    1
    2
    3
    4
    5
    var sig = new signer.Signer()
    // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables.
    // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
    sig.Key = process.env.HUAWEICLOUD_SDK_AK
    sig.Secret = process.env.HUAWEICLOUD_SDK_SK
    

  4. 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 = '';
    

  5. 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"};
    

  6. Execute the following function to generate HTTPS request parameters, and add the X-Sdk-Date and Authorization headers for signing the request:

    1
    var opt = sig.Sign(r)
    

  7. 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()