Giter Club home page Giter Club logo

onedriverestapi's Introduction

OneDriveRestAPI

About

OneDriveRestAPI is a C# client for the Microsoft OneDrive service through its RESTful API.

Microsoft has released the Live SDK, a set of C# libraries for interacting with OneDrive, which is hard to extend and debug, as it relies on async HttpWebRequest/HttpWebResponse.

OneDriveRestAPI is based on John Hoerr's SkyNet project.

Contact

You can contact me on twitter @saguiitay.

NuGet

OneDriveRestAPI is available as a NuGet package

Release Notes

  • 1.3.2 Fixed usage of downsize_photo_uploads when uploading images
  • 1.2.0 Replaced GetAsync method with separate GetFolderAsync & GetFileAsync. Additional cleanup to public interfaces.
  • 1.1.0 Change the way Client is created, to allow better extendibility:
    • Added a new IHttpClientFactory witha default implementation
    • Client now accept an IRequestGenerator
    • Added Throttling mechanism to avoid overwhelming the OneDrive service, and getting errors
  • 1.0.1 Added NuGet Package.
  • 1.0.0 Initial release.

Usage

var options = new Options
    {
        ClientId = "...",
        ClientSecret = "...",
        CallbackUrl = "https://login.live.com/oauth20_desktop.srf",

        AutoRefreshTokens = true,
        PrettyJson = false,
        ReadRequestsPerSecond = 2,
        WriteRequestsPerSecond = 2
    };

// Initialize a new Client (without an Access/Refresh tokens
var client = new Client(options);

// Get the OAuth Request Url
var authRequestUrl = client.GetAuthorizationRequestUrl(new[] {Scope.Basic, Scope.Signin, Scope.SkyDrive, Scope.SkyDriveUpdate});

// TODO: Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
var authCode = "...";

// Exchange the Authorization Code with Access/Refresh tokens
var token = await client.GetAccessTokenAsync(authCode);

// Get user profile
var userProfile = await client.GetMeAsync();
Console.WriteLine("Name: " + userProfile.Name);
Console.WriteLine("Preferred Email: " + userProfile.Emails.Preferred);

// Get user photo
var userProfilePicture = await client.GetProfilePictureAsync(PictureSize.Small);
Console.WriteLine("Avatar: " + userProfilePicture);

// Retrieve the root folder
var rootFolder = await client.GetFolderAsync();
Console.WriteLine("Root Folder: {0} (Id: {1})", rootFolder.Name, rootFolder.Id);

// Retrieve the content of the root folder
var folderContent = await client.GetContentsAsync(rootFolder.Id);
foreach (var item in folderContent)
{
    Console.WriteLine("\tItem ({0}: {1} (Id: {2})", item.Type, item.Name, item.Id);
}


// Initialize a new Client, this time by providing previously requested Access/Refresh tokens
var client2 = new Client(clientId, clientSecret, callbackUrl, token.Access_Token, token.Refresh_Token);

// Find a file in the root folder
var file = folderContent.FirstOrDefault(x => x.Type == File.FileType);

// Download file to a temporary local file
var tempFile = Path.GetTempFileName();
using (var fileStream = System.IO.File.OpenWrite(tempFile))
{
    var contentStream = await client2.DownloadAsync(file.Id);
    await contentStream.CopyToAsync(fileStream);
}


// Upload the file with a new name
using (var fileStream = System.IO.File.OpenRead(tempFile))
{
    await client2.UploadAsync(rootFolder.Id, fileStream, "Copy Of " + file.Name);
}

onedriverestapi's People

Contributors

saguiitay avatar timerickson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.