使用Java创建连接到cosmos db的程序时,发生错误,我问了一个问题。
当我在以下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();
}
}
如果能给我们任何建议,我将不胜感激。
答案 0 :(得分:0)
该错误是由于构建了旧版本所致。
使用最新版本2.4.6构建时,它可以正常工作。
https://mvnrepository.com/artifact/com.microsoft.azure/azure-documentdb/2.4.6