2014-10-30 87 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