无法使用BOX API v2.0创建文件夹

时间:2013-12-24 07:47:38

标签: box-api

function post($url, $params, $headers) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$parent_id = 0;
$name = 'examplefolder';
$params = array('name' => $name => json_encode(array('id' => $parent_id)));
$resp = post('https://api.box.com/2.0/folders', $params, array('Authorization: Bearer ' . $ACCESS_TOKEN_HERE));

当这段代码执行时,我得到了Json格式的错误

{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"missing_parameter","name":"parent","message":"'parent' is required"},{"reason":"missing_parameter","name":"name","message":"'name' is required"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"204107642652b93aceca050"}

我有什么遗漏吗?

补充说明。 我甚至试图将params作为这样的查询字符串。

name=examplefolder&parent={"id":0}

这给了我一个不同的错误

{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'name=examplefolder&parent={\"id\":0}'. 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":"154368831052b93bb027779"}

2 个答案:

答案 0 :(得分:1)

我并不完全熟悉这种语言语法,但在我看来,parent对象并未在POST主体中声明。你需要发送这个:

{"name":NAME, "parent":{"id":"0"}}

您可以通过更改$ params来实现这一目标:

$params = array('name' => $name, 'parent' => json_encode(array('id' => $parent_id)));

另外,根据文档,$parent_id应序列化为字符串,而不是整数。

答案 1 :(得分:0)

{“name”:NAME,“parent”:{“parent_id”:“0”}}

当我使用Angular 1 - angular file upload module时,这对我有用。

然而,与angular2不同,仍在继续......

{{1}}