Skip to content

Commit 9817ec7

Browse files
committed
new repo
0 parents  commit 9817ec7

39 files changed

+7416
-0
lines changed

Qcloud_CDN_API/go/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## qcloud cdn openapi go版本sdk
2+
3+
## 使用方法
4+
- 在GOPATH下的src 下添加openapi的sdk qcloudcdn_api
5+
- qcloudcdn_api.Signature 生成签名 qcloudcdn_api.SendRequest 发送请求
6+
7+
## 代码示例
8+
```
9+
package main
10+
11+
import (
12+
"fmt"
13+
cdnapi "qcloudcdn_api"
14+
)
15+
16+
func main() {
17+
/**get SecretKey & SecretId from https://console.qcloud.com/capi**/
18+
var Requesturl string = "cdn.api.qcloud.com/v2/index.php"
19+
var SecretKey string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
20+
var Method string = "POST"
21+
22+
/**params to signature**/
23+
params := make(map[string]interface{})
24+
params["SecretId"] = "ooooooooooooooooooooooooooooooo"
25+
params["Action"] = "RefreshCdnUrl"
26+
params["urls.0"] = "http://hello.world.att.oa.com/test1.php"
27+
28+
/*use qcloudcdn_api.Signature to obtain signature and params with correct signature**/
29+
signature, request_params := cdnapi.Signature(SecretKey, params, Method, Requesturl)
30+
fmt.Println("signature : ", signature)
31+
32+
/*use qcloudcdn_api.SendRequest to send request**/
33+
response := cdnapi.SendRequest(Requesturl, request_params, Method)
34+
fmt.Println(response)
35+
}
36+
```
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package qcloudcdn_api
2+
3+
import (
4+
"crypto/hmac"
5+
"crypto/sha1"
6+
"crypto/tls"
7+
"encoding/base64"
8+
"fmt"
9+
"io/ioutil"
10+
"math/rand"
11+
"net/http"
12+
"net/url"
13+
"sort"
14+
"strconv"
15+
"strings"
16+
"time"
17+
)
18+
19+
/***
20+
qcloud cdn openapi
21+
author:evincai@tencent.com
22+
***/
23+
24+
/**
25+
*@brief qcloud cdn openapi signature
26+
*@param secretKey secretKey to log in qcloud
27+
*@param params params of qcloud openapi interface
28+
*@param method http method
29+
*@param requesturl url
30+
31+
*@return Signature signature
32+
*@return params params of qcloud openapi interfac include Signature
33+
**/
34+
35+
func Signature(secretKey string, params map[string]interface{}, method string, requesturl string) (string, map[string]interface{}) {
36+
/*add common params*/
37+
timestamp := time.Now().Unix()
38+
rd := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000)
39+
params["Timestamp"] = timestamp
40+
params["Nonce"] = rd
41+
/**sort all the params to make signPlainText**/
42+
sigUrl := method + requesturl + "?"
43+
var keys []string
44+
for k := range params {
45+
keys = append(keys, k)
46+
}
47+
sort.Strings(keys)
48+
isfirst := true
49+
for _, key := range keys {
50+
if !isfirst {
51+
sigUrl = sigUrl + "&"
52+
}
53+
isfirst = false
54+
if strings.Contains(key, "_") {
55+
strings.Replace(key, ".", "_", -1)
56+
}
57+
value := typeSwitcher(params[key])
58+
sigUrl = sigUrl + key + "=" + value
59+
}
60+
fmt.Println("signPlainText: ", sigUrl)
61+
unencode_sign, _sign := sign(sigUrl, secretKey)
62+
params["Signature"] = unencode_sign
63+
fmt.Println("unencoded signature: ", unencode_sign)
64+
return _sign, params
65+
}
66+
67+
/**
68+
*@brief send request to qcloud
69+
*@param params params of qcloud openapi interface include signature
70+
*@param method http method
71+
*@param requesturl url
72+
73+
*@return Signature signature
74+
*@return params params of qcloud openapi interfac include Signature
75+
**/
76+
77+
func SendRequest(requesturl string, params map[string]interface{}, method string) string {
78+
requesturl = "https://" + requesturl
79+
var response string
80+
if method == "GET" {
81+
params_str := "?" + ParamsToStr(params)
82+
requesturl = requesturl + params_str
83+
response = httpGet(requesturl)
84+
} else if method == "POST" {
85+
response = httpPost(requesturl, params)
86+
} else {
87+
fmt.Println("unsuppported http method")
88+
return "unsuppported http method"
89+
}
90+
return response
91+
}
92+
93+
func typeSwitcher(t interface{}) string {
94+
switch v := t.(type) {
95+
case int:
96+
return strconv.Itoa(v)
97+
case string:
98+
return v
99+
case int64:
100+
return strconv.Itoa(int(v))
101+
default:
102+
return ""
103+
}
104+
}
105+
106+
func ParamsToStr(params map[string]interface{}) string {
107+
isfirst := true
108+
requesturl := ""
109+
for k, v := range params {
110+
if !isfirst {
111+
requesturl = requesturl + "&"
112+
}
113+
isfirst = false
114+
if strings.Contains(k, "_") {
115+
strings.Replace(k, ".", "_", -1)
116+
}
117+
v := typeSwitcher(v)
118+
requesturl = requesturl + k + "=" + url.QueryEscape(v)
119+
}
120+
return requesturl
121+
}
122+
123+
func sign(signPlainText string, secretKey string) (string, string) {
124+
key := []byte(secretKey)
125+
hash := hmac.New(sha1.New, key)
126+
hash.Write([]byte(signPlainText))
127+
sig := base64.StdEncoding.EncodeToString([]byte(string(hash.Sum(nil))))
128+
encd_sig := url.QueryEscape(sig)
129+
return sig, encd_sig
130+
}
131+
132+
func httpGet(url string) string {
133+
tr := &http.Transport{
134+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
135+
}
136+
client := &http.Client{Transport: tr, Timeout: time.Duration(3) * time.Second}
137+
fmt.Println(url)
138+
resp, err := client.Get(url)
139+
if err != nil {
140+
fmt.Println(err)
141+
return err.Error()
142+
}
143+
defer resp.Body.Close()
144+
body, erro := ioutil.ReadAll(resp.Body)
145+
if erro != nil {
146+
fmt.Println("http wrong erro")
147+
return erro.Error()
148+
}
149+
return string(body)
150+
}
151+
152+
func httpPost(requesturl string, params map[string]interface{}) string {
153+
req, err := http.NewRequest("POST", requesturl, strings.NewReader(ParamsToStr(params)))
154+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
155+
/*
156+
req, err := http.NewRequest("POST", requesturl, strings.NewReader(form.Encode()))
157+
fmt.Println(strings.NewReader(form.Encode()))
158+
*/
159+
tr := &http.Transport{
160+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
161+
}
162+
client := &http.Client{Transport: tr, Timeout: time.Duration(3) * time.Second}
163+
resp, err := client.Do(req)
164+
if err != nil {
165+
fmt.Println(err)
166+
return err.Error()
167+
}
168+
defer resp.Body.Close()
169+
body, erro := ioutil.ReadAll(resp.Body)
170+
if erro != nil {
171+
fmt.Println("http wrong erro")
172+
return erro.Error()
173+
}
174+
return string(body)
175+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
cdnapi "qcloudcdn_api"
6+
)
7+
8+
func main() {
9+
/**get SecretKey & SecretId from https://console.qcloud.com/capi**/
10+
var Requesturl string = "cdn.api.qcloud.com/v2/index.php"
11+
var SecretKey string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
12+
var Method string = "POST"
13+
14+
/**params to signature**/
15+
params := make(map[string]interface{})
16+
params["SecretId"] = "ooooooooooooooooooooooooooooooo"
17+
params["Action"] = "RefreshCdnUrl"
18+
params["urls.0"] = "http://hello.world.att.oa.com/test1.php"
19+
20+
/*use qcloudcdn_api.Signature to obtain signature and params with correct signature**/
21+
signature, request_params := cdnapi.Signature(SecretKey, params, Method, Requesturl)
22+
fmt.Println("signature : ", signature)
23+
24+
/*use qcloudcdn_api.SendRequest to send request**/
25+
response := cdnapi.SendRequest(Requesturl, request_params, Method)
26+
fmt.Println(response)
27+
}

0 commit comments

Comments
 (0)