【问题标题】:Unauthorized, The input authorization token can't serve the request未授权,输入的授权令牌不能服务于请求
【发布时间】:2017-06-04 10:22:08
【问题描述】:

我正在尝试使用资源令牌获取 documentdbclient。我有一个 redis 集群,其中键作为用户身份,值作为资源令牌。 我有一个使用主密钥为用户生成资源令牌并在 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());

现在,当我尝试使用资源令牌创建 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

科尔斯 dxhxakm3caa= 2017 年 6 月 5 日,星期一,格林威治标准时间 08:56:40

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

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

任何指针都会有很大帮助

【问题讨论】:

  • "readPermissions .getResource().getToken()" - 我希望引号只是问题中的拼写错误?
  • 是的,这些是错别字...我已经编辑了它们。谢谢指出

标签: azure azure-cosmosdb


【解决方案1】:

未授权,输入的授权令牌无法服务请求

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

更新:

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

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

【讨论】:

  • 不,我不认为这是问题所在,因为我在生成代码后才调用 API。
  • 如果直接使用自己生成的资源token访问资源(不是存放在Redis缓存中检索),资源token是否有效?
  • 为了重新确认令牌没有过期,我更改了我的代码。现在我再次读取权限,然后获取令牌以发出请求,但仍然收到错误 java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: 输入授权令牌无法服务请求。请检查预期的有效负载是否按照协议构建,并检查正在使用的密钥。服务器使用以下有效负载进行签名:'get colls dxhxakm3caa= mon, 05 jun 2017 08:56:40 gmt
  • 如果我使用 DocumentClient 的重载构造函数并传递 PermissionFeed 那么它可以工作
  • 如果您看到您可以传递 MasterKey 或资源令牌或传递权限提要,则基于以下链接。 msdn.microsoft.com/en-us/library/… 现在,如果我通过使用 Permission.getToken() 方法获得的资源令牌传递我得到未经授权的错误,但是如果我在权限列表中添加用于获取令牌的相同权限对象并传递它。有用。我没有得到这种行为的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 2018-06-11
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多