4

I'm trying to upload a file to a users folder on OneDrive using their API.

    $cfile = curl_file_create(realpath($_POST['ppt-file']));

    //place file in folder
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://apis.live.net/v5.0/". $my_folder ."/files?access_token=" . $access_token);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $upload_result = trim(curl_exec($ch));
    curl_close($ch);

I get a response from the API.

The request entity body has an incorrect value in the 'Content-Disposition' header. The expected format for this value is 'Content-Disposition: form-data; name="file"; filename="[FileName]"'."

Not sure where I'm going wrong, but this is the http header that's expected.

POST https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN

Content-Type: multipart/form-data; boundary=A300x

--A300x
Content-Disposition: form-data; name="file"; filename="HelloWorld.txt"
Content-Type: application/octet-stream

Hello, World!
--A300x--

Thanks in advance!

Update: When I put the API url directly in my action attribute of my form and rename my file input field to 'file', the file gets uploaded. But then I just get the response printed on my page :) not want I want to happen ofcourse.

 <form action="<?php echo "https://apis.live.net/v5.0/". $my_folder ."/files?access_token=" . $access_token; ?>" method="post" enctype='multipart/form-data'>
    <input type="file" name="file"/>
    <input type="submit" value="Upload your ppt" name="btnUpload"/>
 </form>
axelvnk
  • 442
  • 1
  • 6
  • 15
  • Do you have a trace for the request the CURL is generating? Unfortunately the multipart POST format for the API is not very accommodating, and as a result the request will need to look almost exactly like the example. See http://stackoverflow.com/questions/26225335/the-request-entity-body-has-an-incorrect-value-in-the-content-disposition-head – Brad Oct 08 '14 at 16:09
  • I am now able to upload a file. But weirdly enough, if the file is a powerpoint, it always ends up broken on OneDrive... I will be posting my code to GitHub soon so people can collaborate. – axelvnk Oct 09 '14 at 08:28

3 Answers3

4

Directly from https://developer.microsoft.com/en-us/onedrive

curl https://graph.microsoft.com/v1.0/me/drive/root:/document1.docx:/content -X PUT -d @document1.docx -H "Authorization: bearer access_token_here"
Thinkeye
  • 888
  • 12
  • 22
  • How to obtain the access token? I'm only getting "Access token validation failure. Invalid audience." Thank you! – mud1e Sep 15 '21 at 13:18
  • The authentication is somewhat complex topic, please start reading here: https://learn.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/authentication?view=odsp-graph-online – Thinkeye Oct 29 '21 at 14:28
3

It is possible using PUT with Curl like this. In my example the file is uploaded directly to the root Directory, you can specify a different path. Also change the Mime Type (in my case it was a jpg File). You will of course also need a working Auth Token.

// Get the Binary Content of the File You want to upload
$postdata = file_get_contents($uploadfile);

$upload_path = 'root:'; 
$filename = 'upload.jpg'; 

$ch = curl_init('https://graph.microsoft.com/v1.0/me/'.$upload_path.'/'.$filename.':/content'); 
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: image/jpeg' , $authorization )); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$result = curl_exec($ch); 
curl_close($ch); 
Daniel Resch
  • 584
  • 3
  • 12
0

Apparently the only way to do POST a file to the OneDrive API is by doing it directly in your form like this. And providing a redirect_uri. The response will come in the form of a pound value attached to your URL. Which is not ideal, since I'll have to get my file id with javascript.

<form action="<?php echo "https://apis.live.net/v5.0/". $your_folder ."/files?access_token=" . $access_token . "&redirect_uri=http%3A%2F%2Fwww.whateverredirect.com"; ?>" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="Upload your ppt" name="btnUpload"/>
</form>

I'd still like to do this in a curl type fashion, giving me a proper json response. So if anyone knows how, please do let me know!

axelvnk
  • 442
  • 1
  • 6
  • 15