gpt4 book ai didi

rest - BOX API :how to get location attribute in response using https://api. box.com/2.0/files/{fileId}/content 下载

转载 作者:行者123 更新时间:2023-12-04 12:48:56 24 4
gpt4 key购买 nike

我的代码如下

WebResource webResource1 = cl.resource("https://api.box.com/2.0/files/{fileId}/content");

ClientResponse res1 = webResource1.header("Authorization", "Bearer"+p1.getAccess_token()).get(ClientResponse.class);
String jsonStr1 = res1.getEntity(String.class);

我的回复如下-

{Object-Id=[file_20317568941], Cache-control=[private], Date=[Wed, 24 Sep 2014 12:11:43 GMT], Content-Length=[27], X-Robots-Tag=[noindex, nofollow], Content-Disposition=[attachment;filename="upload.txt";filename*=UTF-8''upload.txt], Accept-Ranges=[bytes, bytes], Connection=[keep-alive], Content-Type=[text/plain; charset=UTF-8], Server=[nginx], X-Content-Type-Options=[nosniff]}

我收到状态码 200, OK;但是要获取 location 属性,我需要状态代码 302 以及位置 url (https://dl.boxcloud.com/*)。

在响应中没有获取 location: https://dl.boxcloud.com/* 属性,如何从 box api 下载文件?

最佳答案

上周六我抽空调查了您的问题。基本问题是,如果您需要获取 Location 值,则需要停止自动重定向。以下是您的问题的解释和解决方案:

Download a File 的引用框 API 文档:

If the file is available to be downloaded, the response will be a 302 Found to a URL at dl.boxcloud.com.

来自关于 HTTP 302 的维基百科文章:

The HTTP response status code 302 Found is a common way of performing URL redirection.

An HTTP response with this status code will additionally provide a URL in the Location header field. The user agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request to the new URL specified in the Location field.

因此,要在响应 header 中获取 Location 属性,您需要停止自动重定向。否则,根据 box doc,您将获得文件的原始数据而不是下载 URL。

以下是使用 Commons HTTPClient 实现的解决方案:

private static void getFileDownloadUrl(String fileId, String accessToken) {
try {
String url = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId);
GetMethod getMethod = new GetMethod(url);
getMethod.setFollowRedirects(false);

Header header = new Header();
header.setName("Authorization");
header.setValue("Bearer " + accessToken);
getMethod.addRequestHeader(header);

HttpClient client = new HttpClient();
client.executeMethod(getMethod);

System.out.println("Status Code: " + getMethod.getStatusCode());
System.out.println("Location: " + getMethod.getResponseHeader("Location"));
} catch (Exception cause) {
cause.printStackTrace();
}
}

使用 java.net.HttpURLConnection 的替代解决方案:

private static void getFileDownloadUrl(String fileId, String accessToken) {
try {
String serviceURL = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId);
URL url = new URL(serviceURL);

HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection());
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
connection.connect();

int statusCode = connection.getResponseCode();
System.out.println("Status Code: " + statusCode);

Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> locations = headerFields.get("Location");

if(locations != null && locations.size() > 0) {
System.out.println("Location: " + locations.get(0));
}
} catch (Exception cause) {
cause.printStackTrace();
}
}

由于 Commons HTTPClient 已过时,以下解决方案基于 Apache HttpComponents :

private static void getFileDownloadUrl(String fileId, String accessToken) {
try {
String url = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId);
CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
HttpGet httpGet = new HttpGet(url);
BasicHeader header = new BasicHeader("Authorization", "Bearer " + accessToken);
httpGet.setHeader(header);
CloseableHttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status Code: " + statusCode);

org.apache.http.Header[] headers = response.getHeaders(HttpHeaders.LOCATION);

if(header != null && headers.length > 0) {
System.out.println("Location: " + headers[0]);
}
} catch (Exception cause) {
cause.printStackTrace();
}
}

关于rest - BOX API :how to get location attribute in response using https://api. box.com/2.0/files/{fileId}/content 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26017206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com