简体   繁体   中英

How to access Azure Storage Table using REST API (ARM)

By looking following link it's not clear what the signature should contain or how to form the canonical string to create signature which can be encrypted using HMAC-SH256 alogrithm.

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

I am using following URL

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

Headers:

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

Getting following error:

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

@Prit, Please see the code below for generating the shared key lite of table storage as reference.

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);
// HMAC-SHA@%^
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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM