Access SkyDrive using PHP and OAuth

2019-03-13 20:06发布

问题:

I would like to access skyDrive using PHP. I want to retreive list of files and folders, download, upload and delete files.

I've got a microsoft dev clientID and clientSecret.

Can anybody get me started with connecting to skyDrive with OAuth and making use of the API?

Thanks a lot!

回答1:

This is actually quite a broad question. Here's hopefully something that will get you started.

  1. Have a look at SkyDrives REST API.
  2. You could use PHP cURL to perform the GET's and POST's.
  3. Use json_decode() to create a map of the received data.
  4. For any data you send, create maps in PHP and convert them to JSON using json_encode().

Try the API

Here is an interactive API you can try out live to see the responses.


Making requests

Example (taken from other SO Answer):

$url = 'POST https://apis.live.net/v5.0/me/skydrive/files';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, array('access_token' => TOKEN, 'name' => 'file', 'filename' => "@HelloWorld.txt"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Request types: http://msdn.microsoft.com/en-us/library/live/hh243648.aspx#http_verbs

I also recommend you have a look at curl_setopt() to better understand how to do the different types of requests you'll be needing, using cURL. (Also this answer on SO has some good explanation on POST vs GET using cURL.)


File object

  • DELETE FILES:

    To delete a file, make a DELETE request to /FILE_ID.

  • UPLOAD FILES:

    To create a new File resource, you can either make a POST request to /FOLDER_ID/files, a POST request to the /UPLOAD_LOCATION for the target folder, or a PUT request to /FOLDER_ID/files/.

  • DOWNLOAD FILES:

    To get properties for a File resource, make a GET request to /FILE_ID (the target file ID).

    • The File resource will contain the URL from which to download the file from SkyDrive in the source field.


Folder object

  • RETRIEVE LIST OF FILES:

    To get the root Folder resource by using the Live Connect REST API, make a GET request to either /me/skydrive or /USER_ID/skydrive.

    To get a subfolder resource, make a GET request to /FOLDER_ID.

  • CREATE FOLDERS:

    To create a new Folder resource, make a POST request to /FOLDER_ID. Pass the name and description attributes in the request body

  • DELETE FOLDERS:

    To delete a folder, make a DELETE request to /FOLDER_ID.


OAuth 2.0

My experience with OAuth is unfortunately limited. I can only provide some relevant links and advice which I hope will help.

Review the Protocol Overview and consider if you want to implement something yourself, or use a library. Quick Google search gives me:

  • http://code.google.com/p/google-api-php-client/wiki/OAuth2
  • http://code.google.com/p/oauth2-php/
  • http://php.net/manual/en/book.oauth.php

Some other potentially useful links and guides:

  • See PHP section of http://oauth.net/code/