有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVA如何为OAuth 1.0 REST API构建签名方法为HMACSHA256的客户端?

我能够在POSTMAN中使用API(OAuth1.0授权和签名方法,如HMAC-SHA256),但不能使用JAVA(maven项目)。 使用OkHttp&;从POSTMAN生成的代码;Unirest库,两者都不工作。他们给出了以下错误

403 Forbidden

我理解错误与无效的身份验证参数有关。但无法找出需要更改的内容。因为邮递员身上也有同样的钥匙

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

感谢您的任何帮助。提前谢谢


共 (0) 个答案