Files
antiwear-web/server/APIGW-javascript-sdk-2.0.6/node_demo.js
ZF sun d43d732c03 feat: 添加华为云API网关SDK并重构认证逻辑
重构服务器端认证逻辑,使用华为云API网关SDK替代原有实现
新增自动获取授权码功能,当用户未输入时自动获取
添加SDK相关测试文件和许可证
2026-04-29 18:14:32 +08:00

38 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
var signer = require('./signer');
var https = require("https");
var sig = new signer.Signer();
//Set the AK/SK to sign and authenticate the request.
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
// 本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
sig.Key = process.env.HUAWEICLOUD_SDK_AK;
sig.Secret = process.env.HUAWEICLOUD_SDK_SK;
//The following example shows how to set the request URL and parameters to query a VPC list.
//Specify a request method, such as GET, PUT, POST, DELETE, HEAD, and PATCH.
//Set request host.
//Set request URI.
//Set parameters for the request URL.
var r = new signer.HttpRequest("GET", "endpoint.example.com/v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limie=1");
//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 = {"Content-Type": "application/json"};
//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 = '';
var opt = sig.Sign(r);
console.log(opt.headers["X-Sdk-Date"]);
console.log(opt.headers["Authorization"]);
var req = https.request(opt, function (res) {
console.log(res.statusCode);
console.log('headers:', JSON.stringify(res.headers));
res.on("data", function (chunk) {
console.log(chunk.toString())
})
});
req.on("error", function (err) {
console.log(err.message)
});
req.write(r.body);
req.end();