Andrew H
Andrew H

Reputation: 225

Using the Box Windows (.NET) V2 API in Web C# project

Is it possible to use the following SDK for a Web Application:

https://github.com/box/box-windows-sdk-v2

The specs say it is targeted for the .NET framework for Windows and Windows Phone applications, but I wasn't able to figure it out for a Web .NET project. Is this SDK strictly for native Windows and Windows phones applications?

I've never used await/async functions in C# and that's possibly where I can't figure it out. I've been able to successfully get an oauth2 token/refresh token, but don't know where to go from here. Could anyone take a look or provide a sample of how to retrieve folder details?

Thanks !

Upvotes: 0

Views: 4590

Answers (1)

letstango
letstango

Reputation: 799

It is absolutely possible to use the Box Windows SDK in an ASP.NET web application.

I've recently updated the readme documentation to mention that the SDK supports the .NET 4.0 framework, so as long as your project is targeting that framework or above you should be good to go. If you are running an express version of Visual Studio, you unfortunately will not be able to open the SDK source project as it's built as a PCL (Portable Class Library). You can, however, still get the binaries through nuget.

One important thing you have to remember when using async/await calls in ASP.NET is that you have to include the Async="true" attribute in your Page declaration:

<%@ Page Language="C#" Async="true" %>

You mentioned that you were able to successfully get OAuth2 tokens/refresh tokens. I'm not sure if you wrote your own workflow to retrieve those tokens, but the SDK supports handling the second step of exchanging an auth code for tokens as follows:

public async Task Authenticate(string authCode)
    {
        BoxConfig config = new BoxConfig("YOUR_CLIENT_KEY", "YOUR_CLIENT_SECRET", new Uri("YOUR_REDIRECT"));
        BoxClient client = new BoxClient(config);
        await client.Auth.AuthenticateAsync("authCode");
    }

Note that when using async/await, you must decorate your method signature with the "async" keyword. Microsoft has written great articles on further understanding the async/await keywords.

In the case that you built your own OAuth2 workflow, the SDK also accepts a completed OAuth Session. Here's a full sample of that, and an example of getting items in your root level folder:

    public partial class WebForm1 : System.Web.UI.Page
{
    BoxClient _client;

    protected async void Page_Load(object sender, EventArgs e)
    {

        BoxConfig config = new BoxConfig("YOUR_CLIENT_KEY", "YOUR_CLIENT_SECRET", new Uri("https://YOUR_REDIRECT"));
        BoxClient client = new BoxClient(config);
        OAuthSession session = new OAuthSession("YOUR_ACCESS_TOKEN", "YOUR_REFRESH_TOKEN", 3600, "bearer");

        _client = new BoxClient(config, session);
    }

    protected async void Button_Click(object sender, EventArgs e)
    {
        BoxFolder folder = await _client.FoldersManager.GetItemsAsync("0", 10);
        string test = folder.Name;
    }
}

Upvotes: 2

Related Questions