标签:des style http color java 使用 os io
小梦今天给大家分享一下windows phone 8.1开发 onedrive中的一些操作:
最后有源代码。
进入 http://msdn.microsoft.com/onedrive/dn630256 页面 选择 适用于 Windows、Windows Phone 和 .NET 的 Live SDK 下载 ,安装即可。安装后再引用 Live SDK 。别急,还没完。
在工具栏 应用商店中选择 将应用程序和应用商店关联 。按着提示完成即可。好了,下来就然我们正式开始吧。
好了,先上代码,我们在慢慢解释。
LiveLoginResult result;
private async void loginButton_Click(object sender, RoutedEventArgs e)//登录
{
try
{
msgText.Text = "亲:正在登录";
var authClient = new LiveAuthClient();//初始化,用来获取用户数据。
result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive", "wl.skydrive_update", "wl.photos" });
//登录,里面的参数是 Scopes
if (result.Status == LiveConnectSessionStatus.Connected)
{
var connectClient = new LiveConnectClient(result.Session);
var meResult = await connectClient.GetAsync("me");
msgText.Text = "亲爱的:" + meResult.Result["name"].ToString() + " 您已成功登录OneDrive!";
}
}
catch (LiveAuthException ex)
{
msgText.Text = ex.Message;
}
catch (LiveConnectException ex)
{
msgText.Text = ex.Message;
}
}
Scopes 翻译为域 。意思是授权允许应用获取用户的哪些数据。 域分为核心域和扩展域: 核心域包括: wl.basic :获取用户的基本信息,也可以获得用户的联系人。 wl.offline_access:可以任何时候读取和更新用户数据。 wl.signin:单点登录。即只要用户已经登录,只需用户授权即可,不需再次登录。在windows phone 上推荐使用。 扩展域: 扩展域很多,常用的主要有: wl.photos:授权获取用户的图片,视频,唱片集,音频等。 wl.skydrive:读取用户在onedrive 上存储的文件 wl.skydrive_update:上传,修改用户onedrive 上存储的文件
{ "id": "8c8ce076ca27823f", "name": "Roberto Tamburello", "first_name": "Roberto", "last_name": "Tamburello", "gender": null, "locale": "en_US" } 也就是上例的meResult.Result。
private async void uploadButton_Click(object sender, RoutedEventArgs e)//上传
{
try
{
msgText.Text = "亲:正在上传";
if (result.Session != null)
{
var liveConnectClient = new LiveConnectClient(result.Session);
//读取文件
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await localFolder.GetFileAsync(App.TestFileName);
LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(
"me/skydrive", file.Name, file, OverwriteOption.Rename);
LiveOperationResult uploadResult = await uploadOperation.StartAsync();
}
}
catch (LiveAuthException ex)
{
msgText.Text = ex.Message;
}
catch (LiveConnectException ex)
{
msgText.Text = ex.Message;
}
}
我们已知要下载的文件名为:Test.txt。也就是刚上传的文件。
private async void downButton_Click_1(object sender, RoutedEventArgs e)//下载
{
try
{
msgText.Text = "亲:正在下载";
string id = string.Empty;
if (result.Session != null)
{
var connectClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult = await connectClient.GetAsync("me/skydrive/search?q=Test.txt");
List<object> items = operationResult.Result["data"] as List<object>;
IDictionary<string, object> file = items.First() as IDictionary<string, object>;
id = file["id"].ToString();
LiveDownloadOperation operation = await connectClient.CreateBackgroundDownloadAsync(string.Format("{0}/content", id));
var results = await operation.StartAsync();
string strings=results.Stream.ToString();
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile files = await localFolder.GetFileAsync(App.TestFileName);
await FileIO.WriteTextAsync(files, strings);
msgText.Text = "恭喜:您已成功从OneDrive下载文件!";
}
}
catch (Exception ex)
{
msgText.Text = ex.Message;
}
}
filter:指定项目类型(all(默认类型)、photos、videos、audio、folders 或 albums)来仅获取某些类型的项目。例如,若要仅获取照片,请使用 FOLDER_ID/files?filter=photos。 limit:指定要获取的项目数来获取有限数量的项目。例如,若要获取前两个项目,请使用 FOLDER_ID/files?limit=2。 offset: offset 参数设置为要获取的第一个项目的索引来指定要获取的第一个项目。例如,若要获取从第三个项目开始的两个项目,请使用 FOLDER_ID/files?limit=2&offset=3。 q:获取指定名称的文件。例如上例中的:me/skydrive/search?q=Test.txt
上传和图片和文件是一样的方法:
private async void uploadPiactrue_Click(object sender, RoutedEventArgs e)
{
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("2.jpg");
try
{
msgText.Text = "亲:正在上传";
if (result.Session != null)
{
var liveConnectClient = new LiveConnectClient(result.Session);
LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(
"me/skydrive/camera_roll", file.Name, file, OverwriteOption.Overwrite);
LiveOperationResult uploadResult = await uploadOperation.StartAsync();
if (uploadResult.Result != null)
{
msgText.Text = "恭喜:您已成功上传OneDrive!";
}
}
}
catch (LiveAuthException ex)
{
msgText.Text = ex.Message;
}
catch (LiveConnectException ex)
{
msgText.Text = ex.Message;
}
}
要访问特定的 OneDrive 文件夹,你可以使用友好名称,而非文件夹 ID。你可以在 OneDrive UI 中使用以下友好名称来访问相应的文件夹: •USER_ID/skydrive/camera_roll 表示“OneDrive 本机照片”文件夹。 •USER_ID/skydrive/my_documents 表示“文档”文件夹。 •USER_ID/skydrive/my_photos 表示“图片”文件夹。 •USER_ID/skydrive/public_documents 表示“公用”文件夹。 一般情况下,将 USER_ID 替换为 me(对于已登录用户) 。如上例,就是将图片上传至本机照片文件夹。
private async void downPictrue_Click(object sender, RoutedEventArgs e)
{
msgText.Text = "亲:正在下载";
string idPictrue = string.Empty;
string namePictrue = string.Empty;
try
{
LiveConnectClient liveClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult =
await liveClient.GetAsync("me/skydrive/camera_roll");
dynamic results = operationResult.Result;
string idfolder= results.id;
LiveOperationResult pictrueResult = await liveClient.GetAsync(idfolder+"/photos");
dynamic pictrues = pictrueResult.Result;
foreach (dynamic pictrue in pictrues.data)
{
namePictrue = pictrue.name;
idPictrue = pictrue.id;
StorageFile pictrueFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(namePictrue,CreationCollisionOption.ReplaceExisting);
await liveClient.BackgroundDownloadAsync(idPictrue+"/picture?type=thumbnail", pictrueFile);
var imgSource = new BitmapImage();
var stream = await pictrueFile.OpenReadAsync();
imgSource.SetSource(stream);
imgResult.Source = imgSource;
}
msgText.Text = "恭喜:您已成功从OneDrive下载图片!";
}
catch (LiveConnectException exception)
{
msgText.Text = "Error getting album info: " + exception.Message;
}
若要显示 OneDrive 项目的预览,请向 /skydrive/get_item_preview?type=TYPE&url=URL 发出 GET 请求。可选的 type 查询字符串参数为以下值之一: •thumbnail (缩略图) •small(为了获得最大值 100 × 100 像素预览) •album(最大 200 × 200) •normal(最大 800 × 800)
private async void creatFodel_Click(object sender, RoutedEventArgs e)
{
try
{
if (result.Session != null)
{
var folderData = new Dictionary<string, object>();
folderData.Add("name", "Test");
LiveConnectClient liveClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult =
await liveClient.PostAsync("me/skydrive", folderData);
msgText.Text = string.Join(" ", "新建文件夹", operationResult.Result["name"].ToString(),
"ID:", operationResult.Result["id"].ToString());
}
}
catch (LiveConnectException exception)
{
msgText.Text = "Error creating folder: " + exception.Message;
}
}
private async void deleteFodel_Click(object sender, RoutedEventArgs e)
{
string id = string.Empty;
LiveConnectClient liveClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult = await liveClient.GetAsync("me/skydrive/search?q=Test");
List<object> items = operationResult.Result["data"] as List<object>;
IDictionary<string, object> file = items.First() as IDictionary<string, object>;
id = file["id"].ToString();
LiveOperationResult operationResult1 =
await liveClient.DeleteAsync(id);
msgText.Text = "删除文件夹成功";
}
LiveOperationResult operationResult =
await moveFileClient.MoveAsync("file.9d24497f7fef8f33.9D24497F7FEF8F33!748",
"folder.9d24497f7fef8f33.9D24497F7FEF8F33!265");
第一参数是你要移动的文件ID,第二个参数是你要移动的文件的ID。复制的话将MoveAsync改为CopeAsync,然后俩个参数改为你想要复制前后的文件或者文件夹的ID即可。 LiveConnectClient.GetAsync在返回的 JavaScript 对象表示法 (JSON) 格式的对象中,要务必注意两个结构:id 结构,表示顶层文件夹的文件夹 ID; 如果向 me/skydrive/files 发出请求,则返回的 JSON 格式的对象仅包含有关用户顶层文件夹中所有子文件夹、子唱片集和文件的信息 要获取用户近期最常用的 OneDrive 文档列表,请使用 wl.skydrive 作用域向 /USER_ID/skydrive/recent_docs 发出 GET 请求
要获取有关用户在 OneDrive 中可用和未用存储空间的信息,请向 /me/skydrive/quota 或 /USER_ID/skydrive/quota 进行 GET 调用 FOLDER_ID/files,其中 FOLDER_ID 代表有效的文件夹 ID,可以获取该文件夹中所有项目的列表 通过使用上面代码中的 sort_by 参数指定如下条件来设置项目的排序条件:updated、name、size 或 default。例如,若要按名称对项目进行排序,请使用 FOLDER_ID/files?sort_by=name。 通过使用上面代码中的 sort_order 参数指定如下顺序来设置项目的排序顺序:ascending 或 descending。例如,若要按降序对项目进行排序,请使用 FOLDER_ID/files?sort_order=descending。
•me/albums,可以获取有关已登录用户的所有唱片集的信息。 •/me/skydrive/shared/albums,可以获取有关已登录用户的所有共享唱片集的信息。 •USER_ID/albums,可以获取有关用户的与有效 USER_ID 相对应的所有唱片集的信息。 •使用 ALBUM_ID/photos 或 ALBUM_ID/videos 获取唱片集中所有照片或视频的列表。之后,你的代码可以进行另一个调用,该调用使用特定的照片 ID 或视频 ID 获取相应照片或视频的信息。 •使用 ALBUM_ID/files 获取唱片集中所有照片、视频、音频、子唱片集和子文件夹的列表。之后,你的代码可以进行另一个调用,该调用使用特定的照片 ID、视频 ID、唱片集 ID、音频 ID 或文件夹 ID 获取相应照片、视频、唱片集、音频或文件夹的信息。 •将上面代码中带有可选 type 参数的 /picture 与下列值之一结合使用来获取照片或视频的图片:small、normal(在未指定 type 参数时为默认值)、album、thumbnail 或 full。例如,若要获取照片的缩略图,请使用 PHOTO_ID/picture?type=thumbnail。 •通过使用 VIDEO_ID/video 获取视频的内容。
遍历onedrive中所有文件并获取指定文件。
await connectClient.GetAsync(“me/skydrive/files”);
List<object> items = operationResult.Result["data"] as List<object>;
foreach (object item in items) {
IDictionary<string, object> file = item as IDictionary<string, object>;
if (file["name"].ToString() == “WorkBook.xml”)
{ id = file["id"].ToString(); }
}
http://www.bcmeng.com/bbs/forum.php?mod=viewthread&tid=188&extra=page%3D1 希望能大家通过网站的广告支持下小梦,谢谢!
WP8.1:onedrive操作,布布扣,bubuko.com
标签:des style http color java 使用 os io
原文地址:http://www.cnblogs.com/bcmeng/p/3928067.html