1

我可以在 POSTMAN 中使用 API(OAuth 1.0 授权和签名方法为 HMAC-SHA256),但不能使用 JAVA(maven 项目)。使用 OkHttp 和 Unirest 库从 POSTMAN 生成的代码,两者都不起作用。他们给出以下错误。

403 Forbidden

我了解错误与无效的身份验证参数有关。但无法弄清楚需要更改什么。因为相同的键在 Postman 中工作

OkHttp JAVA代码

OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
String url = "https://www.example.com?script=508&deploy=1";
String JSONpayload = "{}";
RequestBody body = RequestBody.create(mediaType, JSONpayload);
Request request = new Request.Builder()
  .url(url)
  .method("POST", body)
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "OAuth realm=\"1111222_SB1\",oauth_consumer_key=\"xxxxxxxxx\",oauth_token=\"xxxxxxxx\",oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"1628747273\",oauth_nonce=\"xxxxxx\",oauth_version=\"1.0\",oauth_signature=\"xxxxxxxxxxx\"")
  .addHeader("Cookie", "NS_ROUTING_VERSION=LAGGING")
  .build();
Response response = client.newCall(request).execute();

Unirest JAVA代码

Unirest.setTimeouts(0, 0);
String url = "https://www.example.com?script=508&deploy=1";
String JSONpayload = "{}";
HttpResponse<String> response = Unirest.post(url)
  .header("Content-Type", "application/json")
  .header("Authorization", "OAuth realm=\"1114415_SB1\",oauth_consumer_key=\"xxxxxxxxxx\",oauth_token=\"xxxxxxxxxxx\",oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"1628747273\",oauth_nonce=\"xxxxxxxx\",oauth_version=\"1.0\",oauth_signature=\"xxxxxxxx\"")
  .header("Cookie", "NS_ROUTING_VERSION=LAGGING")
  .body(JSONpayload).asString();

任何形式的帮助表示赞赏。提前致谢。

4

0 回答 0