...
This has to be made with the Authorization
header set to the correct value using the App key and App secret.
To get and process the request token, we'll use a pair of functions. The first will call the request_token endpoint in the Dropbox API. The second will process the response.
Info |
---|
We're writing this app to work within the CloudOS UI framework, so we'll be using CloudOS UI functions to interact with the user. If you're not familiar with writing CloudOS, applications, there is a recipe available in the cookbook. |
Getting the access token requires POSTing to the endpoint with the correct Authorization header. The following rule, which is selected when the user launches the application we're writing inside CloudOS and fires when the application is not authorized to access Dropbox does that:
Code Block | ||||
---|---|---|---|---|
| ||||
rule get_access_token {
select when oauth response
if(not authorized) then {
http:post(dropbox_base_url+"/oauth/request_token") with
body = {} and
headers = {"Authorization" : create_oauth_header_value(keys:dropbox('app_key'), keys:dropbox('app_secret'))
} and
autoraise = "request_token"
}
} |
The POST has no body, uses the function we defined above for creating the Authorization
header, and automatically raises a new event with the result of the POST which we expect to have the request token and request token secret.
When the http:post()
action autoraises an event, the event has event domain http
and event type post
. The value of the autoraise
parameter in the action above will be in the event attribute label
so that we can write a rule that responds specifically to this autoraise. The following rule is selected by that autoraised event, creates a UI panel with a "Click to Link Dropbox" button, and saves the request token and request token secret in entity variables for later use.
Code Block | ||||
---|---|---|---|---|
| ||||
rule process_request_token {
select when http post label "request_token"
pre {
tokens = decode_content(event:attr('content'));
callback = 'http://' + meta:host() + '/blue/event/oauth/response/' + meta:rid() + '/' + math:random(999999);
url = "https://www.dropbox.com/1/oauth/authorize?oauth_token=" + tokens{'oauth_token'} + "&oauth_callback=" + callback;
my_html = <<
<div style="margin: 0px 0px 20px 20px">
<a href="#{url}" class="btn btn-large btn-primary">Click to Link to Dropbox<a>
</div>
>>;
}
CloudRain:createLoadPanel("Link to Dropbox", {}, my_html);
always {
set ent:request_token_secret tokens{'oauth_token_secret'};
set ent:request_token tokens{'oauth_token'};
}
} |
The tokens are in the event attribute content and are URL encoded. You can see how decode_content()
works in another recipe. The callback is the event we want Dropbox to raise when it responds with the access token. The event is an oauth:response
event. We take pains using meta:host()
and meta:rid()
to ensure that this code is portable between KRE instances and rulesets. The form of the URL that calls Dropbox is described in their documentation.