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:
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);
}