为cosmos DB构造哈希令牌签名

时间:2019-05-24 00:48:23

标签: azure azure-cosmosdb

我正在尝试创建用于在cosmos db中创建文档的散列令牌,但我总是得到405方法不允许。任何人都可以告诉我我提供的以下格式是正确的吗?

url = https://cosmos-test-cache.documents.azure.com/dbs/FamilyDB/colls/FamilyCollection/docs/123456
ResourceLink = /dbs/FamilyDB/colls/FamilyCollection/docs/123456
ResourceType = docs
HTTP verb = POST

Response

{
  "code": "MethodNotAllowed",
  "message": "RequestHandler.Post"
}

2 个答案:

答案 0 :(得分:0)

如果您看到官方的cosmos db rest api,则会发现“创建文档”的网址示例为https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs

enter image description here

但是,您的网址是

https://cosmos-test-cache.documents.azure.com/dbs/FamilyDB/colls/FamilyCollection/docs/123456用于Get Document,而不用于创建文档。因此,它被标识为Get请求,而不是POST请求。

然后发生错误405 "MethodNotAllowed"


更新:

让我修复它。

如果使用sdk,则可以设置disableAutomaticIdGeneration属性以避免设置id。它将自动为您生成id。

赞:

enter image description here

但是根据rest api document,id属性是必需的。在未指定ID值的情况下创建文档时,会自动添加ID字段。

enter image description here


请参考示例Java其余代码:

import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.json.JSONObject;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class CreateDocumentRest {

    private static final String account = "***";
    private static final String key = "***";


    public static void main(String args[]) throws Exception {

        String urlString = "https://" + account + ".documents.azure.com/dbs/db/colls/coll/docs";

        //prepare for the json body
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "A");
        jsonObject.put("id", "123");

        String jsonStr = jsonObject.toString();
        String encoding = "UTF-8";
        System.out.println(jsonStr);
        byte[] data = jsonStr.getBytes(encoding);

        HttpURLConnection conn = (HttpURLConnection) (new URL(urlString)).openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        getFileRequest(conn, data);
        OutputStream outStream = conn.getOutputStream();

        outStream.write(data);
        outStream.flush();
        outStream.close();
        System.out.println(conn.getResponseCode());
        System.out.println(conn.getResponseMessage());


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

    public static void getFileRequest(HttpURLConnection request, byte[] data)
            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 = "POST".toLowerCase() + "\n"
                + "docs".toLowerCase() + "\n"
                + "dbs/db/colls/coll" + "\n"
                + date.toLowerCase() + "\n"
                + "" + "\n";
        System.out.println("stringToSign : " + stringToSign);
        String auth = getAuthenticationString(stringToSign);

        request.setRequestMethod("POST");
        request.setRequestProperty("x-ms-date", date);
        request.setRequestProperty("x-ms-version", "2017-02-22");
        request.setRequestProperty("Authorization", auth);
        request.setRequestProperty("Content-Length", String.valueOf(data.length));
        request.setRequestProperty("Content-Type", "application/json");

    }

    private static String getAuthenticationString(String stringToSign) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(Base64.decode(key), "HmacSHA256"));
        String authKey = Base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8")));
        System.out.println("authkey:" + authKey);
        String auth = "type=master&ver=1.0&sig=" + authKey;
        auth = URLEncoder.encode(auth);
        System.out.println("authString:" + auth);
        return auth;
    }

}

根据我的观察,请将ResourceLink调整为dbs/FamilyDB/colls/FamilyCollection,将url调整为https://cosmos-test-cache.documents.azure.com/dbs/FamilyDB/colls/FamilyCollection/docs

答案 1 :(得分:0)

要添加到上述答案中,以用于创建哈希令牌的帖子调用

Resource method:Post
Resource Id:dbs/{dbs}/colls/{colls}
Resource Type:docs
and the url should be
dbs/{dbs}/colls/{colls}/docs