azure - 未经授权,输入的授权 token 无法满足请求

标签 azure azure-cosmosdb

我正在尝试使用资源 token 获取 documentdbclient。我有一个 Redis 集群,其中键作为用户身份,值作为资源 token 。 我有一项服务,它使用主 key 为用户生成资源 token 并在 Redis 中更新它们。我正在使用以下代码在我的主服务中创建资源 token

  ResourceResponse<Permission> readPermissions = documentClient.readPermission("/dbs/customerdb/users/mobileuser/permissions/readperm", null);
        String accessToken=permission.getToken();
DocumentClient documentClient = new DocumentClient(HOST, MASTER_KEY,
                     ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);

然后我使用下面的代码来获取资源 token 并将其存储在redis中

jedis.put("Client_1",readPermissions .getResource().getToken());

现在,在客户端,当我尝试使用资源 token 创建 documentClient

DocumentClient manageClient = new DocumentClient(HOST,  jedis.get("Client_1"),ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);

我收到日志,指出未经授权,并出现错误

Unauthorized, The input authorization token can't serve the request

我在数据库 customerdb 上创建了一个名为 mobileuser 的用户,并使用模式 PermissionMode.Read 对集合客户创建了权限

我更改了代码,以确保 token 没有过期,但仍然出现错误

  java.lang.IllegalStateException: 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: 'get

科尔斯 dxhxakm3caa= 2017 年 6 月 5 日星期一 08:56:40 GMT

下面是我用来获取 token 的代码

    ResourceResponse<Permission> permissions=documentClient.readPermission("/dbs/customerdd/users/mobileuser/permissions/readperm", null);
    System.out.println(permissions.getResource().getResourceLink());
    DocumentClient managedClient=new DocumentClient(HOST,permissions.getResource().getToken(), ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
    FeedResponse<Document> response = managedClient.queryDocuments(collection.getResource().getSelfLink(), "SELECT customers.source FROM customers where customers.source='direct-mail'", null);
    Iterator<Document> itr = response.getQueryIterator();
    while(itr.hasNext()){
        Document doc=itr.next();
        System.out.println(doc.get("source"));
    }

任何指针都会有很大帮助

最佳答案

Unauthorized, The input authorization token can't serve the request

据我所知,默认有效时间跨度为the resource token是一小时。并且如果资源 token 过期,后续请求会收到401未授权异常,请在从Redis缓存中检索资源 token 时确保资源 token 是否过期。

更新:

If i use the overloaded constructor of DocumentClient and pass the PermissionFeed then it works

DocumentClient class有如下两个构造函数,当您使用 new DocumentClient(HOST, jedis.get("Client_1"),ConnectionPolicy.GetDefault(), ConsistencyLevel.Session); 时,它似乎识别 jedis.get("Client_1") 您作为字符串传递并使用第二个构造函数来初始化新实例,这将是问题的原因。

enter image description here

关于azure - 未经授权,输入的授权 token 无法满足请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353016/

相关文章:

sql - Microsoft SQL - 无法永远删除表, "preparing update script"

.net - 云服务回收时出现错误 System.IO.FileLoadException : Could not load file or assembly 'System. Web.Http,版本=4.0.0.0

mongodb - CosmosDB - 无效的 BSON 字段名称 'places.$id'

session - DocumentDB - 较新的 session token 是否保证读回较旧的写入?

sql-server - 如何知道我使用的是 Azure SQL 数据库还是 Azure SQL 数据仓库?

azure - azure 中的批处理

python - Azure 函数以最大间隔超时,而调用尚未达到最大间隔

azure - 如何在部署期间配置 Cosmos Db 集合

python - 组的 AzCosmosDBSqlRoleAssignment 问题

Node.js - 使用 azure DocumentDB 和 readDocument(链接、选项、回调)时传递分区键