iT邦幫忙

3

python 的 requests --- 已搞定 ...

  • 分享至 

  • xImage

看原廠範例是 shell

#!/bin/bash
username="example_username"
apiKey="example_apiKey"
date=`env LANG="en_US.UTF-8" date -u "+%a, %d %b %Y %H:%M:%S GMT"`
password=`echo -en "$date" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64`
curl -i --url "https://open.chinanetcenter.com/api/report/domainflow?datefrom=2017-11-07T00:00:00%2B08:00&dateto=2017-11-07T00:15:00%2B08:00&type=fiveminutes" \
-X "POST" \
-u "$username:$password" \
-H "Date: $date" \
-H "Accept: application/json" \
-d '<?xml version="1.0" encoding="utf-8"?>
<domain-list>
  <domain-name>www.example1.com</domain-name>
</domain-list>
'

試著換成 python ... 沒成功 出現 400 錯誤幫忙看是哪邊問題

import requests
import datetime
import hmac
import hashlib
import base64
username = "example_username"
apiKey= 'example_apiKey'
now = datetime.datetime.now()
nowTime = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
nowTime_bytes = bytes(nowTime, encoding='utf-8')
value = hmac.new(apiKey, nowTime_bytes, hashlib.sha1).digest()
token = base64.b64encode(value).rstrip()
headers = {"Accept": "application/json",
           "Date": nowTime}
url = 'https://open.chinanetcenter.com/api/report/domainflow'
payload= {'datefrom':'2017-11-07T00:00:00%2B08:00',
          'dateto':'2017-11-07T00:15:00%2B08:00',
          'type': 'fiveminutes'}
xml = '<?xml version="1.0" encoding="utf-8"?>
 <domain-list>
   <domain-name>www.example1.com</domain-name>
 </domain-list>
 '

r = requests.post(url, headers=headers, auth=(username, token), data=xml,params=payload)
print (r.text)

輸出 {"code":"InvalidDatePeriod","message":"The date specified is invalid."}
不會改 ...

改好了 ... 原來是 %2B 改成 +

import requests
import datetime
import hmac
import hashlib
import base64
username = "example_username"
apiKey= 'example_apiKey'
now = datetime.datetime.now()
nowTime = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
nowTime_bytes = bytes(nowTime, encoding='utf-8')
value = hmac.new(apiKey, nowTime_bytes, hashlib.sha1).digest()
token = base64.b64encode(value).rstrip()
headers = {"Accept": "application/json",
           "Date": nowTime}
url = 'https://open.chinanetcenter.com/api/report/domainflow'
payload= {'datefrom':'2017-11-07T00:00:00+08:00',
          'dateto':'2017-11-07T00:15:00+08:00',
          'type': 'fiveminutes'}
xml = '<?xml version="1.0" encoding="utf-8"?>
 <domain-list>
   <domain-name>www.example1.com</domain-name>
 </domain-list>
 '

r = requests.post(url, headers=headers, auth=(username, token), data=xml,params=payload)
print (r.text)

是參考
Convert curl syntax to Python, Ansible URI, Node.js, R, PHP, Strest, Go, Dart, JSON, Rust

https://curl.trillworks.com/

看更多先前的討論...收起先前的討論...
echochio iT邦高手 1 級 ‧ 2019-11-08 22:00:00 檢舉
哈我鬼打牆了
原廠範例是 XML 加上 params 不知如何改成 python
ccutmis iT邦高手 2 級 ‧ 2019-11-08 23:00:57 檢舉
請問這個是要作啥用的?
echochio iT邦高手 1 級 ‧ 2019-11-08 23:17:00 檢舉
CDN API
dragonH iT邦超人 5 級 ‧ 2019-11-09 13:15:50 檢舉
沒其他的error message 嗎
echochio iT邦高手 1 級 ‧ 2019-11-09 13:17:18 檢舉
附上了
{"code":"InvalidDatePeriod","message":"The date specified is invalid."}
應該是 params 參數沒有傳回去
echochio iT邦高手 1 級 ‧ 2019-11-09 13:25:04 檢舉
搞定了 ....
感謝 大家 ....
原來是

2017-11-07T00:00:00%2B08:00

改成

2017-11-07T00:00:00+08:00
dragonH iT邦超人 5 級 ‧ 2019-11-09 13:57:49 檢舉
噗 看起來 message 說得很清楚XD
看起來應該是urlencode/urldecode的問題,當初還是資訊菜鳥時也常遇到! XD
只是我以前是在處理get請求時忘記把字符做urlencode,剛好跟你相反!XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答