Java - DocumentDB未经授权的访问

时间:2017-03-26 08:45:40

标签: java azure azure-cosmosdb

我正在尝试编写一个功能来存储来自Azure的 DocumentDB 中的对象。

我有以下代码:

public void saveEvent(Event event) throws DocumentClientException {
        Document document = new Document(JsonCreator.createJson(event));

        //check if document already exists
        FeedOptions feedOptions = new FeedOptions();
        feedOptions.setEnableCrossPartitionQuery(true);
        FeedResponse<Document> eventDocument = documentClient.queryDocuments(COLLECTION_LINK, String.format(SELECT_DOCUMENT, event.getId()), feedOptions);

        // if there is a document with the ID then replace
        if (eventDocument.getQueryIterator().hasNext()) {
            //documentClient.replaceDocument(COLLECTION_LINK, document, null);
            documentClient.replaceDocument(COLLECTION_LINK,  document, null);
        }
        else {
            documentClient.createDocument(COLLECTION_LINK, document, null, false);
        }
    }

如果event不存在(表示数据库中没有id event的记录),则调用createDocument。如果记录已存在于数据库中,则调用replaceDocument

    调用
  • createDocument时没有问题,文档在数据库中创建
  • replaceDocument抛出StatusCode: Unauthorized exception

com.microsoft.azure.documentdb.DocumentClientException: The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'put colls dbs/sporteventsdb/colls/sportevents sun, 26 mar 2017 08:32:41 gmt

完整筹码:http://pastebin.com/YVGwqLkH

代码中使用的常量:

  • COLLECTION_LINK = "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID";
  • SELECT_DOCUMENT = "SELECT * FROM " + DATABASE_ID + " WHERE " + DATABASE_ID + ".id = \"%d\"";

我正在使用Spring IntelliJ IDEA Ubuntu Java 8与[{1}} @Override public void onSaveInstanceState(Bundle savedInstanceState) { int index = llm.findFirstCompletelyVisibleItemPosition();//llm is my layoutmanager savedInstanceState.putInt("Pos", index); super.onSaveInstanceState(savedInstanceState); } 进行开发。

1 个答案:

答案 0 :(得分:2)

您收到错误是因为DocumentDB&#39; replaceDocumentdocumentLink作为参数,而不是collectionLink。 Javadoc在这里:http://azure.github.io/azure-documentdb-java/