jhoerr / box-csharp-sdk-v2

A C# client for the Box API (v2).
http://developers.box.com/docs/
11 stars 15 forks source link

Hey! Don't Use This!

Box has released an official .Net SDK. As such I (jhoerr) will no longer be actively developing this SDK. Please migrate your application as appropriate.

Box C# SDK (API v2)

This is a C# client implementation of the Box v2 REST API. Please feel free to open an issue if you find a bug or would like to request a feature.

License

Creative Commons Attribution 3.0 Unported License

Nuget

This client is available on Nuget. There is also an MVC-based example of the Box OAuth2 authentication flow available.

Release Notes

2.0 (21 Mar 2013)

1.3 (30 Jan 2013)

1.2 (15 Jan 2013)

1.1 (11 Jan 2013)

1.0 (25 Dec 2012)

Known Issues

Usage Example

/*******************/
/** Authorization **/
/*******************/

// Create a OAuth2 access/refresh token provider using your API key/secret.
var tokenProvider = new TokenProvider("apiKey", "apiSecret");

// Fetch the initial token pair using the OAuth2 authorization code...
// You will want to persist these new values for later use.
var initialTokenPair = tokenProvider.GetAccessToken("code");

// You can also refresh the token pair.
// You will want to persist these new values for later use.
var newTokenPair = tokenProvider.RefreshAccessToken("refreshToken");

/*********************/
/** Box Interaction **/
/*********************/

// Instantiate a BoxManager client.
var boxManager = new BoxManager(newTokenPair.AccessToken);

// Create a new file in the root folder
boxManager.CreateFile(Folder.Root, "a new file.txt", Encoding.UTF8.GetBytes("hello, world!"));

// Fetch the root folder
var folder = boxManager.GetFolder(Folder.Root);

// Find a 'mini' representation of the created file among the root folder's contents
var file = folder.Files.Single(f => f.Name.Equals("a new file.txt"));

// Get the file with all properties populated.
file = boxManager.Get(file);

// Rename the file
file = boxManager.Rename(file, "the new name.txt");

// Create a new subfolder
var subfolder = boxManager.CreateFolder(Folder.Root, "my subfolder");

// Move the file to the subfolder
file = boxManager.Move(file, subfolder);

// Write some content to the file
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("goodbye, world!")))
{
    file = boxManager.Write(file, stream);
}

// Read the contents to a stream and write them to the console
using (var stream = new MemoryStream())
{
    boxManager.Read(file, stream);
    using (var reader = new StreamReader(stream))
    {
        stream.Position = 0;
        Console.Out.WriteLine("File content: '{0}'", reader.ReadToEnd());
    }
}

// Delete the folder and its contents
boxManager.Delete(subfolder, recursive: true);

As an enterprise administrator you can create a client and perform Box operations on behalf of another user.

// Instantiate a BoxManager client.
var boxManager = new BoxManager("AccessToken", onBehalfOf: "user@domain.com");

// ... do stuff as that user
// ... use your power only for awesome!

Copyright

(c) 2012-2013 John Hoerr, The Trustees of Indiana University