阿里云 API调用

范JX范 2020-08-04 04:45:17
大师们,关于阿里云的API调用,研究了好几天了,还是搞不定。请教大家。代码如下:

# aliyun
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.request import CommonRequest

def getAladdinConf():
options = {
'hzHost': 'xxx.xxx.xxx.xxx',
'AppKey':'xxxxxxxxx',
'AppSecret': 'XXXXX',
'region_id':'cn-shanghai',
'baseURL': 'http://ali-api.XXXXXX-e.com/',
'reqheader': {
'Content-Type': 'application/json',
'Authorization':'Bearer XXXXXXXXXXXXX',
'Cache-Control': 'no-cache'
},
'stock':{'method':'POST', 'url':'index.php/rest/V2/product/stock'}, # 库存
'price':{'method':'POST', 'url':'index.php/rest/V2/product/price'}, # 价格
'update':{'method':'GET', 'url':'index.php/rest/V2/product/update'}, # 产品更新
'new':{'method':'GET', 'url':'index.php/rest/V2/product/new'} # 产品新增
}
return options

def getAladdinRequest(data_type, logfile = False):
result = {'msg':'OK', 'data':''}
curr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
conf = getAladdinConf()
client = AcsClient(conf['AppKey'],conf['AppSecret'],conf['region_id'])
if data_type in conf:
#url = conf['baseURL'] + conf[data_type]['url']
url = 'http://0bbbae2b380a49238d3b7317d2cd7e86-cn-hangzhou.alicloudapi.com/index.php/rest/V2/product/price' #test url address
r = CommonRequest()
r.set_accept_format('json')
r.set_domain(url)
r.set_method('POST')
r.set_action_name('DescribeInstanceStatus')
r.set_version('2014-05-26')
r.add_query_param('RegionId', conf['region_id'])
response = client.do_action(r)
ret = str(response,encoding= 'utf-8')
line = ret
if logfile:
logfile.write('>>- ' + curr + '\n')
logfile.write('... ' + line + '\n')
else:
logfile.write('>>- ' + curr + '\n')
logfile.write('>>>Error, unknown data type: ' + data_type + '\n')
LEN_TH = 41943040 # 40M
if len(line) > LEN_TH:
result['msg'] = 'SIZE_TOO_BIG'
else:
result['data'] = line
return result

if __name__ == "__main__":
getAladdinRequest_result = getAladdinRequest('price')
print (getAladdinRequest_result)
...全文
305 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
范JX范 2020-08-07
  • 打赏
  • 举报
回复
def getAladdinRequest(data_type, request_data = [], logfile = False):
result = {'msg':'OK', 'data':''}
curr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
conf = getAladdinConf()
clt = AcsClient(conf['AppKey'],conf['AppSecret'],conf['region_id'])
if data_type in conf:
#url = conf['baseURL'] + conf[data_type]['url']
url = 'http://0bbbae2b380a49238d3b7317d2cd7e86-cn-hangzhou.alicloudapi.com/index.php/rest/V2/product/price' #test url
headers = conf['reqheader']
#host = conf['hzHost']
print('---> Access to ' + url)
# print('---> Headers: ' + json.dumps(headers))
if conf[data_type]['method'] == 'POST':
payload = json.dumps({"sku":request_data})
req_post = requests.post(url = url, headers = headers,data=payload)

r = clt.do_action_with_exception(req_post) #感觉是这里有问题,大师能否帮忙看看。
line = r.text
else:
r = requests.get(url, headers = headers)
line = r.text
if logfile:
logfile.write('>>- ' + curr + '\n')
logfile.write(' >>> ' + str(request_data) + '\n')
logfile.write('... ' + line + '\n')
else:
logfile.write('>>- ' + curr + '\n')
logfile.write('>>>Error, unknown data type: ' + data_type + '\n')
LEN_TH = 41943040 # 40M
if len(line) > LEN_TH:
result['msg'] = 'SIZE_TOO_BIG'
else:
result['data'] = line
return result

error information:
r = clt.do_action_with_exception(req_post)
File "D:\apps\py36\lib\site-packages\aliyunsdkcore\client.py", line 472, in do_action_with_exception
acs_request.set_accept_format('JSON')
AttributeError: 'Response' object has no attribute 'set_accept_format'

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧