使用 HttpClient 在 Java 中进行 OAuth2 身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24312747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OAuth2 Authentication in Java using HttpClient
提问by Danny Delott
I'm attempting to connect to the Freesound APIusing Java and the Apache HTTP Client (version 4.3).
我正在尝试使用 Java 和 Apache HTTP 客户端(4.3 版)连接到Freesound API。
I have succeeded in getting my application to successfully handle the authentication process. That is, I have exchanged the user's authorization code for an access token and refresh token, as outlined in Step 3 of the Freesound API OAuth2 Authentication Procedure.
我已成功让我的应用程序成功处理身份验证过程。也就是说,我已将用户的授权代码交换为访问令牌和刷新令牌,如Freesound API OAuth2 身份验证程序的步骤 3 中所述。
Now I wish to download sounds from the API.
现在我想从 API 下载声音。
The documentation for downloading sounds from the APIgives an example cURL command that I am now trying to mimic in Java.
从 API 下载声音的文档提供了一个示例 cURL 命令,我现在正尝试在 Java 中模仿它。
curl -H "Authorization: Bearer {{access_token}}" 'https://www.freesound.org/apiv2/sounds/14854/download/'
curl -H "Authorization: Bearer {{access_token}}" 'https://www.freesound.org/apiv2/sounds/14854/download/'
Unfortunately, the only way I know how to download files from URLs is with the Apache Commons IO line:
不幸的是,我知道如何从 URL 下载文件的唯一方法是使用 Apache Commons IO 行:
org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
Since this new link does not have a file extension, nor does it allow me to specify the Authorization Header, I cannot use this method.
由于这个新链接没有文件扩展名,也不允许我指定授权头,所以我不能使用这种方法。
My code currently does not work. Please help!
我的代码目前不起作用。请帮忙!
private static void downloadSound() {
// the output stream to save .wav file
FileOutputStream out;
// the file to save
File f = new File("test.wav");
// initializes http client and builds authorization header
HttpResponse res;
CloseableHttpClient httpclient = HttpClients.createDefault();
String authorizationString = "Bearer " + accessToken;
try {
// assigns url and authorization header to GET request
HttpGet request = new HttpGet(
URI.create("https://www.freesound.org/apiv2/sounds/14854/download/"));
request.addHeader("Authentication", authorizationString);
// sends GET request
res = httpclient.execute(request);
// downloads file
FileOutputSTream out = new FileOutputStream(f);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = res.getEntity().getContent().read(bytes)) != -1) {
out.write(bytes, 0, read);
}
System.out.println("Done!");
// closes input/output streams
res.getEntity().getContent().close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
采纳答案by sevensevens
It might be Authorizationvs Authentication
它可能是授权与身份验证
curl -H "Authorization: Bearer {{access_token}}"
curl -H "Authorization: Bearer {{access_token}}"
VS
VS
request.addHeader("Authentication", authorizationString);
request.addHeader("Authentication", authorizationString);
You might have an easier time using Apache HttpClient, as it is designed for computer to computer communication.
您可能更容易使用Apache HttpClient,因为它专为计算机到计算机的通信而设计。