This recipe describes how to connect a KRL application to Dropbox using the OAuth 1.0a protocol that Dropbox supports. This recipe should be readily adaptable to other APIs using OAuth 1.0a and will offer hints at how to do OAuth 2, which is considerably easier to support.
We will be using the PLAINTEXT signature method in OAuth 1.0a which means that this is only secure over SSL. Since Dropbox uses SSL for all API interactions, this won't be a problem.
Registering Our Application with Dropbox
The first step is to register our application with Dropbox and get our App key and App secret. Go to the Dropbox Developer Home and click on the App Console menu item on the left. Click the "Create an app" button and fill in the fields. We'll be creating a "Core" application. The Core API is what allows programatic access to the linked Dropbox account. You'll be asked whether you want full access to the user's Dropbox or just a single directory in the /Apps
directory. For purposes of this recipe, I chose the a single directory. At this point, you should have an App key and App secret from Dropbox. You'll put these in the Keys section of the meta directory of your ruleset:
key dropbox { "app_key" : "<dropbox app key>", "app_secret" : "<dropbox app secret>" }
Creating the OAuth Header Value
We'll have to create an OAuth Authorization
header for various interactions with the Dropbox server. This is a complicated string that is better to create using a function than by hand each time it's needed. For getting the request token, the header is simpler and looks like this:
Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_signature="<app-secret>&"
For getting the access token and interactions with the API, we need to add the token and token secret like so:
Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_token="<request-token>", oauth_signature="<app-secret>&<request-token-secret>"
This function will create both. If the function is given just two parameters, then it creates the first Authorization
header, if given four, it creates the second Authentication header shown above:
create_oauth_header_value = function(key, key_secret, token, token_secret) { 'OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'+ key + (token => '", oauth_token="'+token+'", ' | '", ') + 'oauth_signature="' + key_secret + '&' + token_secret + '"'; }
We'll use this function often in the step below.
Get a Request Token
The Dropbox API provides an endpoint for getting request tokens:
POST https://api.dropbox.com/1/oauth/request_token
This has to be made with the Authorization
header set to the correct value using the App key and App secret.