Токен авторизации ввода не может обслуживать запрос. (azure -documentdb- java) - PullRequest
0 голосов
/ 10 февраля 2020

При создании программы для подключения к cosmos db с использованием Java произошла ошибка, и я задал вопрос.

Когда я выполнил исходный код по следующему URL-адресу, произошла следующая ошибка, связанная с аутентификацией .

  • сообщение об ошибке
Exception in thread "main" 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: 

  • источник

https://github.com/Azure/azure-documentdb-java#eclipse

import java.io.IOException;
import java.util.List;

import com.google.gson.Gson;
import com.microsoft.azure.documentdb.ConnectionPolicy;
import com.microsoft.azure.documentdb.ConsistencyLevel;
import com.microsoft.azure.documentdb.Database;
import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.documentdb.RequestOptions;

public class HelloWorld {
    // Replace with your DocumentDB end point and master key.
    private static final String END_POINT = "[YOUR_ENDPOINT_HERE]";
    private static final String MASTER_KEY = "[YOUR_KEY_HERE]";

    // Define an id for your database and collection
    private static final String DATABASE_ID = "TestDB";
    private static final String COLLECTION_ID = "TestCollection";

    // We'll use Gson for POJO <=> JSON serialization for this sample.
    // Codehaus' Jackson is another great POJO <=> JSON serializer.
    private static Gson gson = new Gson();
    public static void main(String[] args) throws DocumentClientException,
            IOException {
        // Instantiate a DocumentClient w/ your DocumentDB Endpoint and AuthKey.
        DocumentClient documentClient = new DocumentClient(END_POINT,
                MASTER_KEY, ConnectionPolicy.GetDefault(),
                ConsistencyLevel.Session);

        // Start from a clean state (delete database in case it already exists).
        try {
            documentClient.deleteDatabase("dbs/" + DATABASE_ID, null);
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }

        // Define a new database using the id above.
        Database myDatabase = new Database();
        myDatabase.setId(DATABASE_ID);

        // Create a new database.
        myDatabase = documentClient.createDatabase(myDatabase, null)
                .getResource();


        System.out.println("Created a new database:");
        System.out.println(myDatabase.toString());
        System.out.println("Press any key to continue..");
        System.in.read();

        // Define a new collection using the id above.
        DocumentCollection myCollection = new DocumentCollection();
        myCollection.setId(COLLECTION_ID);

        // Set the provisioned throughput for this collection to be 1000 RUs.
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.setOfferThroughput(1000);

        // Create a new collection.
        myCollection = documentClient.createCollection(
                "dbs/" + DATABASE_ID, myCollection, requestOptions)
                .getResource();

        System.out.println("Created a new collection:");
        System.out.println(myCollection.toString());
        System.out.println("Press any key to continue..");
        System.in.read();

        // Create an object, serialize it into JSON, and wrap it into a
        // document.
        SomePojo allenPojo = new SomePojo("123", "Allen Brewer", "allen [at] contoso.com");
        String allenJson = gson.toJson(allenPojo);
        Document allenDocument = new Document(allenJson);

        // Create the 1st document.
        allenDocument = documentClient.createDocument(
                "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID, allenDocument, null, false)
                .getResource();

        System.out.println("Created 1st document:");
        System.out.println(allenDocument.toString());
        System.out.println("Press any key to continue..");
        System.in.read();

        // Create another object, serialize it into JSON, and wrap it into a
        // document.
        SomePojo lisaPojo = new SomePojo("456", "Lisa Andrews",
                "lisa [at] contoso.com");
        String somePojoJson = gson.toJson(lisaPojo);
        Document lisaDocument = new Document(somePojoJson);

        // Create the 2nd document.
        lisaDocument = documentClient.createDocument(
                "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID, lisaDocument, null, false)
                .getResource();

        System.out.println("Created 2nd document:");
        System.out.println(lisaDocument.toString());
        System.out.println("Press any key to continue..");
        System.in.read();

        // Query documents
        List<Document> results = documentClient
                .queryDocuments(
                        "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID,
                        "SELECT * FROM myCollection WHERE myCollection.email = 'allen [at] contoso.com'",
                        null).getQueryIterable().toList();

        System.out.println("Query document where e-mail address = 'allen [at] contoso.com':");
        System.out.println(results.toString());
        System.out.println("Press any key to continue..");
        System.in.read();

        // Replace Document Allen with Percy
        allenPojo = gson.fromJson(results.get(0).toString(), SomePojo.class);
        allenPojo.setName("Percy Bowman");
        allenPojo.setEmail("Percy Bowman [at] contoso.com");

        allenDocument = documentClient.replaceDocument(
                allenDocument.getSelfLink(),
                new Document(gson.toJson(allenPojo)), null)
                .getResource();

        System.out.println("Replaced Allen's document with Percy's contact information");
        System.out.println(allenDocument.toString());
        System.out.println("Press any key to continue..");
        System.in.read();

        // Delete Percy's Document
        documentClient.deleteDocument(allenDocument.getSelfLink(), null);

        System.out.println("Deleted Percy's document");
        System.out.println("Press any key to continue..");
        System.in.read();

        // Delete Database
        documentClient.deleteDatabase("dbs/" + DATABASE_ID, null);

        System.out.println("Deleted database");
        System.out.println("Press any key to continue..");
        System.in.read();

    }
}
  • среда
    • java: openjdk 11

Буду признателен, если вы дадите нам какие-либо предложения.

1 Ответ

0 голосов
/ 12 февраля 2020

Ошибка была связана со сборкой старой версии.

При сборке с последней версией 2.4.6 она работает правильно.

https://mvnrepository.com/artifact/com.microsoft.azure/azure-documentdb/2.4.6

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...