【问题标题】:Java - DocumentDB Unauthorized accessJava - DocumentDB 未经授权的访问
【发布时间】:2017-03-26 08:45:39
【问题描述】:

我正在尝试编写一个功能来将对象存储在 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 不存在(意味着数据库中没有idevent 的记录),则调用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 IDEAUbuntuJava 8 上进行开发。

【问题讨论】:

    标签: java azure azure-cosmosdb


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2016-08-12
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多