SecretAccessKey = "yyyyyy"
Addr = "https://iam.volcengineapi.com"
Path = "/" // 路径,不包含 Query
Action = "ListFunctions"
func hmacSHA256(key []byte, content string) []byte {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(content))
func getSignedKey(secretKey, date, region, service string) []byte {
kDate := hmacSHA256([]byte(secretKey), date)
kRegion := hmacSHA256(kDate, region)
kService := hmacSHA256(kRegion, service)
kSigning := hmacSHA256(kService, "request")
func hashSHA256(data []byte) []byte {
if _, err := hash.Write(data); err != nil {
log.Printf("input hash err:%s", err.Error())
func doRequest(method string, queries url.Values, body []byte) error {
queries.Set("Action", Action)
queries.Set("Version", Version)
requestAddr := fmt.Sprintf("%s%s?%s", Addr, Path, queries.Encode())
log.Printf("request addr: %s\n", requestAddr)
request, err := http.NewRequest(method, requestAddr, bytes.NewBuffer(body))
return fmt.Errorf("bad request: %w", err)
date := now.UTC().Format("20060102T150405Z")
request.Header.Set("X-Date", date)
payload := hex.EncodeToString(hashSHA256(body))
request.Header.Set("X-Content-Sha256", payload)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
queryString := strings.Replace(queries.Encode(), "+", "%20", -1)
signedHeaders := []string{"host", "x-date", "x-content-sha256", "content-type"}
for _, header := range signedHeaders {
headerList = append(headerList, header+":"+request.Host)
v := request.Header.Get(header)
headerList = append(headerList, header+":"+strings.TrimSpace(v))
headerString := strings.Join(headerList, "\n")
canonicalString := strings.Join([]string{
strings.Join(signedHeaders, ";"),
log.Printf("canonical string:\n%s\n", canonicalString)
hashedCanonicalString := hex.EncodeToString(hashSHA256([]byte(canonicalString)))
log.Printf("hashed canonical string: %s\n", hashedCanonicalString)
credentialScope := authDate + "/" + Region + "/" + Service + "/request"
signString := strings.Join([]string{
log.Printf("sign string:\n%s\n", signString)
signedKey := getSignedKey(SecretAccessKey, authDate, Region, Service)
signature := hex.EncodeToString(hmacSHA256(signedKey, signString))
log.Printf("signature: %s\n", signature)
authorization := "HMAC-SHA256" +
" Credential=" + AccessKeyID + "/" + credentialScope +
", SignedHeaders=" + strings.Join(signedHeaders, ";") +
", Signature=" + signature
request.Header.Set("Authorization", authorization)
log.Printf("authorization: %s\n", authorization)
requestRaw, err := httputil.DumpRequest(request, true)
return fmt.Errorf("dump request err: %w", err)
log.Printf("request:\n%s\n", string(requestRaw))
response, err := http.DefaultClient.Do(request)
return fmt.Errorf("do request err: %w", err)
responseRaw, err := httputil.DumpResponse(response, true)
return fmt.Errorf("dump response err: %w", err)
log.Printf("response:\n%s\n", string(responseRaw))
if response.StatusCode == 200 {
query1 := make(url.Values)
doRequest(http.MethodGet, query1, nil)