当前位置: 首页 > 知识库问答 >
问题:

输入授权令牌不能为请求服务。(azure-documentdb-java)

益何平
2023-03-14
    null
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: 

    null
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个答案

太叔炎彬
2023-03-14

该错误是由于生成的旧版本造成的。

当使用最新的2.4.6版本构建时,它可以正常工作。

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

 类似资料:
  • 我必须用第三方授权服务器在我的项目中实现OAuth 2.0。我的客户机和服务器都已经在AS上注册了。我在Azure上创建了一个API管理实例,并导入了swagger APIs。我希望每个传入的请求都根据我的AS进行验证,所以我只需要将请求重定向到https://my-as.com/as/introspect.oauth2,并验证令牌。如果令牌有效,则让它继续,否则发送401。我试图使用“入站处理”

  • 我想在Fiddler中查询DocumentDB X-MS-版本:2016-07-11 授权:type=master&ver=1.0&sig=masterkey 内容类型:应用程序/查询+JSON

  • 试图让OAuth2谷歌登录工作,这是我的应用程序发出的原始请求: : : : : 这是回应: 状态: null 对此的帮助将不胜感激。我试图在我的应用程序中设置OAuth2支持的“用X登录”功能,已经让脸书和推特没有问题地工作,也想让谷歌工作,但如果我不能解决这个问题,我恐怕我将不得不放弃谷歌Auth。

  • 我目前正在学习OAuth 2.0和OpenID Connect,我对授权服务器和访问令牌有疑问。规范将授权服务器定义为: 服务器在成功验证资源所有者并获得授权后向客户端发放访问令牌。 因此,据我所知,客户端将用户重定向到授权服务器,用户在授权服务器上进行身份验证,授权服务器向客户端发出访问令牌。 现在有一件事发生了,直到现在我才明白。有两种可能的方法来理解这一点,我正在努力找到正确的方法: > 授

  • 我很难让刷新令牌在Azure App Service中使用移动应用程序为某些身份验证提供商工作。CGillum就此写了一篇很棒的文章(http://cgillum.tech/2016/03/07/app-service-token-store/ ),在这篇文章之后,我发现刷新方法对微软账户很有效,但我很难刷新脸书和谷歌的访问令牌。我们的应用程序(Xamarin Forms)使用微软账户、谷歌和脸书

  • 我使用springfox 2.9.2我有这样的api: 女巫来自身份验证服务器。我尝试在swagger中首次调用此服务器,并将其传递给像上面这样的控制器请求。所以我做 在Swagger ui上,授权调用成功返回了令牌,但它没有将令牌添加到请求头中。它会产生 如果我像这样设置令牌: