1

我正在尝试通过 POSTMAN 通过 BOX API 下载图像文件。文件编号为 6917417032

我的代码是: https ://api.box.com/2.0/files/6917417032/content 我在标题中指定了授权码

文档状态:返回 如果文件可供下载,则响应将是 302 Found 到 dl.boxcloud.com 上的 URL。客户端需要遵循重定向才能实际下载文件。除非文件 ID 无效或用户无权访问,否则返回文件的原始数据。

我在 POSTMAN 中的回复是“200,OK” dl.boxcloud.com 没有 302。POSTMAN 还会预览空白缩略图。如果我对 word 文档执行相同的操作,POSTMAN 会显示文档中的下一个。我期待的是一个文件下载对话框,或者上一段中描述的 url。

注意,curl 示例代码和响应也不同于描述返回的段落:

方法

获取 /files/{文件 ID}/内容

示例请求

curl -L https://api.box.com/2.0/files/FILE_ID/content **强文本** -H "授权:Bearer ACCESS_TOKEN"

示例响应我刚刚下载的文本文件的原始文本(所以下载成功)

4

2 回答 2

3

有两件事混淆了这里发生的事情:

  1. POSTMAN 跟踪 GET 请求的重定向,但不提供有关请求历史记录的信息(例如,如果我说 GET URL1 并被重定向到 GET URL2,它只会告诉我我原来的 GET URL1,即使它提供了 GET URL2 的结果)

  2. 文档中的 cURL 示例包含一个 -L 标志,它告诉 cURL 遵循重定向。cURL 不以提供明确要求之外的任何信息而闻名,这是另一种情况。

如果您想查看实际发生的情况,可以通过添加 'v 标志将 cURL 置于详细模式并查看发生的所有情况,例如:

curl -v -L https://api.box.com/2.0/files/FILE_ID/content -H "Authorization: Bearer TOKEN"

* About to connect() to api.box.com port 443 (#0)
*   Trying 74.112.184.225...
* connected
* Connected to api.box.com (74.112.184.225) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-SHA
* Server certificate:
*    subject: serialNumber=ts5jamwzcjWP0oTyh2m74yh0zsB2zyOR; C=US; ST=California; L=Palo Alto; O=Box.net, Inc.; CN=*.box.com
*    start date: 2011-09-27 22:07:27 GMT
*    expire date: 2013-09-29 11:02:11 GMT
*    subjectAltName: api.box.com matched
*    issuer: C=US; O=GeoTrust, Inc.; CN=GeoTrust SSL CA
*    SSL certificate verify ok.
> GET /2.0/files/5620199214/content HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: api.box.com
> Accept: */*
> Authorization: Bearer TOKEN
> 
< HTTP/1.1 302 Found
< Server: nginx
< Date: Sun, 17 Mar 2013 20:48:40 GMT
< Content-Type: text/html; charset=utf-8
< Connection: keep-alive
< Cache-control: no-cache, no-store
< Location: https://www.boxcloud.com/bc/1/b4c1b5e872bf843d3ded7bcae5f4f175/2Of-pgQHiFlfW6KldVyHAxtUIEW0l7HoOpi14-VGcxcSnn2CtobCO1UjJZLI58w_oCTNcPMKbUR_gn-ncDkAbw,,/f9a04d69f716d92ceebd848bb9ccded8/
< Content-Length: 0
< 
* Connection #0 to host api.box.com left intact
* Issue another request to this URL: 'https://www.boxcloud.com/bc/1/b4c1b5e872bf843d3ded7bcae5f4f175/2Of-pgQHiFlfW6KldVyHAxtUIEW0l7HoOpi14-VGcxcSnn2CtobCO1UjJZLI58w_oCTNcPMKbUR_gn-ncDkAbw,,/f9a04d69f716d92ceebd848bb9ccded8/'
* About to connect() to www.boxcloud.com port 443 (#1)
*   Trying 74.112.184.74...
* connected
* Connected to www.boxcloud.com (74.112.184.74) port 443 (#1)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-SHA
* Server certificate:
*    subject: serialNumber=o2MxwjKDoF9mEZ3YNhBMR/WISRBUXT6b; C=US; ST=California; L=Palo Alto; O=Box, Inc.; CN=*.boxcloud.com
*    start date: 2012-02-27 00:10:39 GMT
*    expire date: 2014-03-01 01:48:45 GMT
*    subjectAltName: www.boxcloud.com matched
*    issuer: C=US; O=GeoTrust, Inc.; CN=GeoTrust SSL CA
*    SSL certificate verify ok.
> GET /bc/1/b4c1b5e872bf843d3ded7bcae5f4f175/2Of-pgQHiFlfW6KldVyHAxtUIEW0l7HoOpi14-VGcxcSnn2CtobCO1UjJZLI58w_oCTNcPMKbUR_gn-ncDkAbw,,/f9a04d69f716d92ceebd848bb9ccded8/ HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.boxcloud.com
> Accept: */*
> Authorization: Bearer TOKEN
> 
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 17 Mar 2013 20:48:41 GMT
< Content-Type: image/png
< Content-Length: 15039
< Connection: keep-alive
< Cache-control: private
< Accept-Ranges: bytes
< Content-Disposition: attachment;filename="Screenshot_1_14_13_5_33_PM.png";filename*=UTF-8''Screenshot_1_14_13_5_33_PM.png
< X-Content-Type-Options: nosniff
< Accept-Ranges: bytes
< 
?PNG
…
于 2013-03-17T20:51:03.500 回答
1

解决方案

获取 box.com 文件的直接下载链接

您需要为此使用 Curl

<?php


    $token = "<accsess_token>"; 

//setup the request, you can also use CURLOPT_URL
$ch = curl_init('https://api.box.com/2.0/files/FILE_ID_HERE/content');//Enter File id

// Returns the data/output as a string instead of raw data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Set your auth headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
   'Authorization: Bearer ' . $token
   ));

// get stringified data/output. See CURLOPT_RETURNTRANSFER
$response = curl_exec($ch);

// get info about the request
$info = curl_getinfo($ch);
// print_r($info->getBody());
// $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// $header = substr($response, 0, $header_size);
// $body = substr($response, $header_size);
echo '<pre>'; print_r($info['redirect_url']);
// close curl resource to free up system resources
curl_close($ch);
?>
于 2019-11-06T10:31:19.830 回答