public static IOneDriveClient GetUniversalClient( string[] scopes, string returnUrl = null, IHttpProvider httpProvider = null) { return(OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes, returnUrl, httpProvider)); }
private async void OneDriveButton_Click(object sender, RoutedEventArgs e) { var scopes = new[] { "onedrive.readwrite", "onedrive.appfolder", "wl.signin" }; SongListStorage._client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes); var session = await SongListStorage._client.AuthenticateAsync(); //Debug.WriteLine($"Token: {session.AccessToken}"); }
private async void AuthenticateClick(object sender, RoutedEventArgs e) { ShowBusy(true); Exception error = null; try { _session = null; // Using the OnlineIdAuthenticator _client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator( _scopes); // Using the WebAuthenticationBroker //_client = OneDriveClientExtensions.GetClientUsingWebAuthenticationBroker( // "000000004C172C3F", // _scopes); _session = await _client.AuthenticateAsync(); Debug.WriteLine($"Token: {_session.AccessToken}"); var dialog = new MessageDialog("You are authenticated!", "Success!"); await dialog.ShowAsync(); ShowBusy(false); } catch (Exception ex) { error = ex; } if (error != null) { var dialog = new MessageDialog("Problem when authenticating!", "Sorry!"); await dialog.ShowAsync(); ShowBusy(false); } }
/// <summary> /// 将str做备份 /// </summary> /// <param name="str">需要备份的string</param> /// <param name="st">备份的类型</param> /// <returns>返回成功与否</returns> public async static Task <string> BackupAsync(string str, SaveType st) { try { switch (st) { case SaveType.LocalState: StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile LocalStateFile = await localFolder.CreateFileAsync("data.pwsv", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(LocalStateFile, str); break; case SaveType.LocalFile: FileSavePicker picker = new FileSavePicker(); picker.DefaultFileExtension = ".pwsv"; picker.FileTypeChoices.Add("密码计算器文件", new List <string>() { ".pwsv" }); picker.SuggestedFileName = "backup"; picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; StorageFile LocalFileFile = await picker.PickSaveFileAsync(); if (LocalFileFile == null) { return("-未选择文件!"); } await FileIO.WriteTextAsync(LocalFileFile, str); break; case SaveType.RoamingData: StorageFolder RoamingFolder = ApplicationData.Current.RoamingFolder; //Debug.WriteLine(ApplicationData.Current.RoamingStorageQuota); StorageFile RoamingDataFile = await RoamingFolder.CreateFileAsync("data.pwsv", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(RoamingDataFile, str); break; case SaveType.OneDrive: try { //string[] scopes = new string[] { "wl.signin", "wl.offline_access", "onedrive.readwrite" }; string[] scopes = new string[] { "wl.signin", "onedrive.readwrite" }; var oneDriveClient = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes); await oneDriveClient.AuthenticateAsync(); byte[] array = Encoding.UTF8.GetBytes(str); MemoryStream stream = new MemoryStream(array); var uploadedItem = await oneDriveClient .Drive .Root .ItemWithPath("Documents/backup.pwsv") .Content .Request() .PutAsync <Item>(stream); } catch (Exception exc) { return("-" + exc.Message + "\n详细信息:" + exc.InnerException.Message); } break; default: return("-备份时遇到未知参数"); } return("1"); } catch (Exception ex) { return("-" + ex.Message); } }
/// <summary> /// 还原数据 /// </summary> /// <param name="st">还原的类型</param> /// <returns>还原回的string,开头为-则失败</returns> public async static Task <string> RecoverAsync(SaveType st) { try { switch (st) { case SaveType.LocalState: StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile LocalStateFile = await localFolder.GetFileAsync("data.pwsv"); if (LocalStateFile == null) { return("-本地不存在此文件"); } return(await FileIO.ReadTextAsync(LocalStateFile)); case SaveType.LocalFile: FileOpenPicker openFile = new FileOpenPicker(); openFile.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; openFile.FileTypeFilter.Add(".pwsv"); StorageFile LocalFileFile = await openFile.PickSingleFileAsync(); if (LocalFileFile == null) { return("-未选择文件"); } return(await FileIO.ReadTextAsync(LocalFileFile)); case SaveType.RoamingData: StorageFolder RoamingFolder = ApplicationData.Current.RoamingFolder; StorageFile RoamingDataFile = await RoamingFolder.GetFileAsync("data.pwsv"); return(await FileIO.ReadTextAsync(RoamingDataFile)); case SaveType.OneDrive: try { //string[] scopes = new string[] { "wl.signin", "wl.offline_access", "onedrive.readwrite" }; string[] scopes = new string[] { "wl.signin", "onedrive.readwrite" }; var oneDriveClient = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes); await oneDriveClient.AuthenticateAsync(); var item = await oneDriveClient .Drive .Root .ItemWithPath("Documents/backup.pwsv") .Request() .GetAsync(); using (var contentStream = await oneDriveClient.Drive.Items[item.Id].Content.Request().GetAsync()) { StreamReader reader = new StreamReader(contentStream); return(reader.ReadToEnd()); } } catch (Exception exc) { return("-" + exc.Message + "\n详细信息:" + exc.InnerException.Message); } default: return("-还原时遇到未知参数"); } } catch (Exception ex) { return("-" + ex.Message); } }
public void InitInstance() { oneDriveClient = OneDriveClientExtensions .GetClientUsingOnlineIdAuthenticator(new string[] { "onedrive.appfolder", "offline_access" }); }
private async void OnOneDriveLogin(object sender, RoutedEventArgs e) { _client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(Scopes); await _client.AuthenticateAsync(); }