1

任何人都可以发布一些代码片段以使用 java 访问 Azure DocumentDB 吗?

我能够理解如何键入字符串来签署 StringToSign = VERB + "\n" + ResourceType + "\n" + ResourceId + "\n" + x-ms-date + "\n" +
date + “\n ”;

4

1 回答 1

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

于 2014-10-30T23:15:51.043 回答