Java - DocumentDB未经授权的访问 [英] Java - DocumentDB Unauthorized access

查看:320
本文介绍了Java - DocumentDB未经授权的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a functionality to store an object in DocumentDB from Azure.

我有以下代码:

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);
        }
    }

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

If the event does not exist (means that there is no record in database with the id of event ) then createDocument is called. If the record already exists in the database then replaceDocument is called.


  • <$调用c $ c> createDocument 没有问题,并在数据库中创建文档

  • replaceDocument throws StatusCode:未经授权的异常

  • createDocument is called without problem and document is created in the database
  • replaceDocument throws StatusCode: Unauthorized exception

com.microsoft.azure .documentdb.DocumentClientException:输入授权令牌无法提供请求。请检查是否按照协议构建了预期的有效负载,并检查正在使用的密钥。服务器使用以下有效负载进行签名:'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 \;

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

我正在开发 Spring 框架 IntelliJ IDEA on Ubuntu with Java 8

I'm developing with Spring framework on IntelliJ IDEA on Ubuntu with Java 8.

推荐答案

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

You're getting the error because DocumentDB's replaceDocument takes the documentLink as an argument, no the collectionLink. Javadoc is here: http://azure.github.io/azure-documentdb-java/

这篇关于Java - DocumentDB未经授权的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆