尝试使用REST API在OneDrive中创建文件夹时遇到问题。我正在使用以下文档页面https://dev.onedrive.com/items/create.htm。我已成功通过身份验证,令牌在其他端点上正常工作。
我花了一天时间在这个上尝试每个可能的URI /方法组合,但没有成功。所有其他端点(目录列表等)都没问题,只是这一点让我发疯了。
如果有人能指出我的方法中的错误,我们将不胜感激。
以下代码返回错误400,并显示以下消息:
{"error":{"code":"invalidRequest","message":"The request is malformed or incorrect."}}
我正在使用GuzzlePhp库来处理请求。我的代码(简化):
$parentId = '01WZZ7ZY2LNHB75JADQJD3GGUQFSCRRZTQ'; //id to root
$method = "POST";
//none of these does the trick (to be clear, I use only one at the time)
$url = '/_api/v2.0/drive/items/'.$parentId.'/NewFolder'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //post
$url = '/_api/v2.0/drive/root:/NewFolder'; //post
$options = [
'headers' => [
'Authorization' => $token,
'Content-Type' => 'application/json',
'Content-Length'=> 0,
]
'form_params' => [
"name" => "NewFolder",
"folder" => (object)[],
"@name.conflictBehavior" => "fail"
]
];
//Guzzle library sends the code as specified
$res = $this->client->request($method, $url, $options);
答案 0 :(得分:1)
OneDrive API不支持表单语义 - 这些参数在主体中需要作为JSON编码的blob。我还没有使用过Guzzle,但像this这样的东西应该有效:
$parentId = '01WZZ7ZY2LNHB75JADQJD3GGUQFSCRRZTQ'; //id to root
$method = "POST";
//none of these does the trick (to be clear, I use only one at the time)
$url = '/_api/v2.0/drive/items/'.$parentId.'/NewFolder'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //post
$url = '/_api/v2.0/drive/root:/NewFolder'; //post
$options = [
'headers' => [
'Authorization' => $token,
'Content-Type' => 'application/json',
'Content-Length'=> 0,
]
'body' =>
'{
"name": "NewFolder",
"folder": { },
"@name.conflictBehavior": "fail"
}'
];
//Guzzle library sends the code as specified
$res = $this->client->request($method, $url, $options);