首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建文件夹时OneDrive REST错误400

创建文件夹时OneDrive REST错误400
EN

Stack Overflow用户
提问于 2016-03-27 12:07:39
回答 1查看 546关注 0票数 0

当试图使用REST在OneDrive中创建文件夹时,我遇到了问题。我正在使用下面的文档页面https://dev.onedrive.com/items/create.htm。我已经成功地进行了身份验证,并且令牌正在其他端点上正常工作。

现在,我花了一天的时间在这个问题上尝试各种可能的URI/方法组合,但都没有成功。所有其他端点(目录列表等)好吧,只是这一个快把我逼疯了。

如果有人能指出我的方法中的错误,任何帮助都将不胜感激。

下面的代码返回错误400,并带有以下消息:

代码语言:javascript
运行
复制
{"error":{"code":"invalidRequest","message":"The request is malformed or incorrect."}}

我使用GuzzlePhp库来处理请求。我的代码(简化):

代码语言:javascript
运行
复制
$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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 21:02:56

JSON不支持表单post语义--在OneDrive编码的blob中,参数被期望是一个JSON。我还没有使用口香糖,但是像这样的东西应该能用:

代码语言:javascript
运行
复制
$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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36247126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档