【问题标题】:OneDrive REST API error 400 when creating folder创建文件夹时 OneDrive REST API 错误 400
【发布时间】:2016-04-01 05:02:56
【问题描述】:

尝试使用 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);

【问题讨论】:

    标签: rest guzzle onedrive


    【解决方案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);
    

    【讨论】:

    • 谢谢!它完全按照您的建议工作:) 我在 $url = '/drive/items/'.$data->id.'/children/'; 上使用了 POST requeston,数据 ID 是来自 GET '/ 的字母数字字符串驱动器/根目录:/'.$parentFolderName.'/'.
    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    相关资源
    最近更新 更多