Install-Package Microsoft.OneDriveSDK
string[] scopes = { "wl.signin", "onedrive.readwrite" }; IOneDriveClient _client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes); await _client.AuthenticateAsync();
await _client.SignOutAsync();
string[] scopes = { "wl.signin", "onedrive.readwrite" }; IOneDriveClient _client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes); AccountSession session = await _client.AuthenticateAsync(); if (!_client.IsAuthenticated) return; Item item = await _client .Drive .Root .ItemWithPath("CodeExamples/MyDemo.zip") .Request() .GetAsync(); using (Stream contentStream = await _client .Drive .Items[item.Id] .Content .Request() .GetAsync()) { StorageFile file = await ApplicationData.Current.LocalFolder. CreateFileAsync("MyDemo.zip", CreationCollisionOption.OpenIfExists); using (Stream outputstream = await file.OpenStreamForWriteAsync()) { await contentStream.CopyToAsync(outputstream); } }
string[] scopes = { "wl.signin", "onedrive.readwrite" }; IOneDriveClient _client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(scopes); AccountSession acse = await _client.AuthenticateAsync(); if (!_client.IsAuthenticated) return; FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.FileTypeFilter.Add(".jpg"); StorageFile file = await fileOpenPicker.PickSingleFileAsync(); if (file != null) { using (Stream contentStream = await file.OpenStreamForReadAsync()) { var uploadedItem = await _client .Drive .Root .ItemWithPath("CodeExamples/" + file.Name) .Content .Request() .PutAsync<Item>(contentStream); } }
IChildrenCollectionPage ic= await _client .Drive .Root .Children .Request() .GetAsync();
Uri uri = new Uri("https://api.onedrive.com/v1.0/drive/root/children"); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", session.AccessToken); string jsonstring = await client.GetStringAsync(uri);
Uri uri = new Uri("https://api.onedrive.com/v1.0/drive/root:/CodeExamples/MyDemo.zip:/action.createLink"); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", session.AccessToken); var requestJson = JsonConvert.SerializeObject( new RequestLinkInfo { type = "view" }); var content = new StringContent(requestJson,Encoding.UTF8,"application/json"); var response = await client.PostAsync(uri,content); LinkResponseInfo result = JsonConvert.DeserializeObject<LinkResponseInfo>( await response.Content.ReadAsStringAsync());
public class RequestLinkInfo { public string type { get; set; } // : view, edit embed public string scope { get; set; } // optional - : anonymous organization } public class LinkResponseInfo { public string id { get; set; } public string[] roles { get; set; } public Link link { get; set; } } public class Link { public string type { get; set; } public string scope { get; set; } public string webUrl { get; set; } public OneDriveApplication application { get; set; } } public class OneDriveApplication { public string id { get; set; } public string displayName { get; set; } }
string fileurl = result.link.webUrl;
Uri uri = new Uri("https://apis.live.net/v5.0/me"); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", session.AccessToken); string jsonstring = await client.GetStringAsync(uri);
Source: https://habr.com/ru/post/307130/
All Articles