Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the new OAuth 2 in Box

Tags:

box-api

I'm trying to use the new Box authentication API with OAuth. I would like to use the credential of the the box account I'm currently using to authorize my application.

The configuration of OAuth requests a redirection URI and I don't know what must be entered there. In the previous authentication method, the following URI was given http://www.box.net/api/1.0/auth/{ticket}, but this was done after getting the authentication ticket.

I'm new to OAuth so my question may be a bit obvious... but I'd like to know how to do the authentication with the credentials of a box account user.

I'm doing this in a Windows application, so I would also like to understand how to show the response from the request.

like image 598
user1466502 Avatar asked Oct 22 '22 22:10

user1466502


1 Answers

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

like image 61
John117 Avatar answered Oct 25 '22 20:10

John117