【问题标题】:BOX API:how to get location attribute in response using https://api.box.com/2.0/files/{fileId}/content for downloadingBOX API:如何使用 https://api.box.com/2.0/files/{fileId}/content 获取位置属性以进行下载
【发布时间】:2014-11-18 23:09:18
【问题描述】:

我的代码如下

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 以及位置网址(https://dl.boxcloud.com/*)。

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

【问题讨论】:

  • 我没有找到任何解决方案来解决我的问题。任何遇到过问题并能够解决的人。请给我建议。提前致谢。
  • 返回的内容看起来像实际响应的标题。我建议您检查代码的语法——请参阅this example

标签: rest nginx box-api box boxapiv2


【解决方案1】:

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

引用 Download a File 的 Box API 文档:

如果文件可供下载,则响应为 302 在 dl.boxcloud.com 上找到一个 URL。

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

HTTP 响应状态码 302 Found 是一种常见的执行方式 网址重定向。

带有此状态代码的 HTTP 响应将另外提供一个 URL 在 Location 标头字段中。用户代理(例如网络浏览器)是 由带有此代码的响应邀请进行第二次,否则 相同,请求到 Location 字段中指定的新 URL。

因此,要在响应标头中获取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();
    }
}

【讨论】:

    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多