diff --git a/Qcloud_CDN_API/go/README.md b/Qcloud_CDN_API/go/README.md index 45fbfe9..3612d33 100644 --- a/Qcloud_CDN_API/go/README.md +++ b/Qcloud_CDN_API/go/README.md @@ -6,31 +6,30 @@ ## 代码示例 ``` -package main - -import ( - "fmt" - cdnapi "qcloudcdn_api" -) - -func main() { - /**get SecretKey & SecretId from https://console.qcloud.com/capi**/ - var Requesturl string = "cdn.api.qcloud.com/v2/index.php" - var SecretKey string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - var Method string = "POST" - - /**params to signature**/ - params := make(map[string]interface{}) - params["SecretId"] = "ooooooooooooooooooooooooooooooo" - params["Action"] = "RefreshCdnUrl" - params["urls.0"] = "http://hello.world.att.oa.com/test1.php" - - /*use qcloudcdn_api.Signature to obtain signature and params with correct signature**/ - signature, request_params := cdnapi.Signature(SecretKey, params, Method, Requesturl) - fmt.Println("signature : ", signature) - - /*use qcloudcdn_api.SendRequest to send request**/ - response := cdnapi.SendRequest(Requesturl, request_params, Method) - fmt.Println(response) +package main + +import ( + "fmt" + cdnapi "qcloudcdn_api" +) + +func main() { + /**get SecretKey & SecretId from https://console.qcloud.com/capi**/ + var Requesturl string = "cdn.api.qcloud.com/v2/index.php" + var SecretKey string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + var Method string = "POST" + + /**params to signature**/ + params := make(map[string]interface{}) + params["SecretId"] = "ooooooooooooooooooooooooooooooo" + params["Action"] = "DescribeCdnHosts" + + /*use qcloudcdn_api.Signature to obtain signature and params with correct signature**/ + request, _ := cdnapi.Signature(SecretKey, params, Method, Requesturl) + fmt.Println("request : ", request) + + /*use qcloudcdn_api.SendRequest to send request**/ + response := cdnapi.SendRequest(Requesturl, request, Method) + fmt.Println(response) } ``` diff --git a/Qcloud_CDN_API/go/qcloudcdn_api/qcloudcdn_api.go b/Qcloud_CDN_API/go/qcloudcdn_api/qcloudcdn_api.go index 0f0a559..d15e1b2 100644 --- a/Qcloud_CDN_API/go/qcloudcdn_api/qcloudcdn_api.go +++ b/Qcloud_CDN_API/go/qcloudcdn_api/qcloudcdn_api.go @@ -40,6 +40,7 @@ func Signature(secretKey string, params map[string]interface{}, method string, r params["Nonce"] = rd /**sort all the params to make signPlainText**/ sigUrl := method + requesturl + "?" + sigParam := "" var keys []string for k := range params { keys = append(keys, k) @@ -49,6 +50,7 @@ func Signature(secretKey string, params map[string]interface{}, method string, r for _, key := range keys { if !isfirst { sigUrl = sigUrl + "&" + sigParam = sigParam + "&" } isfirst = false if strings.Contains(key, "_") { @@ -56,12 +58,13 @@ func Signature(secretKey string, params map[string]interface{}, method string, r } value := typeSwitcher(params[key]) sigUrl = sigUrl + key + "=" + value + sigParam = sigParam + key + "=" + value } fmt.Println("signPlainText: ", sigUrl) unencode_sign, _sign := sign(sigUrl, secretKey) + sigParam = "Signature=" + _sign + "&" + sigParam params["Signature"] = unencode_sign - fmt.Println("unencoded signature: ", unencode_sign) - return _sign, params + return sigParam, params } /** @@ -74,15 +77,20 @@ func Signature(secretKey string, params map[string]interface{}, method string, r *@return params params of qcloud openapi interfac include Signature **/ -func SendRequest(requesturl string, params map[string]interface{}, method string) string { +func SendRequest(requesturl string, params string, method string) string { requesturl = "https://" + requesturl var response string if method == "GET" { - params_str := "?" + ParamsToStr(params) + params_str := "?" + params requesturl = requesturl + params_str response = httpGet(requesturl) } else if method == "POST" { - response = httpPost(requesturl, params) + res, err := httpPost(requesturl, params) + if err != nil { + println(err.Error()) + return err.Error() + } + response = string(res) } else { fmt.Println("unsuppported http method") return "unsuppported http method" @@ -103,23 +111,6 @@ func typeSwitcher(t interface{}) string { } } -func ParamsToStr(params map[string]interface{}) string { - isfirst := true - requesturl := "" - for k, v := range params { - if !isfirst { - requesturl = requesturl + "&" - } - isfirst = false - if strings.Contains(k, "_") { - strings.Replace(k, ".", "_", -1) - } - v := typeSwitcher(v) - requesturl = requesturl + k + "=" + url.QueryEscape(v) - } - return requesturl -} - func sign(signPlainText string, secretKey string) (string, string) { key := []byte(secretKey) hash := hmac.New(sha1.New, key) @@ -149,27 +140,24 @@ func httpGet(url string) string { return string(body) } -func httpPost(requesturl string, params map[string]interface{}) string { - req, err := http.NewRequest("POST", requesturl, strings.NewReader(ParamsToStr(params))) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") - /* - req, err := http.NewRequest("POST", requesturl, strings.NewReader(form.Encode())) - fmt.Println(strings.NewReader(form.Encode())) - */ - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - client := &http.Client{Transport: tr, Timeout: time.Duration(3) * time.Second} - resp, err := client.Do(req) +func httpPost(requesturl string, params string) ([]byte, error) { + client := &http.Client{} + + req, err := http.NewRequest("POST", requesturl, strings.NewReader(params)) if err != nil { - fmt.Println(err) - return err.Error() + return nil, err } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + defer resp.Body.Close() - body, erro := ioutil.ReadAll(resp.Body) - if erro != nil { - fmt.Println("http wrong erro") - return erro.Error() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err } - return string(body) + return body, nil } + diff --git a/Qcloud_CDN_API/go/qcloudcdn_api_demo.go b/Qcloud_CDN_API/go/qcloudcdn_api_demo.go index bd3e398..cd5dd32 100644 --- a/Qcloud_CDN_API/go/qcloudcdn_api_demo.go +++ b/Qcloud_CDN_API/go/qcloudcdn_api_demo.go @@ -14,14 +14,13 @@ func main() { /**params to signature**/ params := make(map[string]interface{}) params["SecretId"] = "ooooooooooooooooooooooooooooooo" - params["Action"] = "RefreshCdnUrl" - params["urls.0"] = "http://hello.world.att.oa.com/test1.php" + params["Action"] = "DescribeCdnHosts" /*use qcloudcdn_api.Signature to obtain signature and params with correct signature**/ - signature, request_params := cdnapi.Signature(SecretKey, params, Method, Requesturl) - fmt.Println("signature : ", signature) + request, _ := cdnapi.Signature(SecretKey, params, Method, Requesturl) + fmt.Println("request : ", request) /*use qcloudcdn_api.SendRequest to send request**/ - response := cdnapi.SendRequest(Requesturl, request_params, Method) + response := cdnapi.SendRequest(Requesturl, request, Method) fmt.Println(response) } diff --git a/Qcloud_CDN_API/nodejs/index.js b/Qcloud_CDN_API/nodejs/index.js index f0cbfe6..67541cc 100644 --- a/Qcloud_CDN_API/nodejs/index.js +++ b/Qcloud_CDN_API/nodejs/index.js @@ -14,6 +14,7 @@ var METHOD = 'POST'; var QcloudSDK = function() { this.secretKey = ''; this.secretId = ''; + this.token = ''; } QcloudSDK.prototype.config = function(userConfig) { @@ -21,18 +22,20 @@ QcloudSDK.prototype.config = function(userConfig) { this.secretKey = userConfig.secretKey; this.secretId = userConfig.secretId; + this.token = userConfig.token; } QcloudSDK.prototype.request = function(actionName, params, callback) { checkUserConfig({ secretKey: this.secretKey, - secretId: this.secretId + secretId: this.secretId, + token: this.token, }) params = params || {}; var timestamp = Math.ceil((new Date()-0)/1000); var nonce = _.random(1000000); - var signature = createSignature(actionName, nonce, timestamp, params, this.secretKey, this.secretId); + var signature = createSignature(actionName, nonce, timestamp, params, this.secretKey, this.secretId, this.token); var requestData = _.assign({ 'Action': actionName, @@ -40,6 +43,7 @@ QcloudSDK.prototype.request = function(actionName, params, callback) { 'Nonce': nonce, 'SecretId': this.secretId, 'Signature': signature, + 'Token': this.token }, params) requestData = commonUtils.serialize(requestData) @@ -67,12 +71,13 @@ function checkUserConfig(userConfig) { } } -function createSignature(actionName, nonce, timestamp, params, secretKey, secretId) { +function createSignature(actionName, nonce, timestamp, params, secretKey, secretId, token) { var originObject = _.assign({ 'Action': actionName, 'Nonce': nonce, 'SecretId': secretId, - 'Timestamp': timestamp + 'Timestamp': timestamp, + 'Token': token }, params); var sortedObject = commonUtils.sortObject(originObject); var serializeString = commonUtils.serialize(sortedObject);