2017-08-31 147 views
0

我目前正在嘗試從salesforce中檢索我的Azure帳戶中可用的列表共享。我想從下面的示例代碼實現的例子:azure REST API通信

https://docs.microsoft.com/en-us/rest/api/storageservices/list-shares#samplerequestandresponse

//private key: access key of my account 
    string storageKey =private key; 
     string storageName = 'accountName'; 
     Datetime dt = Datetime.now(); 
     string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT'; 
     system.debug('formattedDate--'+formattedDate); 

     string CanonicalizedHeaders = 'x-ms-date:'+formattedDate+'\nx-ms-version:2016-05-31'; 
     string CanonicalizedResource = '/' + storageName + '/\ncomp:list'; 
     string StringToSign = 'GET\n\n\n\n\n\n\n\n\n\n\n\n' + CanonicalizedHeaders+'\n'+CanonicalizedResource; 
     system.debug('StringToSign--'+StringToSign); 

     Blob temp = EncodingUtil.base64Decode(storageKey); 
     Blob hmac = Crypto.generateMac('HmacSHA256',Blob.valueOf(StringToSign),temp); //StringToSign 
     system.debug('oo-'+EncodingUtil.base64Encode(hmac)); 
     HttpRequest req = new HttpRequest(); 
     req.setMethod('GET'); 
     //req.setHeader('content-type', 'application/xml'); 
     req.setHeader('x-ms-version','2016-05-31'); 
     req.setHeader('x-ms-date', formattedDate); 
     string signature = EncodingUtil.base64Encode(hmac); 
     string authHeader = 'SharedKey salesforcestrongaccount'+':'+signature; 

     req.setHeader('Authorization',authHeader); 
     req.setEndpoint('https://<accountName>.file.core.windows.net/?comp=list'); 

     Http http = new Http(); 
     HTTPResponse res; 
     res = http.send(req);     
     System.debug(LoggingLevel.INFO, 'http.send result status: ' + res.getStatus()); 

任何幫助嗎?

+0

有你正在計算'stringToSign'的方式問題。請按照此處列出的說明進行操作:https://docs.microsoft.com/zh-cn/rest/api/storageservices/authentication-for-the-azure-storage-services並再次嘗試您的請求。 –

+0

你能否提供任何示例代碼。因爲我嘗試了鏈接中指定的過程。按鈕運氣 – Rv1

+0

不,你沒有按照說明那裏。我快速瀏覽了一下你的代碼,並發現了這一點。我建議你再仔細閱讀文件。否則,如果您搜索Azure存儲REST API示例,我相信您會發現充足的源代碼。 –

回答

1

正如Gaurav Mantri所說,你的stringToSign有問題。所以你會得到這個錯誤。

右共享密鑰認證是這樣的:

StringToSign = 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; 

在這裏,我創建一個測試演示,你可以參考一下吧。

列表份額:

 string storageAccount = "storage account"; 
     string accessKey = "accountkey"; 
     string resourcePath = "?comp=list"; 
     string uri = @"https://" + storageAccount + ".file.core.windows.net/" + resourcePath; 
     // Web request 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
     request.Method = "GET"; 
     request.Headers["x-ms-date"] = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture); 
     request.Headers["x-ms-version"] = "2015-02-21"; 
     String stringToSign = "GET\n" 
      + "\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 
      + "x-ms-date:" + request.Headers["x-ms-date"] + "\nx-ms-version:2015-02-21\n" // headers 
      + "/" + storageAccount + "/" + "\ncomp:list"; // resources 

     System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(accessKey)); 
     string strAuthorization = "SharedKey " + storageAccount + ":" + System.Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(stringToSign))); 


     request.Headers["Authorization"] = strAuthorization; 

     Task<WebResponse> response = request.GetResponseAsync(); 
     HttpWebResponse responseresult = (HttpWebResponse)response.Result; 

     using (System.IO.StreamReader r = new System.IO.StreamReader(responseresult.GetResponseStream())) 
     { 
      string jsonData = r.ReadToEnd(); 
      Console.WriteLine(jsonData); 
     } 

結果:

enter image description here

的Java:

private static final String account = "accountname"; 
    private static final String key = "Key"; 

    public static void main(String args[]) throws Exception { 
//  String urlString = "http://" + account + ".file.core.windows.net/sampleshare/name.txt"; 
     String urlString = "https://" + account + ".file.core.windows.net/?comp=list"; 
     HttpURLConnection connection = (HttpURLConnection) (new URL(urlString)).openConnection(); 
     getFileRequest(connection, account, key); 
     connection.connect(); 
     System.out.println("Response message : " + connection.getResponseMessage()); 
     System.out.println("Response code : " + connection.getResponseCode()); 

     BufferedReader br = null; 
     if (connection.getResponseCode() != 200) { 
      br = new BufferedReader(new InputStreamReader((connection.getErrorStream()))); 
     } else { 
      br = new BufferedReader(new InputStreamReader((connection.getInputStream()))); 
     } 
     System.out.println("Response body : " + br.readLine()); 
    } 

    public static void getFileRequest(HttpURLConnection request, String account, String key) throws Exception { 
     SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); 
     fmt.setTimeZone(TimeZone.getTimeZone("GMT")); 
     String date = fmt.format(Calendar.getInstance().getTime()) + " GMT"; 
     String stringToSign = "GET\n" + "\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 
       + "x-ms-date:" + date + "\nx-ms-version:2015-02-21\n" // headers 
       + "/" + account + request.getURL().getPath() + "\ncomp:list"; // resources 
     System.out.println("stringToSign : " + stringToSign); 
     String auth = getAuthenticationString(stringToSign); 
     request.setRequestMethod("GET"); 
     request.setRequestProperty("x-ms-date", date); 
     request.setRequestProperty("x-ms-version", "2015-02-21"); 
     request.setRequestProperty("Authorization", auth); 
    } 

    private static String getAuthenticationString(String stringToSign) throws Exception { 
     Mac mac = Mac.getInstance("HmacSHA256"); 
     mac.init(new SecretKeySpec(Base64.decode(key), "HmacSHA256")); 
     String authKey = new String(Base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8")))); 
     String auth = "SharedKey " + account + ":" + authKey; 
     return auth; 
    } 
+0

我可以知道您在哪個平臺上執行了此操作嗎?它在java中嗎? #Brando – Rv1

+0

我嘗試了上面的stringtosign格式,但得到了與密鑰相同的錯誤@Brando Thang – Rv1

+1

我更新了Java代碼,請重試。 –