When I was searching around for answers on creating a Box.net application for desktop trying to get the login authentication took more than that it really should have...
So I decided to put together an article on my website that talks through the process of creating a C# .Net 4.0 desktop application that can login and work with their SDK. This is using their new OAuth 2.0 login system.
Firstly we send the initial web request using a standard HttpWebRequest object to get the UI web page for the OAuth 2.0 login. Once the web response has been returned, we convert it into a Stream for our web-browser to consume. The redirect URI can be any HTTPS based URI.
string baseURI = "https://www.box.com/api/oauth2/authorize?";
string responseType = "&response_type=code";
string clientId = "&client_id=YOUR OWN CLIENT ID";
string redirectURI = "&redirect_uri=https://app.box.com/services/poc_connector";
var targetUri = new Uri(baseURI + responseType + clientId + redirectURI);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(targetUri);
To inject the Stream into the web-browser control we use the document property
webBrowser1.DocumentStream = view;
Once that is done all the operations by the user are handled by the web-browser control. To capture the Authentication token when the user presses the "Grant access" button. We add an event listener for the web-browsers Navigated event.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (e.Url.AbsolutePath != "blank" && e.Url.ToString().Contains("&code="))
{
Token = e.Url.ToString().Substring(e.Url.ToString().IndexOf("&code="));
Token = Token.Replace("&code=", String.Empty);
this.Close();
}
}
Link my original article and source code: link