Dropbox API 令牌验证
Dropbox API Token Verification
这个问题来自我的 . I thought I would perform a basic token authentication by calling the get_space_usage API 函数。我试过了
$headers = array("Authorization: Bearer token",
"Content-Type:application/json");
$ch = curl_init('https://api.dropboxapi.com/2/users/get_space_usage/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
该文档实际上并未表明有必要提供 Content-Type header。但是,如果没有 header 我会收到消息
Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json",...
输入 header 但不提供 POST 字段会产生另一个错误
request body: could not decode input as JSON
只是提供一些虚拟的 post 数据 curl_setopt($ch,CURL_POSTFIELDS,json_encode(array('a'=>1)));
并不能解决这个问题。我做错了什么?
documentation 并不表示需要 Content-Type
header,因为,由于此端点不采用任何参数,因此不需要 body , 因此没有内容可以通过 Content-Type
header 来描述。根据文档,这是一个有效的命令行 curl 示例:
curl -X POST https://api.dropboxapi.com/2/users/get_space_usage \
--header "Authorization: Bearer <ACCESS_TOKEN>"
将其转换为 PHP 中的 curl 需要确保 PHP 也不会发送 Content-Type
header。默认情况下,它显然发送 "application/x-www-form-urlencoded",但 API 不接受。如果您确实设置了 "application/json",API 将尝试这样解释 body,但无法这样做,因为它无效 JSON,因此失败。
omit the Content-Type
header with curl in PHP 显然不容易(或者不可能),因此替代方案是设置 "application/json",但提供有效的 JSON,例如 "null" .这是您的代码的修改版本:
<?php
$headers = array("Authorization: Bearer <ACCESS_TOKEN>",
"Content-Type: application/json");
$ch = curl_init('https://api.dropboxapi.com/2/users/get_space_usage');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "null");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
这个问题来自我的
$headers = array("Authorization: Bearer token",
"Content-Type:application/json");
$ch = curl_init('https://api.dropboxapi.com/2/users/get_space_usage/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
该文档实际上并未表明有必要提供 Content-Type header。但是,如果没有 header 我会收到消息
Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json",...
输入 header 但不提供 POST 字段会产生另一个错误
request body: could not decode input as JSON
只是提供一些虚拟的 post 数据 curl_setopt($ch,CURL_POSTFIELDS,json_encode(array('a'=>1)));
并不能解决这个问题。我做错了什么?
documentation 并不表示需要 Content-Type
header,因为,由于此端点不采用任何参数,因此不需要 body , 因此没有内容可以通过 Content-Type
header 来描述。根据文档,这是一个有效的命令行 curl 示例:
curl -X POST https://api.dropboxapi.com/2/users/get_space_usage \
--header "Authorization: Bearer <ACCESS_TOKEN>"
将其转换为 PHP 中的 curl 需要确保 PHP 也不会发送 Content-Type
header。默认情况下,它显然发送 "application/x-www-form-urlencoded",但 API 不接受。如果您确实设置了 "application/json",API 将尝试这样解释 body,但无法这样做,因为它无效 JSON,因此失败。
omit the Content-Type
header with curl in PHP 显然不容易(或者不可能),因此替代方案是设置 "application/json",但提供有效的 JSON,例如 "null" .这是您的代码的修改版本:
<?php
$headers = array("Authorization: Bearer <ACCESS_TOKEN>",
"Content-Type: application/json");
$ch = curl_init('https://api.dropboxapi.com/2/users/get_space_usage');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "null");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>