Tools

Namespaces

Variants

Views

Actions

Search

Contents

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Some user may want to connect other cloud services than SkyDrive which is offered by Microsoft. By using Open Source project called Sharpbox, you can access to Dropbox clouds. At the time of writing this article, it is still not possible to access other clouds like CloudMe in Windows Phone although those others works with desktops.

+

''SharpBox'' is an open source project that provides access to ''Dropbox'' cloud services on Windows Phone. This article covers basic functionality of Sharpbox library: how to use it and how to wrap its synchronous functions to work with Windows Phone. This provides a useful complement or addition to Microsoft's SkyDrive cloud services.

+

{{Note|SharpBox also provides access to other cloud services like ''CloudMe''. However at time of writing this article these services work on Desktop only (not Windows Phone)}}

−

== Summary ==

+

== Using SharpBox ==

+

=== Getting SharpBox ===

−

== Required software ==

+

SharpBox is available as a [http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c NuGet] package or for download from codeplex [http://sharpbox.codeplex.com/ here]. If you download a Sharpbox release, extract the downloaded zip file. In Visual Studion add '''AppLimit.CloudComputing.SharpBox.dll''' and '''Newtonsoft.Json.Silverlight.dll''' files from '''sl3-wp''' folder as reference to your project.

−

Go to Sharpbox website: http://sharpbox.codeplex.com/

+

{{Warning|The newer release (1.2) has few bugs when logging and authenticating to Dropbox service. Mainly, token exchanging uses synchronous functions which do not work in Windows Phone. Earlier versions have functions to log in with credentials, those will work only with older Dropbox v0 API. If you have Dropbox key & secret for older API, older library works just fine. But if you create Dropbox application now, you will get Dropbox v1 key and secret and those do not work with older API.}}

−

Download SharpBox release 1.1 SR2 or older. Newer release 1.2 does not work so well with Windows Phone and have few bugs when logging and authenticating to Dropbox service.

+

=== Prerequisites ===

−

Extract downloaded zip file and add two DLL, '''AppLimit.CloudComputing.SharpBox.dll'' and '''Newtonsoft.Json.Silverlight.dll'''', files from '''sl3-wp''' folder as reference to your project.

+

SharpBox needs a valid Dropbox application key and secret (for release, not required during testing and developing).These can be obtained in the [http://www.dropbox.com/developers developer section] of the Dropbox website.

−

== Sharpbox prerequisities ==

+

=== SharpBox should not run in UI thread ===

−

Sharpbox needs valid Dropbox application key and secret. These you can make in Dropbox website, developers section where you create own application.

+

The SharpBox library has both synchronous and and asynchronous functions for many operations. Both forms are useful for desktop usage, but the synchronous versions cannot be used directly in Windows Phone apps because these calls block the running UI thread and therefore the whole application.

−

Dropbox developers: https://www.dropbox.com/developers

+

The asynchronous functions can be used in the UI thread. If needed functions only exist in synchronous variants it is possible to run these in another (non UI) thread and return the results using a callback.

−

== Using Sharpbox ==

+

The following snippet shows how to use [http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher%28v=vs.95%29.aspx Dispatcher] to call {{Icode|parseFilesAndDirectories}} function - a normal private function in application thread. Example function {{Icode|CallbackFunction()}} is asynchronous callback function.

−

Sharpbox library has synchronized and asynchronized functions for many operations. For desktop usage both are fine but in Windows Phone, operating system and it's frameworks has their own limitations. All of the synchronized calls will block running UI thread and therefore the whole application. The only way to get things working is to use asynchronous functions. Some needed functions still lack of asynchronous versions but it is possible to use these by implementing own async versions to own application.

+

<code csharp>

−

+

−

=== Sharpbox does not run in UI thread ===

+

−

+

−

Note that used functions from Sharpbox are asynchronous and those are not run in UI thread where your application is running. Still it is possible to call your application functions or change member variables from callback functions by using dispatcher.

+

−

+

−

Example function '''CallbackFunction''' is asynchronous callback function. This shows how to use dispatcher to call '''parseFilesAndDirectories''' function what is normail private function in application thread.

+

−

+

−

<code cpp>

+

void CallbackFunction(IAsyncResult result)

void CallbackFunction(IAsyncResult result)

{

{

−

Deployment.Current.Dispatcher.BeginInvoke(() =>

+

Deployment.Current.Dispatcher.BeginInvoke(() =>

−

{

+

{

−

if (fs != null)

+

if (fs != null)

−

{

+

{

−

parseFilesAndDirectories(fs);

+

parseFilesAndDirectories(fs);

−

}

+

}

−

});

+

});

}

}

</code>

</code>

−

=== Logging to Dropbox ===

+

== Logging to Dropbox ==

−

This is pretty easy, create credentials and configuration object. Then call '''BeginOpenRequest''' and wait for callback.

+

This is pretty easy, create credentials and configuration object. Then call {{Icode|BeginOpenRequest()}} and wait for callback.

−

<code cpp>

+

=== Dropbox API v0, Sharpbox 1.1 and older ===

−

public void ConnectCloud(string username, string password)

+

<code csharp>

−

{

+

public void ConnectCloud(string username, string password)

+

{

DropBoxCredentials creds = new DropBoxCredentials();

DropBoxCredentials creds = new DropBoxCredentials();

creds.ConsumerSecret = APP_SECRET;

creds.ConsumerSecret = APP_SECRET;

Line 82:

Line 75:

m_dropBox.BeginOpenRequest(LoginCallback, mCloudConfig, creds);

m_dropBox.BeginOpenRequest(LoginCallback, mCloudConfig, creds);

−

}

+

}

</code>

</code>

−

And the callback function for open request. Token of Dropbox connection is extracted from asynchronous functions result. This token also determines did connection succeed; if value is null, connection failed and in other values it succeeded. Without having token value, cannot continue because Sharpbox library does not know is service there.

+

=== Dropbox API v1, Sharpbox 1.2 ===

+

From Sharpbox 1.2, usage of '''DropBoxTokenIssuer.exe''' has been deprecated. Instead the access token is generated in code.

−

<code cpp>

+

===Getting Security Token===

−

void LoginCallback(IAsyncResult result)

+

====Generate a request token====

−

{

+

In the first phase a request token has to be generated based on the app key and app secret issued by Dropbox in your developer account interface. The interface will be available after registration under [http://www.dropbox.com/developers developer console].

{{Note|For testing purpose you can put a breakpoint at {{Icode|AuthorizationUrl}} and manually browse to it to allow or deny access.}}

+

+

====Exchange the request token into an access token====

+

The last phase converts the request token (which is only be usable during the authorization process) into an issued access token which can be stored on a local cache and has to be reused during login phase when the user comes back with your application. The following code performs the exchange process:

This is the callback function for the open request. The Dropbox connection token is extracted from the asynchronous function's result. This token also determines whether the connection succeeded; if the value is null, connection failed and with other values it succeeded.

+

+

The token is required because without it the the SharpBox library does not know there is a service and will not continue.

+

+

<code csharp>

+

void LoginCallback(IAsyncResult result)

+

{

ICloudStorageAccessToken token = m_dropBox.EndOpenRequest(result);

ICloudStorageAccessToken token = m_dropBox.EndOpenRequest(result);

if (token != null)

if (token != null)

Line 99:

Line 178:

m_dropBox.BeginGetRootRequest(RootCallback);

m_dropBox.BeginGetRootRequest(RootCallback);

}

}

−

}

+

}

−

<code>

+

</code>

== Get Dropbox file listing ==

== Get Dropbox file listing ==

−

After successful connection, we can proceed to reading files from Dropbox. As seen in '''LoginCallback''', '''RootCallback''' function is callback for '''BeginGetRootRequest''' asynchronous function.Before reading any file from Dropbox, you need to know root where start. After having root, it's possible to read files from anywhere. So reading root folder is important only in the beginning. And from reading root callback, again jump to another async callback function.

+

After successful connection, we can proceed to reading files from Dropbox. As seen in {{Icode|LoginCallback()}}, {{Icode|RootCallback()}} function is callback for {{Icode|BeginGetRootRequest()}} asynchronous function. Before reading any file from Dropbox, you need to know root where start. After having root, it's possible to read files from anywhere. So reading root folder is important only in the beginning. And from reading root callback, again jump to another async callback function.

−

<code cpp>

+

<code csharp>

−

private void RootCallback(IAsyncResult result)

+

private void RootCallback(IAsyncResult result)

−

{

+

{

−

ICloudDirectoryEntry root = m_dropBox.EndGetRootRequest(result);

+

ICloudDirectoryEntry root = m_dropBox.EndGetRootRequest(result);

if (root != null)

if (root != null)

{

{

m_dropBox.BeginGetChildsRequest(ChildCallback, root);

m_dropBox.BeginGetChildsRequest(ChildCallback, root);

}

}

−

}

+

}

</code>

</code>

−

After having child objects of root, any file or dir in topmost level, start parsing results.

+

After having child objects of root, any file or directory in topmost level, start parsing results.

Although SharpBox has synchronous function to get file system objects from Dropbox, those won't work in Windows Phone because again, synchronous functions will block UI-thread and therefore whole application. To fix this problem, implement and asynchronous wrapper for the synchronous function. This wrapping code will run the synchronous function in separate thread.

+

+

Implement helper functions, {{Icode|GetFileUri}} is entry point to start work. This function constructs request and sets callback function to thread, what is sent to {{Icode|ThreadPool}}.

After a while, callback function will be fired and we can try to get Dropbox file object URL. Note that this is not in your application (UI-thread) anymore. Use dispatcher if you need to communicate the URL to your application.

SharpBox is an open source project that provides access to Dropbox cloud services on Windows Phone. This article covers basic functionality of Sharpbox library: how to use it and how to wrap its synchronous functions to work with Windows Phone. This provides a useful complement or addition to Microsoft's SkyDrive cloud services.

Note: SharpBox also provides access to other cloud services like CloudMe. However at time of writing this article these services work on Desktop only (not Windows Phone)

SharpBox is available as a NuGet package or for download from codeplex here. If you download a Sharpbox release, extract the downloaded zip file. In Visual Studion add AppLimit.CloudComputing.SharpBox.dll and Newtonsoft.Json.Silverlight.dll files from sl3-wp folder as reference to your project.

Warning: The newer release (1.2) has few bugs when logging and authenticating to Dropbox service. Mainly, token exchanging uses synchronous functions which do not work in Windows Phone. Earlier versions have functions to log in with credentials, those will work only with older Dropbox v0 API. If you have Dropbox key & secret for older API, older library works just fine. But if you create Dropbox application now, you will get Dropbox v1 key and secret and those do not work with older API.

The SharpBox library has both synchronous and and asynchronous functions for many operations. Both forms are useful for desktop usage, but the synchronous versions cannot be used directly in Windows Phone apps because these calls block the running UI thread and therefore the whole application.

The asynchronous functions can be used in the UI thread. If needed functions only exist in synchronous variants it is possible to run these in another (non UI) thread and return the results using a callback.

The following snippet shows how to use Dispatcher to call parseFilesAndDirectories function - a normal private function in application thread. Example function CallbackFunction() is asynchronous callback function.

In the first phase a request token has to be generated based on the app key and app secret issued by Dropbox in your developer account interface. The interface will be available after registration under developer console.

The last phase converts the request token (which is only be usable during the authorization process) into an issued access token which can be stored on a local cache and has to be reused during login phase when the user comes back with your application. The following code performs the exchange process:

This is the callback function for the open request. The Dropbox connection token is extracted from the asynchronous function's result. This token also determines whether the connection succeeded; if the value is null, connection failed and with other values it succeeded.

The token is required because without it the the SharpBox library does not know there is a service and will not continue.

After successful connection, we can proceed to reading files from Dropbox. As seen in LoginCallback(), RootCallback() function is callback for BeginGetRootRequest() asynchronous function. Before reading any file from Dropbox, you need to know root where start. After having root, it's possible to read files from anywhere. So reading root folder is important only in the beginning. And from reading root callback, again jump to another async callback function.

Although SharpBox has synchronous function to get file system objects from Dropbox, those won't work in Windows Phone because again, synchronous functions will block UI-thread and therefore whole application. To fix this problem, implement and asynchronous wrapper for the synchronous function. This wrapping code will run the synchronous function in separate thread.

Implement helper functions, GetFileUri is entry point to start work. This function constructs request and sets callback function to thread, what is sent to ThreadPool.

After a while, callback function will be fired and we can try to get Dropbox file object URL. Note that this is not in your application (UI-thread) anymore. Use dispatcher if you need to communicate the URL to your application.