SecretAccessKey = "yyyyy"
Addr = "https://iam.volcengineapi.com"
Path = "/" // path without Query
Action = "ListFunctions"
Func hmacSHA256 (key [] bytes, content string) [] bytes {
Mac: = hmac.New (sha256.New, key)
Mac. Write ([] bytes (content))
func getSignedKey (secretKey, date, region, service string) [] byte {
kDate: = hmacSHA256 ([] bytes (secretKey), date)
kRegion: = hmacSHA256 (kDate, region)
kService: = hmacSHA256 (kRegion, service)
kSigning: = hmacSHA256 (kService, "request")
Func hashSHA256 (data [] bytes) [] bytes {
If _, err: = hash. Write (data); err! = nil {
Log. Printf ("input hash err:% s", err. Error ())
func doRequest (method string, queries url. Values, body [] bytes) 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))
Returns fmt. Errorf ("bad request:% w", err)
//2. Build the signature material
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"}
Var headerList [] string
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: = auth Date + "/" + Region + "/" + Service + "/request"
signString: = strings. Join ([] string {
Log. Printf ("sign string:\ n% s\ n", signString)
//3. Build the authentication request header
signedKey: = getSignedKey (SecretAccessKey, authDate, Region, Service)
Signature: = hex. EncodeToString (hmacSHA256 (signedKey, signString))
Log. Printf ("signature:% s\ n", signature)
Authorization: = "HMAC-SHA256" +
"Credential =" + AccessKey ID + "/" + credential Scope +
", SignedHeaders =" + strings. Join (signedHeaders , ";") +
", Signature =" + signature
Request. Header. Set ("Authorization", authorization)
Log. Printf ("authorization:% s\ n", authorization)
//4. Print the request and initiate the request
requestRaw, err: = htputil. DumpRequest (request, true)
Returns fmt. Errorf ("dump request err:% w", err)
Log. Printf ("request:\ n% s\ n", string (requestRaw))
Response, err: = http.DefaultClient.Do (request)
Returns fmt. Errorf ("do request err:% w", err)
responseRaw, err: = htputil. DumpResponse (response, true)
Returns fmt. Errorf ("dump response err:% w", err)
Log. Printf ("response:\ n% s\ n", string (responseRaw))
If response. StatusCode == 200 {
Log. Printf ("Request successful")
Log. Printf ("Request failed")
Query1: = make (url. Values)
doRequest (http. MethodGet, query1, nil)