首页 文章

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

提问于
浏览
0

尝试使用REST API在OneDrive中创建文件夹时遇到问题 . 我正在使用以下文档页面https://dev.onedrive.com/items/create.htm . 我已成功通过身份验证,令牌在其他 endpoints 上运行正常 .

我现在花了一天时间尝试每一个可能的URI /方法组合,但没有成功 . 所有其他 endpoints (目录列表等)都可以,只是这一个让我发疯 .

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

以下代码返回错误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);

1 回答

  • 1

    OneDrive API没有使用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);
    

相关问题