我是使用PHP cURL的初学者,我正在尝试使用使用oAuth的Box.com API 2.0。 我试图返回有关用户的基本信息。这是我的代码:
$box_1 = curl_init("https://api.box.com/2.0/users/me");
curl_setopt($box_1, CURLOPT_HTTPHEADER, "Authorization: Bearer token_was_here");
curl_setopt($box_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($box_1, CURLOPT_POSTFIELDS, "fields=id,name,login");
$box_curl1 = curl_exec($box_1);
curl_close($box_1);
当我运行此操作时,我收到此错误:{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'fields=id,name,login'. Entity body should be a correctly nested resource attribute name\/value pair"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"request_id_was_here"}
当我删除第4行时,我根本没有回复。
答案 0 :(得分:1)
fields=id,name,login
应该在查询字符串中,而不是请求正文中。
答案 1 :(得分:0)
好吧,我明白了!
我的“访问令牌”已过期,由于我没有选择输出标题,我从未发现过这个!
我还稍微修改了我的代码:
$token = "token here";
$box_1 = curl_init();
curl_setopt($box_1, CURLOPT_URL, "https://api.box.com/2.0/users/me?fields=id");
curl_setopt($box_1, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$token));
curl_setopt($box_1, CURLOPT_HTTPGET, TRUE);
curl_setopt($box_1, CURLOPT_RETURNTRANSFER, true);
$box_curl1 = curl_exec($box_1);
curl_close($box_1);