Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
can';t通过python连接到azure文件服务REST api_Rest_Azure_Python Requests_Azure Storage_Azure Storage Files - Fatal编程技术网

can';t通过python连接到azure文件服务REST api

can';t通过python连接到azure文件服务REST api,rest,azure,python-requests,azure-storage,azure-storage-files,Rest,Azure,Python Requests,Azure Storage,Azure Storage Files,我想通过python请求访问我的文件服务,我不熟悉REST和python请求。 我的意见是 headers= {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongstorageaccount:codecodecodeFiTzubX9tvC3G3PcDYzR2cX/TMjkOu4JhsvQffS+xTDDBQ==', 'x-ms-version': '2017-07-29'} url = 'ht

我想通过python请求访问我的文件服务,我不熟悉REST和python请求。 我的意见是

headers= {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongstorageaccount:codecodecodeFiTzubX9tvC3G3PcDYzR2cX/TMjkOu4JhsvQffS+xTDDBQ==', 'x-ms-version': '2017-07-29'}
url = 'https://zedongstorageaccount.file.core.windows.net/?comp=list'
r=requests.get(url,headers=headers)
但是获取错误,r.content的输出:

b'\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:ef98f282-f01a-0042-3e24-d62397000000\nTime:2018-04-17T08:16:21.9635335Z</Message><AuthenticationErrorDetail>The Date header in the request is incorrect.</AuthenticationErrorDetail></Error>'
我还尝试了标题
{'x-ms-date':'2018-04-17 06:22:15.181620','Authorization':'SharedKey ZedongNewsStorage帐户:NJYYtabOIj5D1R+XB0PPMXRJCLDF6NA6OLKYREAKFIZUBX9TVC3PCDYZR2Cx/TMJKOU4JHSVQFs+xTDDBQ=','x-ms-version':'2017-07-29'
,因为我不知道
授权
账户
是否在同一行。我还尝试了许多版本的
x-ms-version
。 但所有400或403人都有反应

我读过这本书,但很困惑


错误在哪里?还有,我是否可以通过Azure REST API学习构建我的应用程序?(我使用谷歌关键词,所有页面都是关于构建REST API和Azure官方文档的)

我也建议使用Python SDK,但出于教育目的:

导入请求
导入日期时间
进口hmac
导入hashlib
导入base64
存储帐户名称='teststorageaccount'
存储\帐户\密钥='D4x6YChpyfqWk9a……'
api_版本='2016-05-31'
请求时间=datetime.datetime.utcnow().strftime(“%a,%d%b%Y%H:%M:%S GMT”)
字符串参数={
“动词”:“GET”,
“内容编码”:“,
“内容语言”:“,
“内容长度”:“,
“Content-MD5”:“,
“内容类型”:“,
“日期”:“,
'如果自':''起修改,
“如果匹配”:“,
“如果没有匹配项”:”,
'如果自':''起未修改,
“范围”:“,
“CanonicalizedHeaders”:“x-ms-date:”+请求时间+”\nx ms版本:“+api\U版本+”\n“,
“CanonicalizedResource”:“/”+存储\帐户\名称+”/\n编码:列表”
}
字符串到符号=(字符串参数['verb']+'\n'
+字符串参数['Content-Encoding']+'\n'
+字符串参数['Content-Language']+'\n'
+字符串参数['Content-Length']+'\n'
+字符串参数['Content-MD5']+'\n'
+字符串参数['Content-Type']+'\n'
+字符串参数['Date']+'\n'
+字符串参数['If-Modified-Since']+'\n'
+字符串参数['If-Match']+'\n'
+字符串参数['If-None-Match']+'\n'
+字符串参数['If-Unmodified-Since']+'\n'
+字符串参数['Range']+'\n'
+字符串_参数['CanonicalizedHeaders']
+字符串_参数['CanonicalizedResource'])
signed_string=base64.b64encode(hmac.new(base64.b64decode(存储帐户密钥),msg=string_to_sign.encode('utf-8'),digestmod=hashlib.sha256)。digest()).decode()
标题={
“x-ms-date”:请求时间,
“x-ms-version”:api_版本,
'授权':('SharedKey'+存储\帐户\名称+':'+签名\字符串)
}
url=('https://'+存储\帐户\名称+'.file.core.windows.net/?comp=list')
r=requests.get(url,headers=headers)
印刷品(r.content)
输出:

myfileshareTue,2018年4月17日13:43:00 GMT“0x8D5A46922CA8BDA”10

您是否在
授权
标题中使用帐户密钥?是,帐户存储密钥。错了吗?我应该用什么?是的,它是不正确的。实际上,您需要计算授权标头步骤,如下所述:。我只是好奇,为什么你不在Azure存储中使用Python SDK而不是直接使用REST API?Gaurav是对的,为什么不使用Python SDK而不是直接调用REST API@张泽栋 您有Azure Storage Python SDK GitHub repo中提供的示例,我建议您可以重新生成帐户密钥,如果它是真实密钥。@TomSun我已重新生成密钥,并从Begging中输入了存储帐户的假名称,这是为了更好地演示,但感谢您的关心!