package mainimport ("crypto/hmac""crypto/md5""crypto/sha1""encoding/base64""encoding/hex""fmt""io/ioutil""log""net/http""net/url""sort""strings""time")func main() {// Application's `ApiAppKey`const ApiAppKey = "Your ApiAppKey"// Application's `ApiAppSecret`const ApiAppSecret = "Your ApiAppSecret"const Url = "http://service-xxx-xxx.gz.apigw.tencentcs.com/"const GmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT"const HTTPMethod = "GET"const Accept = "application/json"const ContentType = "application/json"// Parse `Host` and `Path` according to `Url`u, err := url.Parse(Url)if err != nil {log.Fatal(err)}Host:= u.Hostname()Path:= u.PathQuery := u.RawQuery// Environment information is not included in the signature pathif strings.HasPrefix(Path, "/release") {Path = strings.TrimPrefix(Path, "/release")}else if strings.HasPrefix(Path, "/test") {Path = strings.TrimPrefix(Path, "/test")}else if strings.HasPrefix(Path, "/prepub") {Path = strings.TrimPrefix(Path, "/prepub")}if Path == "" {Path = "/"}// Splice query parameters. The query parameters need to be sorted in lexicographical order.if len(Query) > 0 {args, _ := url.ParseQuery(Query)var keys []stringfor k := range args {keys = append(keys, k)}sort.Strings(keys)sortQuery := ""for _, k := range keys {if args[k][0] != "" {sortQuery = sortQuery + "&" + k + "=" + args[k][0]} else {sortQuery = sortQuery + "&" + k}}sortQuery = strings.TrimPrefix(sortQuery, "&")Path = Path + "?" + sortQuery}// Get the current UTC timexDate := time.Now().UTC().Format(GmtFormat)ContentMD5 := ""bodyStr := `{"arg1":"a","arg2":"b"}`if HTTPMethod == "POST" {h := md5.New()h.Write([]byte(bodyStr))md5Str := hex.EncodeToString(h.Sum(nil))ContentMD5 = base64.StdEncoding.EncodeToString([]byte(md5Str))}// Construct the signaturesigningStr := fmt.Sprintf("x-date: %s\\n%s\\n%s\\n%s\\n%s\\n%s", xDate, HTTPMethod, Accept, ContentType,ContentMD5, Path)mac := hmac.New(sha1.New, []byte(ApiAppSecret))_, err= mac.Write([]byte(signingStr))if err != nil {log.Fatal(err)}signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))sign := fmt.Sprintf("hmac id=\\"%s\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date\\", signature=\\"%s\\"",ApiAppKey, signature)// Construct the requestheaders := map[string]string{"Host": Host,"Accept": Accept,"Content-Type": ContentType,"x-date": xDate,"Authorization": sign,}// Send the requestreq, err := http.NewRequest(HTTPMethod, Url, strings.NewReader(bodyStr))if err != nil {log.Fatal(err)}for k, v := range headers {req.Header.Add(k, v)}res, err := http.DefaultClient.Do(req)if err != nil {log.Fatal(err)}defer res.Body.Close()resBody, _ := ioutil.ReadAll(res.Body)fmt.Println(string(resBody))}
package mainimport ("crypto/hmac""crypto/sha1""encoding/base64""fmt""io/ioutil""log""net/http""net/url""sort""strings""time")func main() {// Application's `ApiAppKey`const ApiAppKey = "Your ApiAppKey"// Application's `ApiAppSecret`const ApiAppSecret = "Your ApiAppSecret"const Url = "http://service-xxx-xxx.gz.apigw.tencentcs.com/"const GmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT"const HTTPMethod = "POST"const Accept = "application/json"const ContentType = "application/x-www-form-urlencoded"// Parse `Host` and `Path` according to `Url`u, err := url.Parse(Url)if err != nil {log.Fatal(err)}Host := u.Hostname()Path := u.PathQuery := u.RawQuery// Environment information is not included in the signature pathif strings.HasPrefix(Path, "/release") {Path = strings.TrimPrefix(Path, "/release")}else if strings.HasPrefix(Path, "/test") {Path = strings.TrimPrefix(Path, "/test")}else if strings.HasPrefix(Path, "/prepub") {Path = strings.TrimPrefix(Path, "/prepub")}if Path == "" {Path = "/"}// Splice query parameters. The query parameters need to be sorted in lexicographical order. In this demo, it is assumed the parameters are already sorted.if len(Query) > 0 {Path = Path + "?" + Query}// Get the current UTC timexDate := time.Now().UTC().Format(GmtFormat)ContentMD5 := ""// Request body form databody := map[string]string{"arg1": "a","arg2": "b",}var bodyKeys []stringfor k := range body {bodyKeys = append(bodyKeys, k)}var bodyBuilder strings.Buildersort.Strings(bodyKeys)for _, k := range bodyKeys {bodyBuilder.WriteString(fmt.Sprintf("%s=%s&", k, body[k]))}bodyStr := bodyBuilder.String()// Remove the last `&`bodyStr = bodyStr[:len(bodyStr) - 1]// Construct the signaturesigningStr := fmt.Sprintf("x-date: %s\\n%s\\n%s\\n%s\\n%s\\n%s?%s", xDate, HTTPMethod, Accept, ContentType,ContentMD5, Path, bodyStr)mac := hmac.New(sha1.New, []byte(ApiAppSecret))_, err = mac.Write([]byte(signingStr))if err != nil {log.Fatal(err)}signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))sign := fmt.Sprintf("hmac id=\\"%s\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date\\", signature=\\"%s\\"",ApiAppKey, signature)// Construct the requestheaders := map[string]string{"Host": Host,"Accept": Accept,"Content-Type": ContentType,"x-date": xDate,"Authorization": sign,}// Send the requestreq, err := http.NewRequest(HTTPMethod, Url, strings.NewReader(bodyStr))if err != nil {log.Fatal(err)}for k, v := range headers {req.Header.Add(k, v)}res, err := http.DefaultClient.Do(req)if err != nil {log.Fatal(err)}defer res.Body.Close()resBody, _ := ioutil.ReadAll(res.Body)fmt.Println(string(resBody))}
package mainimport ("crypto/md5""encoding/base64""encoding/hex""fmt""io/ioutil""log""net/http""net/url""sort""strings""time""unsafe")/*#cgo CFLAGS: -I./#cgo LDFLAGS: -L./ -lTencentSM#include "sm.h" // Quotation mark is used because it is not a standard "C" header file*/import "C"func mySM3_HMAC(data []byte, dataLen int, key []byte, keyLen int, mac []byte, macLen int) int {if data == nil || key == nil || mac == nil || len(mac) != macLen {panic("invalid parameter")}return int(C.SM3_HMAC((*C.uchar)(unsafe.Pointer(&data[0])), (C.size_t)(dataLen),(*C.uchar)(unsafe.Pointer(&key[0])), (C.size_t)(keyLen), (*C.uchar)(unsafe.Pointer(&mac[0]))))}func main() {// Application's `ApiAppKey`const ApiAppKey = "Your ApiAppKey"// Application's `ApiAppSecret`const ApiAppSecret = "Your ApiAppSecret"const Url = "http://service-xxx-xxx.gz.apigw.tencentcs.com/a?c=1&b=3"const GmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT"const HTTPMethod = "GET"const Accept = "application/json"const ContentType = "application/json"// Parse `Host` and `Path` according to `Url`u, err := url.Parse(Url)if err != nil {log.Fatal(err)}Host:= u.Hostname()Path:= u.PathQuery := u.RawQuery// Environment information is not included in the signature pathif strings.HasPrefix(Path, "/release") {Path = strings.TrimPrefix(Path, "/release")}else if strings.HasPrefix(Path, "/test") {Path = strings.TrimPrefix(Path, "/test")}else if strings.HasPrefix(Path, "/prepub") {Path = strings.TrimPrefix(Path, "/prepub")}if Path == "" {Path = "/"}// Splice query parameters. The query parameters need to be sorted in lexicographical order.if len(Query) > 0 {args, _ := url.ParseQuery(Query)var keys []stringfor k := range args {keys = append(keys, k)}sort.Strings(keys)sortQuery := ""for _, k := range keys {if args[k][0] != "" {sortQuery = sortQuery + "&" + k + "=" + args[k][0]} else {sortQuery = sortQuery + "&" + k}}sortQuery = strings.TrimPrefix(sortQuery, "&")Path = Path + "?" + sortQuery}// Get the current UTC timexDate := time.Now().UTC().Format(GmtFormat)ContentMD5 := ""bodyStr := `{"arg1":"a","arg2":"b"}`if HTTPMethod == "POST" {h := md5.New()h.Write([]byte(bodyStr))md5Str := hex.EncodeToString(h.Sum(nil))ContentMD5 = base64.StdEncoding.EncodeToString([]byte(md5Str))}// Construct the signaturesigningStr := fmt.Sprintf("x-date: %s\\n%s\\n%s\\n%s\\n%s\\n%s", xDate, HTTPMethod, Accept, ContentType,ContentMD5, Path)data := []byte(signingStr)key := []byte(ApiAppSecret)var out [32]bytemySM3_HMAC(data[:], len(data), key[:], len(key), out[:], len(out))signature := base64.StdEncoding.EncodeToString(out[:])sign := fmt.Sprintf("hmac id=\\"%s\\", algorithm=\\"hmac-sm3\\", headers=\\"x-date\\", signature=\\"%s\\"",ApiAppKey, signature)// Construct the requestheaders := map[string]string{"Host": Host,"Accept": Accept,"Content-Type": ContentType,"x-date": xDate,"Authorization": sign,}// Send the requestreq, err := http.NewRequest(HTTPMethod, Url, strings.NewReader(bodyStr))if err != nil {log.Fatal(err)}for k, v := range headers {req.Header.Add(k, v)}res, err := http.DefaultClient.Do(req)if err != nil {log.Fatal(err)}defer res.Body.Close()resBody, _ := ioutil.ReadAll(res.Body)fmt.Println(string(resBody))}
Was this page helpful?