This post will teach you how to integrate with Dropbox OAuth using PHP platform. Please take note that I'm not using any library to perform this integration nor special coding or whatsoever.
This is basically built from scratch which I want to share with you all, who wish to integrate with Dropbox.
To start with, you need an API credentials which you can get by logging in to your Dropbox account and create an app on this url -
https://www.dropbox.com/developers/apps
We need the credentials below to proceed:
We also need to understand the 3 url's that we need to access to complete the authentication process as listed below.
The request token url will be use to get a token which will be use to authorize your app to access users information in their behalf.
The authorization url will be the page for users to allow our application to access the users credential.
The access token url will be use to get an access token which will be use to access users credentials in their behalf.
Another thing that we need to understand is how to create signature. We need this in performing oauth request.
For the sake of this post, we will use the PLAINTEXT signature method, which is the simplest signature method.
Once we have the credentials and the api url's, we are now ready to start by
just following the simple steps below.
1. Lets request for a token from the request token url - https://api.dropbox.com/1/oauth/request_token. Since we are using plaintext signature method, the signature will be your app secret plus "%26". Please see below.
$key = '<your app key>';
$secret = '<your app secret>';
$timestamp = time();$nonce = md5(time());
$sig = $secret."%26";
$method = "PLAINTEXT";
2. Once we have all the parameters, lets compose the post request.
$url = "https://api.dropbox.com/1/oauth/request_token";
$param = "oauth_consumer_key=$key".
"&oauth_signature_method=$method".
"&oauth_signature=$sig".
"&oauth_timestamp=$timestamp".
"&oauth_nonce=$nonce".
"&oauth_version=1.0";
3. Execute post request using curl, which is the basic command in performing post request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// disable ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// submit post request parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// getting response from server
$output = curl_exec($ch);
4. Parse the output and you will get the "oauth_token" and "oauth_token_secret" below which will be use to perform authorization. You can save these information in your session so that you will retrieve it later when performing access token.
// parse the output
parse_str($output, $token);
// save to session
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['token_secret'] = $token['oauth_token_secret'];
5. From the step #4, we only need the "oauth_token" to request for authorization. For this process, we need to define our callback url in which Dropbox will redirect the users after allowing the access.
$oauth_token = $_SESSION['oauth_token'];
$callback = '<your callback url>';
$url = "https://www.dropbox.com/1/oauth/authorize";
$param = "oauth_token=$oauth_token".
"&oauth_callback=$callback";
header("Location: $url?$param");
6. After the user allows our application, Dropbox will redirect the user to our callback url we specify on step #5. We need to submit post request to access token url with the parameter "oauth_token" from step #4. This will also require signature, the same way we did on request token process, but this time with extra parameter "oauth_token_secret" from step #4.
$oauth_token = $_SESSION['oauth_token'];
$token_secret = $_SESSION['token_secret'];
$key = '<your app key>';
$secret = '<your app secret>';
$timestamp = time();$nonce = md5(time());
$sig = $secret."%26".$token_secret;
$method = "PLAINTEXT";
7. Lets compose the url and parameter with "oauth_token" as part of the request.
$url = "https://api.dropbox.com/1/oauth/access_token";
$param = "oauth_consumer_key=$key".
"&oauth_token=$oauth_token".
"&oauth_signature_method=$method".
"&oauth_signature=$sig".
"&oauth_timestamp=$timestamp".
"&oauth_nonce=$nonce".
"&oauth_version=1.0";
8. Execute the post request using curl.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// disable ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// submit post request parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// getting response from server
$output = curl_exec($ch);
9. Parse the output and we will get an access token which we will use to do the api request. You can save these information in your database so that you won't need to request for authorization when doing api request.
// parse the output
parse_str($output, $token);
// save to session
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['token_secret'] = $token['oauth_token_secret'];
You can try the full script below I made with a live API account and a basic API request. I optimized the script as well for better coding.
<?php
session_start();
$key = 'vh096l7q9m5m8tv'; // put here your app key
$secret = 'omri1uakcak8zqz'; // put here your app secret
// from callback
$oauth_token = $_GET['oauth_token'];
if ($oauth_token) {
access_token($key, $secret);
} else {
request_token($key, $secret);
}
function request_token($key='', $secret='') {
$timestamp = time();
$nonce = md5(time());
$sig = $secret."%26";
$method = "PLAINTEXT";
$url = "https://api.dropbox.com/1/oauth/request_token";
$param = "oauth_consumer_key=$key".
"&oauth_signature_method=$method".
"&oauth_signature=$sig".
"&oauth_timestamp=$timestamp".
"&oauth_nonce=$nonce".
"&oauth_version=1.0";
$output = post_request($url, $param);
// parse the output
parse_str($output, $token);
// save to session
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['token_secret'] = $token['oauth_token_secret'];
authorize();
}
function authorize() {
$oauth_token = $_SESSION['oauth_token'];
$callback = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; // put your callback url
$url = "https://www.dropbox.com/1/oauth/authorize";
$param = "oauth_token=$oauth_token".
"&oauth_callback=$callback";
header("Location: $url?$param");
}
function access_token($key='', $secret='') {
$oauth_token = $_SESSION['oauth_token'];
$token_secret = $_SESSION['token_secret'];
$timestamp = time();
$nonce = md5(time());
$sig = $secret."%26".$token_secret;
$method = "PLAINTEXT";
$url = "https://api.dropbox.com/1/oauth/access_token";
$param = "oauth_consumer_key=$key".
"&oauth_token=$oauth_token".
"&oauth_signature_method=$method".
"&oauth_signature=$sig".
"&oauth_timestamp=$timestamp".
"&oauth_nonce=$nonce".
"&oauth_version=1.0";
$output = post_request($url, $param);
// parse the output
parse_str($output, $token);
// save to session
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['token_secret'] = $token['oauth_token_secret'];
folders($key, $secret);
}
function folders($key='', $secret='') {
$oauth_token = $_SESSION['oauth_token'];
$token_secret = $_SESSION['token_secret'];
$timestamp = time();
$nonce = md5(time());
$sig = $secret."%26".$token_secret;
$method = "PLAINTEXT";
$url = "https://api.dropbox.com/1/metadata/dropbox";
$param = "oauth_consumer_key=$key".
"&oauth_token=$oauth_token".
"&oauth_signature_method=$method".
"&oauth_signature=$sig".
"&oauth_timestamp=$timestamp".
"&oauth_nonce=$nonce".
"&oauth_version=1.0";
$output = file_get_contents($url."?".$param);
$jsondata = json_decode($output);
foreach ($jsondata->contents as $contents) {
echo $contents->path."<br/>";
}
}
function post_request($url='', $param='') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// disable ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// submit post request parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// getting response from server
$output = curl_exec($ch);
return $output;
}
?>