上一年年底寫過兩篇文章
UWP 使用OneDrive雲存儲2.x api(一)【全網首發】
UWP 使用OneDrive雲存儲2.x api(二)【全網首發】
沒想到半年之后,VS編譯提示方法已經過時了,可見微軟朝三暮四,氣走開發者的傳言,並不假😂😂😂
不過新升級后的OneDrive service更加好用了,但是我沒感覺出來🙄🙄🙄
下面就來和大家分享一下新版的使用方法。
要把大象裝,啊呸,哪有大象🐘了。要想使用OneDrive API,攏共分三步🚶
1. 注冊應用程序ID
2. 授權程序功能
3. 使用OneDrive API
好吧,開始
1. 注冊應用程序ID
去 https://apps.dev.microsoft.com/,在已聚合的應用旁邊,選擇添加應用,按照向導走,注意選擇“Mobile and Desktop App”.
完成之后應該就有下圖了。
注意記住這個應用ID,一會要用到。最好的辦法就是收藏一下這個頁面咯。
2. 授權程序功能
打開 Package.appxmanifest,進入“功能”頁面,勾選 ✔專用網絡(客戶端和服務端)。如圖
3. 使用OneDrive API
3.0 下載NuGet包
打開NuGet,搜索Microsoft.Toolkit.Uwp.Services,安裝3.0版本或以上。
友情提示:4.0還有可能會變哦
3.1 初始化
一句話搞定
string[] scopes = new string[] { MicrosoftGraphScope.FilesReadWriteAppFolder};
OneDriveService.Instance.Initialize("剛才申請的那個應用ID", scopes, null, null);
scopes是使用的權限,我的App只需要使用OneDrive下的應用程序文件夾,所以就是這個了。當然還有其它的權限,比如 Files.Read.All,Files.ReadWrite.All等,詳見MicrosoftGraphScope下面的枚舉。
3.2 登錄
核心也是一句話
if (await OneDriveService.Instance.LoginAsync()) { OneDriveStorageFolder oneDriveAppFolder = await OneDriveService.Instance.AppRootFolderAsync(); TipServices.TipAuthenticateSuccess(); } else { TipServices.TipAuthenticateFail(); throw new Exception("Unable to sign in"); }
在登錄成功后,我馬上獲取了OneDrive下面的應用程序文件夾。別的文件夾堅決不訪問,不做流氓行為。堅決不向BAT看齊。
3.3 獲取文件
循環獲取
var OneDriveItems = await folder.GetItemsAsync(); do { OneDriveItems = await folder.NextItemsAsync(); } while (OneDriveItems != null);
3.4 創建文件夾
string newFolderName = await OneDriveSampleHelpers.InputTextDialogAsync("New Folder Name"); if (!string.IsNullOrEmpty(newFolderName)) { await folder.StorageFolderPlatformService.CreateFolderAsync(newFolderName, CreationCollisionOption.GenerateUniqueName); }
3.5 進入子文件夾
var currentFolder = await _graphCurrentFolder.GetFolderAsync(item.Name); OneDriveItemsList.ItemsSource = await currentFolder.GetItemsAsync(20); _graphCurrentFolder = currentFolder;
3.6 移動、復制、重命名項目
await _onedriveStorageItem.MoveAsync(targetonedriveStorageFolder); await _onedriveStorageItem.CopyAsync(targetonedriveStorageFolder); await _onedriveStorageItem.RenameAsync("NewLevel3");
3.7 創建/上傳小於4M的文件
var selectedFile = await OpenLocalFileAsync(); if (selectedFile != null) { using (var localStream = await selectedFile.OpenReadAsync()) { var fileCreated = await folder.StorageFolderPlatformService.CreateFileAsync(selectedFile.Name, CreationCollisionOption.GenerateUniqueName, localStream); } }
3.8 創建/上傳大於4M的文件
var selectedFile = await OpenLocalFileAsync(); if (selectedFile != null) { using (var localStream = await selectedFile.OpenReadAsync()) { Shell.Current.DisplayWaitRing = true; // If the file exceed the Maximum size (ie 4MB) var largeFileCreated = await folder.StorageFolderPlatformService.UploadFileAsync(selectedFile.Name, localStream, CreationCollisionOption.GenerateUniqueName, 320 * 1024); } } }
至於為什么非要區分,而且是4M為分界線,我也不清楚。好像GayHub上討論過,有興趣可以去查下。
3.9 下載文件
var oneDriveFile = (Toolkit.Services.OneDrive.OneDriveStorageFile)item; using (var remoteStream = (await oneDriveFile.StorageFilePlatformService.OpenAsync()) as IRandomAccessStream) { await SaveToLocalFolder(remoteStream, oneDriveFile.Name); }
3.10 獲取縮略圖
var file = (Toolkit.Services.OneDrive.OneDriveStorageItem)((AppBarButton)e.OriginalSource).DataContext; using (var stream = (await file.StorageItemPlatformService.GetThumbnailAsync(Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ThumbnailSize.Large)) as IRandomAccessStream) { await OneDriveSampleHelpers.DisplayThumbnail(stream, "thumbnail"); }
關於OneDrive的API操作基本都在這了。
如果你覺得微軟的OneDrive客戶端非常渣渣,那么看完這個,你也完全可以寫一個OneDrive 的App,然后發布到商店。
到時別忘記@我一下,我也用用。