使用 PHP 和 OAuth 访问 SkyDrive [英] Access SkyDrive using PHP and OAuth

查看:29
本文介绍了使用 PHP 和 OAuth 访问 SkyDrive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 PHP 访问 skyDrive.我想检索文件和文件夹列表,下载、上传和删除文件.

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

我有一个 microsoft dev clientID 和 clientSecret.

I've got a microsoft dev clientID and clientSecret.

有人可以让我开始使用 OAuth 连接到 skyDrive 并使用 API 吗?

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

非常感谢!

推荐答案

这实际上是一个相当广泛的问题.希望这里有一些可以帮助您入门的内容.

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

  1. 查看 SkyDrives REST API.
  2. 您可以使用 PHP cURL 来执行 GET 和 POST.
  3. 使用json_decode()创建接收数据的映射.
  4. 对于您发送的任何数据,请在 PHP 中创建地图并使用 json_encode().
  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().


试用 API

这是一个交互式 API,您可以实时试用以查看响应.

示例 (取自其他 SO 答案):

$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);

请求类型:http://msdn.microsoft.com/en-us/library/live/hh243648.aspx#http_verbs

我还建议您查看 curl_setopt() 以更好地了解如何使用 cURL 执行您需要的不同类型的请求.(SO 上的这个答案对使用 cURL 的 POST 与 GET 也有一些很好的解释.)

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_ID 发出 DELETE 请求.

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

  • 上传文件:

    要创建新的文件资源,您可以向/FOLDER_ID/files 发出 POST 请求,向目标文件夹的/UPLOAD_LOCATION 发出 POST 请求,或者向/FOLDER_ID/files/发出 PUT 请求.

    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/.

  • 下载文件:

    要获取文件资源的属性,请向/FILE_ID(目标文件 ID)发出 GET 请求.

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

    • 文件资源将在字段中包含从 SkyDrive 下载文件的 URL.
      • The File resource will contain the URL from which to download the file from SkyDrive in the source field.

        • 检索文件列表:

        要使用 Live Connect REST API 获取根文件夹资源,请向/me/skydrive 或/USER_ID/skydrive 发出 GET 请求.

        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.

        要获取子文件夹资源,请向/FOLDER_ID 发出 GET 请求.

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

      • 创建文件夹:

        要创建新的文件夹资源,请向/FOLDER_ID 发出 POST 请求.在请求体中传递 name 和 description 属性

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

      • 删除文件夹:

        要删除文件夹,请向/FOLDER_ID 发出 DELETE 请求.

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

      • 不幸的是,我对 OAuth 的体验有限.我只能提供一些相关的链接和建议,希望能有所帮助.

        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:

        其他一些可能有用的链接和指南:

        Some other potentially useful links and guides:

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

        这篇关于使用 PHP 和 OAuth 访问 SkyDrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆