場(chǎng)景描述
下文內(nèi)使用的腳本,主要應(yīng)用場(chǎng)景為:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-832460.html
在日常使用阿里云服務(wù)時(shí),獲取數(shù)據(jù)時(shí),一般都需要調(diào)用openapi,下面的腳本為調(diào)用阿里云直播接口的腳本,如大家并非使用直播產(chǎn)品的接口,需做以下準(zhǔn)備:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-832460.html
- 查詢接口版本,示例:‘Version’:‘2016-11-01’。
- 阿里云AK的RAM權(quán)限。
- 其他產(chǎn)品的地址,示例:http://live.aliyuncs.com為直播產(chǎn)品地址。
- 阿里云的openapi一般情況下同時(shí)支持GET和POST請(qǐng)求,但是也有例外的,需注意請(qǐng)求方式。
- action_json 內(nèi)的所有參數(shù)需要更改為openapi調(diào)用時(shí)的參數(shù),此接口只有兩個(gè)參數(shù),如有其他參數(shù),需增加一起輸入。
腳本如下
# -*- coding: utf-8 -*-
import base64
import hmac
import json
import time
import sys
import requests
import urllib.parse
from pprint import pprint
from datetime import datetime,timedelta
from hashlib import sha1
class openapi_debug():
def __init__(self,HttpMethod:str,action_json:dict):
self.base_json = {
'Format':'JSON',
'Version':'2016-11-01',#接口版本
'SignatureMethod':'HMAC-SHA1',
'AccessKeyId':'xxxx',#AK
'SignatureNonce': str(int(time.time()*10000)),
'SignatureVersion': '1.0'
}
self.HttpMethod = HttpMethod
self.access_key_secret = 'xxxx'#secret
self.base_json.update(action_json)
def get_utc_time(self):
now_time = datetime.now()
utc_time = now_time - timedelta(hours=8) # UTC只是比北京時(shí)間提前了8個(gè)小時(shí)
utc_time = utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")
return utc_time
def percentEncode(self,str_v):
res = urllib.parse.quote(str(str_v).encode('utf8').decode(sys.stdin.encoding), '')
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return res
def get_all_param(self):
Timestamp = self.get_utc_time()
self.base_json['Timestamp'] = Timestamp
sortedD = sorted(self.base_json.items(), key=lambda x: x[0])
canstring = ''
for k,v in sortedD:
canstring += '&' + self.percentEncode(k) + '=' + self.percentEncode(v)
stringToSign = self.HttpMethod+ '&%2F&' + self.percentEncode(canstring[1:])
h = hmac.new((self.access_key_secret+'&').encode('utf-8'), stringToSign.encode('utf-8'), sha1)
signature = base64.encodebytes(h.digest()).strip()
self.base_json['Signature'] = str(signature,encoding='utf-8')
return self.base_json
class action_debug():
def __init__(self,HttpMethod,base_json):
self.base_json = base_json
self.HttpMethod = HttpMethod
def action_run(self):
# 這里需要注意下下面的url內(nèi)的地址,這里是直播調(diào)用,所以是live.aliyuncs.com,如果是其他產(chǎn)品,需要更改地址
url = 'http://live.aliyuncs.com/?' + urllib.parse.urlencode(self.base_json)
if self.HttpMethod == 'GET':
res = requests.get(url=url, json=self.base_json)
pprint(json.loads(res.text))
elif self.HttpMethod == 'POST':
res = requests.post(url=url,json=self.base_json)
pprint(json.loads(res.text))
if __name__ == '__main__':
# action_json 內(nèi)的所有參數(shù)需要更改為openapi調(diào)用時(shí)的參數(shù),此接口只有兩個(gè)參數(shù),如有其他參數(shù),需增加一起輸入。
action_json = {
'Action': 'xxxx', # 接口名
'StartTime': str(int(time.time())),
'EndTime': str(int(time.time() + 3600)),
HttpMethod = 'POST' # GET/POST
base_debug = openapi_debug(HttpMethod,action_json)
base_json = base_debug.get_all_param()
action_debug = action_debug(HttpMethod,base_json)
action_debug.action_run()
到了這里,關(guān)于python3調(diào)用阿里云openapi腳本 - 生產(chǎn)環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!