From 94fbfae22d86a81cb6cc7bbf7caa0235b32b0ce0 Mon Sep 17 00:00:00 2001 From: evincai Date: Wed, 5 Apr 2017 17:01:18 +0800 Subject: [PATCH 1/5] Update README.md --- Qcloud_CDN_API/python/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Qcloud_CDN_API/python/README.md b/Qcloud_CDN_API/python/README.md index 4dca341..c2f5e95 100644 --- a/Qcloud_CDN_API/python/README.md +++ b/Qcloud_CDN_API/python/README.md @@ -41,20 +41,20 @@ python QcloudCdnTools_V2.py OnlineHost -u xxxxx -p xxxxxxx --hostId 206092 ## DeleteCdnHost python QcloudCdnTools_V2.py DeleteCdnHost -u xxxxx -p xxxxxxx --hostId 81094 -##GenerateLogList +## GenerateLogList python QcloudCdnTools_V2.py GenerateLogList -u xxxxx -p xxxxxxx --hostId 206092 -##GetCdnRefreshLog +## GetCdnRefreshLog python QcloudCdnTools_V2.py GetCdnRefreshLog -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-04-25 --endDate 2016-04-26 -##GetCdnStatTop +## GetCdnStatTop python QcloudCdnTools_V2.py GetCdnStatTop -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --statType bandwidth --projects 0 --hosts ping.cdn.qcloud.com --hosts ts6.cache.qcloudcdn.com -##GetCdnStatusCode +## GetCdnStatusCode python QcloudCdnTools_V2.py GetCdnStatusCode -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --projects 0 --hosts ping.cdn.qcloud.com --hosts ts6.cache.qcloudcdn.com -##DescribeCdnHostDetailedInfo +## DescribeCdnHostDetailedInfo python QcloudCdnTools_V2.py DescribeCdnHostDetailedInfo -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --projects 0 --hosts ping.cdn.qcloud.com --hosts ts6.cache.qcloudcdn.com --statType bandwidth -##DescribeCdnHostInfo +## DescribeCdnHostInfo python QcloudCdnTools_V2.py DescribeCdnHostInfo -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --projects 0 --statType bandwidth --debug From b5a2e8db50ead78b874da540bf52d302bce6ae5f Mon Sep 17 00:00:00 2001 From: Galen-Yip <275497985@qq.com> Date: Wed, 5 Apr 2017 17:01:39 +0800 Subject: [PATCH 2/5] mod nodejs package --- Qcloud_CDN_API/nodejs/AddCdnHost.js | 141 ++++++++++++++++++++++++++++ Qcloud_CDN_API/nodejs/package.json | 12 +++ Qcloud_CDN_API/python/README.md | 12 +-- 3 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 Qcloud_CDN_API/nodejs/AddCdnHost.js create mode 100644 Qcloud_CDN_API/nodejs/package.json diff --git a/Qcloud_CDN_API/nodejs/AddCdnHost.js b/Qcloud_CDN_API/nodejs/AddCdnHost.js new file mode 100644 index 0000000..109827b --- /dev/null +++ b/Qcloud_CDN_API/nodejs/AddCdnHost.js @@ -0,0 +1,141 @@ +/*需要填写你的密钥,可从 https://console.qcloud.com/capi 获取 SecretId 及 $secretKey*/ +$secretKey='YOUR_SECRET_KEY'; +$secretId='YOUR_SECRET_ID'; +$action='AddCdnHost'; + + + +/*****************参数************************/ +/** + 参数名 类型 是否必填 描述 +host string 是 域名 +hostType string 是 域名类型,(cname,ftp) +projectId int 是 项目id +origin string 否 源站 +**/ + + + + +/*参数*/ +$PRIVATE_PARAMS = array( + "host" => "c.u-ndefined.com", + "projectId" => 0, + "hostType" => "cname", + "origin" => "e.u-ndefinded.com:12023" + ); + +$HttpUrl="cdn.api.qcloud.com"; + +/*除非有特殊说明,如MultipartUploadVodFile,其它接口都支持GET及POST*/ +$HttpMethod="POST"; + +/*是否https协议,大部分接口都必须为https,只有少部分接口除外(如MultipartUploadVodFile)*/ +$isHttps =true; + +/*下面这五个参数为所有接口的 公共参数;对于某些接口没有地域概念,则不用传递Region(如DescribeDeals)*/ +$COMMON_PARAMS = array( + 'Nonce' => rand(), + 'Timestamp' =>time(NULL), + 'Action' =>$action, + 'SecretId' => $secretId, + ); + +/***********************************************************************************/ + + +CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps); + +function CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps) +{ + $FullHttpUrl = $HttpUrl."/v2/index.php"; + + /***************对请求参数 按参数名 做字典序升序排列,注意此排序区分大小写*************/ + $ReqParaArray = array_merge($COMMON_PARAMS, $PRIVATE_PARAMS); + ksort($ReqParaArray); + + /**********************************生成签名原文********************************** + * 将 请求方法, URI地址,及排序好的请求参数 按照下面格式 拼接在一起, 生成签名原文,此请求中的原文为 + * GETcvm.api.qcloud.com/v2/index.php?Action=DescribeInstances&Nonce=345122&Region=gz + * &SecretId=AKIDz8krbsJ5yKBZQ ·1pn74WFkmLPx3gnPhESA&Timestamp=1408704141 + * &instanceIds.0=qcvm12345&instanceIds.1=qcvm56789 + * ****************************************************************************/ + $SigTxt = $HttpMethod.$FullHttpUrl."?"; + + $isFirst = true; + foreach ($ReqParaArray as $key => $value) + { + if (!$isFirst) + { + $SigTxt = $SigTxt."&"; + } + $isFirst= false; + + /*拼接签名原文时,如果参数名称中携带_,需要替换成.*/ + if(strpos($key, '_')) + { + $key = str_replace('_', '.', $key); + } + + $SigTxt=$SigTxt.$key."=".$value; + } + + /*********************根据签名原文字符串 $SigTxt,生成签名 Signature******************/ + $Signature = base64_encode(hash_hmac('sha1', $SigTxt, $secretKey, true)); + + + /***************拼接请求串,对于请求参数及签名,需要进行urlencode编码********************/ + $Req = "Signature=".urlencode($Signature); + foreach ($ReqParaArray as $key => $value) + { + $Req=$Req."&".$key."=".urlencode($value); + } + + /*********************************发送请求********************************/ + if($HttpMethod === 'GET') + { + if($isHttps === true) + { + $Req="https://".$FullHttpUrl."?".$Req; + } + else + { + $Req="http://".$FullHttpUrl."?".$Req; + } + + $Rsp = file_get_contents($Req); + + } + else + { + if($isHttps === true) + { + $Rsp= SendPost("https://".$FullHttpUrl,$Req,$isHttps); + } + else + { + $Rsp= SendPost("http://".$FullHttpUrl,$Req,$isHttps); + } + } + + var_export(json_decode($Rsp,true)); +} + +function SendPost($FullHttpUrl, $Req, $isHttps) +{ + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $Req); + + curl_setopt($ch, CURLOPT_URL, $FullHttpUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + if ($isHttps === true) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } + + $result = curl_exec($ch); + + return $result; +} diff --git a/Qcloud_CDN_API/nodejs/package.json b/Qcloud_CDN_API/nodejs/package.json new file mode 100644 index 0000000..972b623 --- /dev/null +++ b/Qcloud_CDN_API/nodejs/package.json @@ -0,0 +1,12 @@ +{ + "name": "nodejs", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/Qcloud_CDN_API/python/README.md b/Qcloud_CDN_API/python/README.md index 4dca341..c2f5e95 100644 --- a/Qcloud_CDN_API/python/README.md +++ b/Qcloud_CDN_API/python/README.md @@ -41,20 +41,20 @@ python QcloudCdnTools_V2.py OnlineHost -u xxxxx -p xxxxxxx --hostId 206092 ## DeleteCdnHost python QcloudCdnTools_V2.py DeleteCdnHost -u xxxxx -p xxxxxxx --hostId 81094 -##GenerateLogList +## GenerateLogList python QcloudCdnTools_V2.py GenerateLogList -u xxxxx -p xxxxxxx --hostId 206092 -##GetCdnRefreshLog +## GetCdnRefreshLog python QcloudCdnTools_V2.py GetCdnRefreshLog -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-04-25 --endDate 2016-04-26 -##GetCdnStatTop +## GetCdnStatTop python QcloudCdnTools_V2.py GetCdnStatTop -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --statType bandwidth --projects 0 --hosts ping.cdn.qcloud.com --hosts ts6.cache.qcloudcdn.com -##GetCdnStatusCode +## GetCdnStatusCode python QcloudCdnTools_V2.py GetCdnStatusCode -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --projects 0 --hosts ping.cdn.qcloud.com --hosts ts6.cache.qcloudcdn.com -##DescribeCdnHostDetailedInfo +## DescribeCdnHostDetailedInfo python QcloudCdnTools_V2.py DescribeCdnHostDetailedInfo -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --projects 0 --hosts ping.cdn.qcloud.com --hosts ts6.cache.qcloudcdn.com --statType bandwidth -##DescribeCdnHostInfo +## DescribeCdnHostInfo python QcloudCdnTools_V2.py DescribeCdnHostInfo -u xxxxxxxxxxxx -p xxxxxxxxxxxx --startDate 2016-05-08 --endDate 2016-05-09 --projects 0 --statType bandwidth --debug From 1f0c5b43d36396e3ffc1237ea73e3c3783e4cea8 Mon Sep 17 00:00:00 2001 From: Galen-Yip <275497985@qq.com> Date: Mon, 17 Apr 2017 14:41:49 +0800 Subject: [PATCH 3/5] mod readme --- Qcloud_CDN_API/nodejs/package.json | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Qcloud_CDN_API/nodejs/package.json b/Qcloud_CDN_API/nodejs/package.json index bdf9434..cf88569 100644 --- a/Qcloud_CDN_API/nodejs/package.json +++ b/Qcloud_CDN_API/nodejs/package.json @@ -1,16 +1,4 @@ { -<<<<<<< HEAD - "name": "nodejs", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC" -======= "name": "qcloud-cdn-node-sdk", "version": "1.0.0", "description": "腾讯云CDN OpenAPI Node.js SDK", @@ -36,5 +24,4 @@ ], "author": "QcloudCDN", "license": "MIT" ->>>>>>> 35ab9beb20e69594d7044a9a4dddf5a99d09ff68 } From 98f532ca7eb93dbc5f577e70e572f1fdc738a185 Mon Sep 17 00:00:00 2001 From: Galen-Yip <275497985@qq.com> Date: Wed, 24 Apr 2019 19:14:16 +0800 Subject: [PATCH 4/5] =?UTF-8?q?[update]=20nodejs=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E5=AF=86=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Qcloud_CDN_API/nodejs/index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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); From 58a84117b4626575fee00480b549179f48505817 Mon Sep 17 00:00:00 2001 From: alieliu Date: Thu, 5 Sep 2019 15:22:01 +0800 Subject: [PATCH 5/5] fix golang demo post bug --- Qcloud_CDN_API/go/README.md | 51 +++++++------- .../go/qcloudcdn_api/qcloudcdn_api.go | 70 ++++++++----------- Qcloud_CDN_API/go/qcloudcdn_api_demo.go | 9 ++- 3 files changed, 58 insertions(+), 72 deletions(-) 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) }