Java – DocumentDB未经授权的访问
我正在尝试编写一个function来从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 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
不存在(意味着数据库中没有event
id
的记录),则调用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\"";
我正在使用Java 8
在Ubuntu
上的IntelliJ IDEA
上使用Spring
框架进行开发。
您收到错误是因为DocumentDB的replaceDocument
将documentLink
作为参数,而不是collectionLink。 Javadoc在这里: http : //azure.github.io/azure-documentdb-java/