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
Log in to the API Gateway console and choose Help Center > SDK Process Flow. Then download the SDK. For details, see section "SDKs" in the User Guide.
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)
- 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 http = require('http')
- Generate a new signer and enter the key and secret.
1 2 3
var sig = new signer.Signer() sig.Key = "QTWAOYT*******QVKYUC" sig.Secret = "MFyfvK41ba2giqM7**********KGpownRZlmVmHc"
- 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:
1
var 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.