【问题标题】:Coinbase pagination returning {"errors":[{"id":"not_found","message":"Not found"}]}Coinbase 分页返回 {"errors":[{"id":"not_found","message":"Not found"}]}
【发布时间】:2021-12-25 18:44:13
【问题描述】:

我正在尝试遍历分页的帐户列表,但是当我使用“next_uri”中的值发送请求时,我收到来自服务器的错误: {"errors":[{"id":"not_found","message":"Not found"}]}

我正确地添加了标头等,因为所有其他 API 调用都可以正常工作,它只是使用“next_uri”的请求不起作用。我认为我正确地遵循了 api 规范,所以我不确定问题是什么以及如何解决它。请问有人知道代码/逻辑有什么问题吗?

简化代码:

ArrayList<X> results = new ArrayList<>();
String uri = "/v2/accounts";
javax.ws.rs.client.Client client = getClient();
while(uri != null){
   T response = client.target("https://api.coinbase.com")
                    .path(uri).request(MediaType.APPLICATION_JSON).get(responseType);
   results.addAll(response.getData());
   uri = response.getPagination()==null ? null :response.getPagination().getNextUri();
}
return results;

结果是这样的:

请求 1:

https://api.coinbase.com/v2/accounts

响应 1:分页“:

{"ending_before":null,"starting_after":null,"previous_ending_before":null,"next_starting_after":"ef35df6c-a45b-5858-b755-f12a709cf26e","limit":25,"order":"desc ","previous_uri":null,"next_uri":"/v2/accounts?starting_after=ef35df6c-a45b-5858-b755-f12a709cf26e"},"data":[{....} ]

请求 2:

https://api.coinbase.com/v2/accounts%3Fstarting_after=ef35df6c-a45b-5858-b755-f12a709cf26e

响应 2:

{"errors":[{"id":"not_found","message":"未找到"}]}

【问题讨论】:

    标签: java jax-rs resteasy coinbase-api


    【解决方案1】:

    这与 jax-rs 库如何需要添加查询参数有关。仅仅依靠uri是不够的,参数还需要具体添加:

    target = target.queryParam(e.getKey(), e.getValue());
    

    所以最终的代码类似于

       WebTarget target = client.target(e"https://api.coinbase.com");
    
            if(params !=null){
                for(Map.Entry<String, String> e : params.entrySet()){
                    target = target.queryParam(e.getKey(), e.getValue());
                }
            }
            target = target.path(path);
            return target.request(MediaType.APPLICATION_JSON).get(responseType);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2017-11-19
      • 2020-10-11
      • 2016-03-16
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多