2014-10-30 48 views
1

任何人都可以發佈一些代碼片段來使用java訪問Azure DocumentDB嗎?使用java訪問Azure documentDB

我能夠理解如何密鑰字符串簽署 StringToSign =動詞+ 「\ n」 +的ResourceType + 「\ n」 + RESOURCEID + 「\ n」 + X-MS-日期+ 「\ n」 +
date +「\ n」;

+1

你看過這裏給出的例子(雖然它在Node.js中):http://msdn.microsoft.com/en-us/library/azure/dn783368.aspx。 – 2014-10-30 15:09:45

回答

0

下面是http://msdn.microsoft.com/en-us/library/azure/dn783368.aspx基於JavaScript例如,Java代碼片斷:

public static String generateAuthHeader(String verb, String resourceId, 
     String resourceType, String date, String masterKeyBase64) 
     throws Exception { 

    // Decode the master key and setup the MAC object for signing. 
    byte[] masterKeyBytes = Base64.decodeBase64(masterKeyBase64.getBytes()); 
    Mac mac = Mac.getInstance("HMACSHA256"); 
    mac.init(new SecretKeySpec(masterKeyBytes, "HMACSHA256")); 

    // Build the unsigned authorization string. 
    String stringToSign = verb + "\n" 
      + resourceType + "\n" 
      + resourceId + "\n" 
      + date + "\n" 
      + "\n"; 

    // Sign and encode the authorization string. 
    String signature = Base64.encodeBase64String(mac.doFinal(stringToSign.toLowerCase().getBytes())); 

    // Generate the authorization header. 
    String authHeader = URLEncoder.encode("type=master&ver=1.0&sig=" + signature, "UTF-8"); 

    return authHeader; 
} 

我使用Apache Commons Codec爲Base64編碼/解碼。順便說一下,我對涉及DocumentDB和Java的項目非常感興趣...如果您希望對您的項目有所幫助,請隨時與我聯繫(andrl {at} microsoft.com) !

編輯:自該問題得到解答以來,DocumentDB Java SDK已發佈。退房:https://github.com/Azure/azure-documentdb-java