Java &引用;旋度-u“;ApacheHttpClient的API密钥授权
我可以使用Java &引用;旋度-u“;ApacheHttpClient的API密钥授权,java,http-post,authorization,apache-httpclient-4.x,Java,Http Post,Authorization,Apache Httpclient 4.x,我可以使用restapi使用cURL作为答案。现在我想使用库将其“翻译”为Java代码。我只是在添加API密钥authorization头时遇到问题: HttpPost request = new HttpPost(DE_COMPILATION_URL); byte[] encodedAuth = Base64.encodeBase64(API_KEY.getBytes(StandardCharsets.UTF_8)); String authHeader = "Basic " + new St
restapi
使用cURL
作为答案。现在我想使用库将其“翻译”为Java
代码。我只是在添加API密钥
authorization头时遇到问题:
HttpPost request = new HttpPost(DE_COMPILATION_URL);
byte[] encodedAuth = Base64.encodeBase64(API_KEY.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
request.setHeader("mode", "raw");
request.setHeader("input", "@" + machineCodeFile.toString() + ";filename=" + machineCodeFile.getName());
request.setHeader("architecture", "powerpc");
request.setHeader("endian", "big");
request.setHeader("raw_entry_point", "0x0");
request.setHeader("raw_section_vma", "0x0");
request.setHeader("target_language", "c");
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
这张照片是:
{
"code": 401,
"description": "API key authorization failed (missing or invalid API key).",
"message": "Unauthorized by API Key"
}
我尝试了不同的授权代码,但都不起作用。有什么问题吗?API键是正确的,可以与
cURL
一起使用。不过,我最后忘记了一个冒号。编码中必须包含以下内容:
HttpPost request = new HttpPost(DE_COMPILATION_URL);
byte[] encodedAuth = Base64.encodeBase64((API_KEY + ":").getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);