API签名指南-C#:请求签名与API调用

时间:2024-07-19 18:47:19

请求签名与API调用

  1. 在工程中引入sdk。

    1
    2
    3
    4
    5
    6
    using System;
    using System.Net;
    using System.IO;
    using System.Net.Http;
    using System.Threading;
    using APIGATEWAY_SDK;
    

  2. 生成一个新的Signer, 填入AK和SK。

    1. 本示例以AK和SK保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。以Linux系统为例在本地将已获取的AK/SK设置为环境变量。
      1. 打开终端,输入以下命令打开环境变量配置文件。

        vi ~/.bashrc

      2. 设置环境变量,保存文件并退出编辑器。
        export HUAWEICLOUD_SDK_AK="已获取AK值" 
        export HUAWEICLOUD_SDK_SK="已获取SK值" 
      3. 输入以下命令使配置文件生效。

        source ~/.bashrc

    2. 生成一个新的Signer, 填入已设置的环境变量。
      1
      2
      3
      4
      5
      Signer signer = new 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.
      signer.Key = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK");
      signer.Secret = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK");
      

  3. 生成一个新的Request,指定 域名 、方法名、请求uri和body。

    1
    2
    3
    4
    //The following example shows how to set the request URL and parameters to query a VPC list.
    HttpRequest r = new HttpRequest("GET", new Uri("https://{service}.region.example.com/v1/77b6a44cba5**********9a8ff44fd/vpcs?limit=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 = "";
    

  4. 添加需要签名的其他头域,或者其他用途的头域,如API的环境信息添加x-stage,多项目场景中添加X-Project-Id,或者全局服务场景中添加X-Domain-Id。

    1
    2
    3
    4
    r.headers.Add("x-stage", "RELEASE");
    r.headers.Add("X-Project-Id", "xxx");
    r.headers.Add("X-Domain-Id", "xxx");
    //Add header parameters, for example, X-Domain-Id for invoking a global service and X-Project-Id for invoking a project-level service.
    

  5. 进行签名,执行此函数会生成一个新的HttpWebRequest,并在请求参数中添加用于签名的X-Sdk-Date头和Authorization头。

    如果您使用HTTP Client,可以从请求中获取头部信息使用。关于头部信息,请参考AK/SK签名认证算法详解
    1
    HttpWebRequest req = signer.Sign(r);
    

  6. 访问API,查看访问结果。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    try
    {
        var writer = new StreamWriter(req.GetRequestStream());
        writer.Write(r.body);
        writer.Flush();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        var reader = new StreamReader(resp.GetResponseStream());
        Console.WriteLine(reader.ReadToEnd());
    }
    catch (WebException e)
    {
        HttpWebResponse resp = (HttpWebResponse)e.Response;
        if (resp != null)
        {
            Console.WriteLine((int)resp.StatusCode + " " + resp.StatusDescription);
            var reader = new StreamReader(resp.GetResponseStream());
            Console.WriteLine(reader.ReadToEnd());
        }
        else
        {
            Console.WriteLine(e.Message);
        }
    }
    Console.WriteLine("----------------");
    

support.huaweicloud.com/devg-apisign/api-sign-sdk-csharp.html