The right way to login with Dropbox using API in PHP, simple tutorial with full PHP example code, create login with Dropbox to your website easily.
How To Login With Dropbox Using API
Using Dropbox OAuth 1.0 and PHP cURL, we will create Login with Dropbox to our website easily. I made PHP Script to create login with Dropbox, we will learn how to use it, and how to use Dropbox OAuth.
Live Demo
Check live demo here.
Upload files from direct link to Dropbox: http://remote-box.com
Create Dropbox App
Go to Dropbox Developers and create a new App, you will get “App Key” and “App Secret”.
Create Login With Dropbox
Download Dropbox PHP Script script, unzip it, now open “config.php” file and enter your App data:
$App_Key = "xxxxxx"; // enter your App Key $App_Secret = "xxxxxx"; // enter your App Secret $callback = "http://example.com/bla/callback.php"; // enter your callback link
Done! Now upload script to your website to test it.
How To Use Dropbox OAuth
We need PHP cURL but we will use Qassim_HTTP() Function:
/* Function By Qassim Hassan, wp-time.com */ function Qassim_HTTP($method, $url, $header, $data, $json){ if( $method == 1 ){ $method_type = 1; // 1 = POST }else{ $method_type = 0; // 0 = GET } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_HEADER, 0); if( $header !== 0 ){ curl_setopt($curl, CURLOPT_HTTPHEADER, $header); } curl_setopt($curl, CURLOPT_POST, $method_type); if( $data !== 0 ){ curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } $response = curl_exec($curl); if( $json == 0 ){ $json = $response; }else{ $json = json_decode($response, true); } curl_close($curl); return $json; }
Create new php file “login.php”, in login.php we will request token, paste this code:
<?php // Code By Qassim Hassan, wp-time.com session_start(); include "Qassim_HTTP.php"; $App_Key = "xxx"; // enter your app key $App_Secret = "xxx"; // enter your app secret $Header = array("Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"$App_Key\", oauth_signature=\"$App_Secret&\"\r\n"); $Data = 0; // data = 0 because we do not have data $JSON = 0; // json = 0 because we do not want json for this operation $Method = 1; // method = 1 because we want POST $API = Qassim_HTTP($Method, "https://api.dropbox.com/1/oauth/request_token", $Header, $Data, $JSON); $Result = explode("&", $API); // convert result to array $oauth_token_secret = str_replace("oauth_token_secret=", "", $Result[0]); // user oauth token secret $oauth_token = str_replace("oauth_token=", "", $Result[1]); // user oauth token $_SESSION["oauth_token_secret"] = $oauth_token_secret; // user oauth token secret save in session $_SESSION["oauth_token"] = $oauth_token; // user oauth token save in session $callback_url = "http://localhost/login-with-dropbox/call/callback.php"; // enter your callback link $authorize_url = "https://www.dropbox.com/1/oauth/authorize?oauth_token=$oauth_token&oauth_callback=$callback_url"; header("location: $authorize_url"); // redirect user to authorize page ?>
Now will be redirect user to Authorize page:
Now if choose “Allow”, will be redirect user to your callback page.
Create new php file “callback.php” and paste this code:
<?php // Code By Qassim Hassan, wp-time.com session_start(); include "Qassim_HTTP.php"; $App_Key = "xxx"; // enter your app key $App_Secret = "xxx"; // enter your app secret $oauth_token_secret = $_SESSION["oauth_token_secret"]; // get user oauth token secret $oauth_token = $_SESSION["oauth_token"]; // get user oauth token $Header = array("Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"$App_Key\", oauth_token=\"$oauth_token\", oauth_signature=\"$App_Secret&$oauth_token_secret\"\r\n"); $Data = 0; // data = 0 because we do not have data $JSON = 0; // json = 0 because we do not want json for this operation $Method = 1; // method = 1 because we want POST $API = Qassim_HTTP($Method, "https://api.dropbox.com/1/oauth/access_token", $Header, $Data, $JSON); $Result = explode("&", $API); // convert result to array $access_token_secret = str_replace("oauth_token_secret=", "", $Result[0]); // user access token secret $access_token = str_replace("oauth_token=", "", $Result[1]); // user access token $_SESSION["access_token_secret"] = $access_token_secret; // user access token secret save in session $_SESSION["access_token"] = $access_token; // user access token save in session header("location: success.php"); // redirect user to succes page ?>
Now will be redirect user to “Success Page”, create new php file “success.php” and paste this code:
<?php // Code By Qassim Hassan, wp-time.com session_start(); include "Qassim_HTTP.php"; $App_Key = "xxx"; // enter your app key $App_Secret = "xxx"; // enter your app secret $access_token_secret = $_SESSION["access_token_secret"]; // get user access token secret $access_token = $_SESSION["access_token"]; // get user access token $Header = array("Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"$App_Key\", oauth_token=\"$access_token\", oauth_signature=\"$App_Secret&$access_token_secret\"\r\n"); $Data = 0; // data = 0 because we do not have data $JSON = 1; // json = 1 because we want json for this operation $Method = 0; // method = 0 because we want GET $API = Qassim_HTTP($Method, "https://api.dropbox.com/1/account/info", $Header, $Data, $JSON); $Result = $API; // user info result echo $Result["display_name"]." - ".$Result["email"]; // display user full name and email! ?>
Now user has been logged using Dropbox, will be display user full name and email.
Download full example.
More
Check more Tutorials of Login with API.
1 Comment
Nicolas
Hello Qassim, first of all, great tutorial! I’ve tried many, and this is the only one that worked for me.
Now i am trying to get a listing of the folders and files in “Home” folder, and you give me a hand? thanks in advance