Discuss this help topic in SecureBlackbox Forum

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

As of time of writing, Dropbox supports authentication using OAuth and OAuth2 protocols. The Dropbox component accordingly supports both authentication methods. We strongly recommend using OAuth2 (described below), as support for original OAuth can be dropped at any moment.

To connect to Dropbox for the first time, it is necessary to take the following steps:

  1. Put App key and App secret, obtained during registration, to TElDropboxDataStorage.AppKey and TElDropboxDataStorage.AppSecret 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 TElDropboxDataStorage.HTTPClient property.
  3. Call TElDropboxDataStorage.StartAuthorization2() 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 and put the code to its AuthorizationCode property. Saving the values of these properties will let you avoid the need to ask the user authenticate again for some time.
  5. After receiving the authorization code you need to call TElDropboxDataStorage.CompleteAuthorization2() method and pass the authorization code to that method. If CompleteAuthorization2() method is executed without errors, you need to save the value of TElDropboxDataStorage.AccessToken and TElDropboxDataStorage.AccessTokenExpiration properties for their reuse in future.

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

C#:


// 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
TElDropboxDataStorage storage = new TElDropboxDataStorage();
// link it to the transport client
storage.HTTPClient = transport;

// setup redirect url
storage.RedirectURL = @"http://localhost:5050/";

// set client id and client secret parameters obtained in the Dropbox console
storage.AppKey = @"...";
storage.AppSecret = @"...";

// restore the access token and its expiration date stored in the previous session;
// usage of stored access tokens are explained in the next section
storage.AccessToken = @"...";
storage.AccessTokenExpiration = ...;

// start authorization procedure
string url = storage.StartAuthorization2();

// check if a manual authorization in a browser is required
// (no access token or expired access 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
    receiver.Activate(storage.RedirectURL);

    // start the default browser and ask it to open the authorization web page
    System.Diagnostics.Process.Start(url);

    // 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 only, we just write a dot to the console and
        // freeze the thread for a half of a second; timeout is not handled
        Console.Write(".");
        System.Threading.Thread.Sleep(500);
    }
    Console.WriteLine();

    // complete authorization using the provided code
    storage.CompleteAuthorization2(receiver.AuthorizationCode);

    // save the access token and its expiration date for further use
    Console.WriteLine("Access token: {0}\n", storage.AccessToken);
    Console.WriteLine("Valid until : {0}\n", storage.AccessTokenExpiration);
}

How To articles about Dropbox cloud

Discuss this help topic in SecureBlackbox Forum