未经授权,输入授权令牌无法提供请求

时间:2017-06-04 10:22:08

标签: azure azure-cosmosdb

我正在尝试使用resourcetokens获取documentdbclient。我有一个redis集群,其密钥作为用户标识,值为resourcetoken。 我有一个服务,使用主密钥为用户生成resourcetokens并在Redis中更新它们。我使用以下代码在我的主服务中创建资源令牌

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

然后我使用下面的代码获取resourcetoken并将其存储在redis

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

现在,在客户端,当我尝试使用resourcetoken

创建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创建了权限

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

  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

colls dxhxakm3caa = 星期一,05年6月8日08:56:40 gmt

下面是我用来获取令牌的代码

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

任何指针都会有很大的帮助

1 个答案:

答案 0 :(得分:1)

  

未经授权,输入授权令牌无法提供请求

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

<强>更新

  

如果我使用DocumentClient的重载构造函数并传递PermissionFeed,那么它可以正常工作

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

enter image description here

相关问题