应用与数据集成平台 ROMA Connect-Go SDK使用说明:调用API示例

时间:2025-02-12 14:55:44

调用API示例

  1. 在工程中引入sdk(signer.go)。
    import "apig-sdk/go/core"
  2. 生成一个新的Signer,输入API所授权凭据的Key和Secret,可参考获取API的调用信息获取。
    s := core.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.         Key: os.Getenv("HUAWEICLOUD_SDK_AK"),        Secret:os.Getenv("HUAWEICLOUD_SDK_SK"),}
  3. 生成一个新的Request,指定 域名 、方法名、请求url、query和body,可参考获取API的调用信息获取。
    r, _ := http.NewRequest("POST", "http://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/api?a=1&b=2",                         ioutil.NopCloser(bytes.NewBuffer([]byte("foo=bar"))))
  4. 给请求添加x-stage头,内容为环境名。如有需要,添加需要签名的其他头域。
    r.Header.Add("x-stage", "RELEASE")
  5. 进行签名,执行此函数会在请求中添加用于签名的X-Sdk-Date头和Authorization头。
    s.Sign(r)
  6. 若使用系统分配的子域名访问https请求的API时,需要忽略证书校验,否则跳过此步。
    client:=&http.Client{  Transport:&http.Transport{    TLSClientConfig:&tls.Config{InsecureSkipVerify:true},  },}
  7. 访问API,查看访问结果。
    resp, err := http.DefaultClient.Do(r)body, err := ioutil.ReadAll(resp.Body)
support.huaweicloud.com/devg-roma/apic-dev-190216006.html