OneDrive REST API 创建文件夹时出现错误 400
OneDrive REST API error 400 when creating folder
我在尝试使用 REST API 在我的 OneDrive 中创建文件夹时遇到问题。我正在使用以下文档页面 https://dev.onedrive.com/items/create.htm。我已成功通过身份验证并且令牌在其他端点上工作正常。
我现在花了一天多的时间尝试了所有可能的 URI/method 组合,但没有成功。所有其他端点(目录列表等)都可以,只有这个让我发疯。
如果有人能指出我方法中的错误,我们将不胜感激。
下面的代码 returns 错误 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);
OneDrive API 不支持形式 post 语义 - 正文中的参数应作为 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);
我在尝试使用 REST API 在我的 OneDrive 中创建文件夹时遇到问题。我正在使用以下文档页面 https://dev.onedrive.com/items/create.htm。我已成功通过身份验证并且令牌在其他端点上工作正常。
我现在花了一天多的时间尝试了所有可能的 URI/method 组合,但没有成功。所有其他端点(目录列表等)都可以,只有这个让我发疯。
如果有人能指出我方法中的错误,我们将不胜感激。
下面的代码 returns 错误 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);
OneDrive API 不支持形式 post 语义 - 正文中的参数应作为 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);