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 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
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
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, 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.CompleteAuthorization(receiver.AuthorizationCode);
}
// save the refresh token for further use
Console.WriteLine("Refresh token: {0}\n", storage.RefreshToken);