package main
import (
"time"
"fmt"
"crypto/hmac"
"crypto/sha1"
"io"
"io/ioutil"
"encoding/base64"
"net/http"
)
func calcAuthorization(source string, secretId string, secretKey string) (sign string, dateTime string, err error) {
timeLocation, _ := time.LoadLocation("Etc/GMT")
dateTime = time.Now().In(timeLocation).Format("Mon, 02 Jan 2006 15:04:05 GMT")
sign = fmt.Sprintf("x-date: %s\\nsource: %s", dateTime, source)
fmt.Println(sign)
//hmac-sha1
h := hmac.New(sha1.New, []byte(secretKey))
io.WriteString(h, sign)
sign = fmt.Sprintf("%x", h.Sum(nil))
sign = string(h.Sum(nil))
fmt.Println("sign:", fmt.Sprintf("%s", h.Sum(nil)))
//base64
sign = base64.StdEncoding.EncodeToString([]byte(sign))
fmt.Println("sign:", sign)
auth := fmt.Sprintf("hmac id=\\"%s\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date source\\", signature=\\"%s\\"",
secretId, sign)
fmt.Println("auth:", auth)
return auth, dateTime, nil
}
func main () {
SecretId := "your SecretId" // 密钥对的 SecretId
SecretKey := "your SecretKey" // 密钥对的 SecretKey
source := "xxxxxx" // 签名水印值,可填写任意值
sign, dateTime, err := calcAuthorization(source, SecretId, SecretKey)
if err != nil {
fmt.Println(err)
return
}
defaultDomain := "service-xxxxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com" // 用户 API 所在服务的域名
reqUrl := "https://service-xxxxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com/release/yousa" // 用户 API 的访问路径
client := &http.Client{
Timeout: 7 * time.Second,//set timeout
}
req, err := http.NewRequest("GET", reqUrl, nil) //set body
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept-Charset", "utf-8;")
req.Header.Set("Host", defaultDomain)
req.Header.Set("Source", source)
req.Header.Set("X-Date", dateTime)
req.Header.Set("Authorization", sign)
// 如果是微服务 API,Header 中需要添加'X-NameSpace-Code'、'X-MicroService-Name'两个字段,通用 API 不需要添加。
req.Header.Set("x-NameSpace-Code", "testmic")
req.Header.Set("x-MicroService-Name", "provider-demo")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println("status code:", resp.StatusCode)
//get resp header
var headerMsg string
for key, _ := range resp.Header {
headerMsg += fmt.Sprintf("\\n%s:%s", key, resp.Header.Get(key))
}
fmt.Println(headerMsg)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
本页内容是否解决了您的问题?