积极答复者
Azure REST API中的Authorization中的头标到底该怎么写

问题
-
Authorization中的头标到底该怎么写
官方给出的格式如下
Authorization="[SharedKey|SharedKeyLite] <AccountName>:<Signature>"
主要是<Signature>部分,官方给出的生成方法如下
Signature=Base64(HMAC-SHA256(UTF8(StringToSign)))
如此,在php中,生成HMAC-SHA256的函数是(参考了PHP: hash_hmac - Manual
hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = false ] )
我是这么使用的
hash_hmac('sha256',$str,'');
那么问题来了,这种函数需要的第三个参数是一个key,不然无法生成结果。这里的key到底要填啥??
答案
-
Hi,
经过我的测试,有些地方还是需要修改的,如果您是需要按照文档:https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx 列出容器的话,首先看我能工作的代码:
<?php $preString = "GET\n\n\n\n\n\n\n\n\n\n\n\n"; $headString = "x-ms-date:Thu, 09 Jun 2016 07:14:04 GMT\nx-ms-version:2014-02-14\n"; $resString = "/<account>/\ncomp:list"; $str = utf8_encode($preString.$headString.$resString); $Out = base64_encode(hash_hmac('sha256',$str,base64_decode(<account key>'),true)); echo $Out ?>
请将上面<account>,<account key>替换成您的相应的值,当然x-ms-date也是需要修改的。特别需要注意我加粗的部分。下图是我请求的截图:
下图是返回结果:
我是在国际版中测试的,中国版只需要修改下请求URL即可。
如果您希望能使用PHP Azure SDK的话,您也可以将您之前遇到的问题在新的帖子中提出来。
Best Regards,
Jambor
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 Jambor yaoMicrosoft employee, Moderator 2016年6月9日 7:34
- 已建议为答案 Jambor yaoMicrosoft employee, Moderator 2016年6月10日 1:33
- 已标记为答案 Jambor yaoMicrosoft employee, Moderator 2016年6月15日 11:27
全部回复
-
尝试着把key字段换成了访问密钥,用php生成的$Out就是需要的Signature,大神们看看这样行不,这样生成的Signature还是无法使用。
<?php $preString = "GET\n\n\n\n\n\n\n\n\n\n\n\n"; $headString = "x-ms-date:Sun, 22 May 2016 16:50:38 GMT\nx-ms-version:2014-02-14\n"; $resString = "/uroxoscna/main\ncomp:list\nrestype:container"; $str = utf8_encode($preString.$headString.$resString); $Out = base64_encode(hash_hmac('sha256',$str,'******AL6hV87zEwyflurXXOrjKmFALuNKCODPMi4SlJfaaPH39p9LRZM95NoE46TWXUFmeDhJn8Tpe3Hnas/Q==')); echo $Out;
-
Hi,
从您的描述来看您是希望通过Rest API 的方式来使用Azure存储,您所疑问的key就是存储账号的密钥,我们可以在Azure门户网站Azure存储账户中找到这个值。
>>这样生成的Signature还是无法使用
从您提供的代码中$preString,$headString,$restring,貌似都没有问题,第二个回复的图中Authrization的签名部分是使用获取到的$Out吗?
顺便提一下,Azure PHP的SDK提供了我们更为简单的操作Azure存储的方式,您可以去官网下载并使用:https://www.azure.cn/zh-cn/downloads
希望能帮助到您,如果还有任何问题,欢迎继续来我们论坛提问。
Best Regards,
Jambor
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 Jambor yaoMicrosoft employee, Moderator 2016年5月22日 13:13
-
Hi,
经过我的测试,有些地方还是需要修改的,如果您是需要按照文档:https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx 列出容器的话,首先看我能工作的代码:
<?php $preString = "GET\n\n\n\n\n\n\n\n\n\n\n\n"; $headString = "x-ms-date:Thu, 09 Jun 2016 07:14:04 GMT\nx-ms-version:2014-02-14\n"; $resString = "/<account>/\ncomp:list"; $str = utf8_encode($preString.$headString.$resString); $Out = base64_encode(hash_hmac('sha256',$str,base64_decode(<account key>'),true)); echo $Out ?>
请将上面<account>,<account key>替换成您的相应的值,当然x-ms-date也是需要修改的。特别需要注意我加粗的部分。下图是我请求的截图:
下图是返回结果:
我是在国际版中测试的,中国版只需要修改下请求URL即可。
如果您希望能使用PHP Azure SDK的话,您也可以将您之前遇到的问题在新的帖子中提出来。
Best Regards,
Jambor
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 Jambor yaoMicrosoft employee, Moderator 2016年6月9日 7:34
- 已建议为答案 Jambor yaoMicrosoft employee, Moderator 2016年6月10日 1:33
- 已标记为答案 Jambor yaoMicrosoft employee, Moderator 2016年6月15日 11:27