Discuss this help topic in SecureBlackbox Forum

Clouds: Connect to Box service and authenticate for the first time

To connect to Box.com for the first time, it's necessary to take the following steps:

  1. Put client_id and client_secret, obtained during registration, to TElBoxDataStorage.ClientID and TElBoxDataStorage.ClientSecret properties respectively. Also you need to set RedirectURL property to the value of the URL, specified as a Redirect URL during registration of your application.
  2. Create an instance of TElHTTPSClient, which will be used as a transport, and put a reference to the newly created instance to TElBoxDataStorage.HTTPClient property.
  3. Call TElBoxDataStorage.StartAuthorization() and obtain the URL, to which the browser should be directed. This is the address of the page, on which the user will be authenitcated.
  4. After the user is authenticated, the browser will be redirected to the URL, specified as a Redirect URL during registration of your application. The server, that handles the URL, should extract the authorization code from the requested URL. The authorization code is passed in "code" parameter of the URL.
    Instead of using an external HTTP server you can use TElOauth2RedirectReceiver component, which will do the job.
  5. After receiving the authorization code you need to call TElBoxDataStorage.CompleteAuthorization() method and pass the authorization code to that method. If CompleteAuthorization method is executed without errors, you need to save the value of TElBoxDataStorage.RefreshToken property. The refresh token will let you avoid re-authentication in the consequent sessions.

The code below shows how to authenticate the user with help of TElOauth2RedirectReceiver component.


// create a HTTP client for transport purpose
TElHTTPSClient transport = new TElHTTPSClient();

// set SSL versions to TLS 1.x
transport.Versions = SBSSLConstants.__Global.sbTLS1 |
    SBSSLConstants.__Global.sbTLS11 | SBSSLConstants.__Global.sbTLS12;

// provide an event handler for validating SSL certificate(s)
transport.OnCertificateValidate += ...;

// actually, create a data storage object
TElBoxDataStorage storage = new TElBoxDataStorage();

// link to the transport client
storage.HTTPClient = transport;

// set client id and client secret parameters obtained in the Box console
storage.ClientID = @"...";
storage.ClientSecret = @"...";

// restore the refresh token stored in the previous session;
// refresh tokens are explained in the next section
storage.RefreshToken = @"...";

// start authorization procedure, passing the redirect URL
string url = storage.StartAuthorization(@"http://localhost:2180/");

// check if a manual authorization in a browser is required
// (no refresh token or expired refresh token provided)
if (!String.IsNullOrEmpty(url))
    Console.WriteLine("Authorization required");

    // create a redirect receiver to get an authorization code
    TElOAuth2RedirectReceiver receiver = new TElOAuth2RedirectReceiver();

    // activate the receiver on the specified redirect URL

    // start the default browser and ask it to open the authorization web page

    // wait until the user authorizes in the browser and
    // grants access to his/her account on the Box server
    while (!receiver.Receive())
        // for demo purpose, we just write a dot to the console and
        // freeze the thread for a half of a second; timeout is not handled

    // complete authorization using the provided code

// save the refresh token for further use
Console.WriteLine("Refresh token: {0}\n", storage.RefreshToken);

How To articles about Box.com cloud

Discuss this help topic in SecureBlackbox Forum