2016-03-27 74 views
0

嘗試使用REST API在我的OneDrive中創建文件夾時出現問題。我正在使用以下文檔頁面https://dev.onedrive.com/items/create.htm。我已成功通過身份驗證,並且令牌在其他終端上運行正常。創建文件夾時OneDrive REST API錯誤400

我現在花了一天的時間嘗試所有可能的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); 

回答

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); 
+0

謝謝!它的工作原理完全按照你的建議:)我已經在$ url ='/drive/items/'.$data->id.'/children/'上使用了POST請求,其中數據ID是來自GET'的字母數字字符串/驅動/根:/'.$ parentFolderName '/'。 –

相關問題