2016-07-28 118 views
0

通过查看以下链接,不清楚签名应该包含什么或者如何形成规范字符串以创建可使用HMAC-SH256算法加密的签名。如何使用REST API(ARM)访问Azure存储表

https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx

我使用以下网址

GET https://mystorageaccount.table.core.windows.net/Tables

页眉:

Authorization SharedKeyLite mystorrageaccount:<<encrypted signature>> 
x-ms-date Thu 28 Jul 2016 11:19:33 GMT 

获得以下错误:

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

+0

您是否尝试使用其中一个客户端SDK?这可能会更容易,除非你的语言/环境没有一个。 – cdelmas

+0

我正在使用REST API和Java。我认为Java SDK不支持从ARM存储表中获取数据。仅支持经典存储。 – Prit

+2

从Classic或ARM存储帐户获取数据没有区别。您可以使用Java SDK。所有你需要的是帐户名和密钥来获取数据。试一试。 –

回答

0

@Prit,请参阅下面的代码以生成表存储的共享密钥文件作为参考。

import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Locale; 
import java.util.TimeZone; 

import javax.crypto.Mac; 
import javax.crypto.spec.SecretKeySpec; 

import org.apache.commons.codec.binary.Base64; 

String secret = "<storage-account-key>"; 
// Date for string to sign 
Calendar calendar = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US); 
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); 
String date = sdf.format(calendar.getTime()); 
// canonicalizedResource, such as "/testaccount1/Tables" 
String canonicalizedResource = "<Canonicalized-Resource>"; 
String stringToSign = date + "\n" + canonicalizedResource; 
System.out.println(stringToSign); 
// [email protected]%^ 
Mac sha256HMAC = Mac.getInstance("HmacSHA256"); 
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); 
sha256HMAC.init(secretKey); 
String hash = Base64.encodeBase64String(sha256HMAC.doFinal(stringToSign.getBytes())); 
System.out.println(hash); 
+0

感谢Peter的代码。我的问题是关于''的内容。我尝试了各种组合来创建,但生成的密钥无法验证,并且始终给出上述相同的错误。 – Prit