Skip to content

Instantly share code, notes, and snippets.

@mafice
Created February 26, 2012 17:10
Show Gist options
  • Save mafice/1917806 to your computer and use it in GitHub Desktop.
Save mafice/1917806 to your computer and use it in GitHub Desktop.
try to use Dropbox API
1. pear channel-discover pear.dropbox-php.com
2. pear install --all-deps dropbox-php/Dropbox-beta
3. install the OAuth library with PEAR
4. get and set CONSUMER_KEY_* and CONSUMER_SECRET_* from https://dropbox.com/developers/apps
5. run this script!
<?php
//
// Constants
//
const CONSUMER_KEY_FULL = '';
const CONSUMER_SECRET_FULL = '';
const CONSUMER_KEY_APP = '';
const CONSUMER_SECRET_APP = '';
// ----------------------------
session_start();
require_once 'Dropbox/autoload.php';
//
//
// Full Dropbox
//
//
print "<h2>Full Dropbox</h2>";
$auth = auth(CONSUMER_KEY_FULL, CONSUMER_SECRET_FULL);
$oauth = $auth['oauth'];
switch($auth['state']){
case 'getAuthURL':
print "the authorize page is at ".$auth['authURL'].PHP_EOL;
exit();
}
$dropbox = new Dropbox_API($oauth, 'dropbox');
//
// upload this script file
//
$dropbox->putFile(basename($_SERVER['PHP_SELF']), basename($_SERVER['PHP_SELF']));
//
// show files and directories in the root directory
//
$root = $dropbox->getMetaData('/');
$dirs = $root['contents'];
foreach($dirs as $dir){
$name = basename($dir['path']);
print <<< HDOC
<b>{$name}</b><br>
revision:&nbsp;{$dir['revision']}<br>
modified:&nbsp;{$dir['modified']}<br>
<br>
HDOC;
}
print "<br><hr><br>";
//
//
// App folder
//
//
print "<h2>App Folder</h2>";
$auth = auth(CONSUMER_KEY_APP, CONSUMER_SECRET_APP);
$oauth = $auth['oauth'];
switch($auth['state']){
case 'getAuthURL':
print "the authorize page is at ".$auth['authURL'].PHP_EOL;
exit();
}
$dropbox = new Dropbox_API($oauth, 'sandbox');
//
// upload this script file
//
$dropbox->putFile(basename($_SERVER['PHP_SELF']), basename($_SERVER['PHP_SELF']));
//
// show files and directories in the root directory
//
$root = $dropbox->getMetaData('/');
$dirs = $root['contents'];
foreach($dirs as $dir){
$name = basename($dir['path']);
print <<< HDOC
<b>{$name}</b><br>
revision:&nbsp;{$dir['revision']}<br>
modified:&nbsp;{$dir['modified']}<br>
<br>
HDOC;
}
exit();
//
//
// Function: auth
//
//
function auth ($consumerKey, $consumerSecret){
$oauth = new Dropbox_OAuth_PEAR($consumerKey, $consumerSecret);
$stateStr = '';
$authURL = '';
//
// You can use the OAuth extention in PECL.
// This script uses PEAR's one.
//
//return new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
// There are multiple steps in this workflow, we keep a 'state number' here
$state = (isset($_SESSION[$consumerKey]['oauth_state']))? $_SESSION[$consumerKey]['oauth_state'] : 1;
switch($state){
//
// State 1: get authorize page URL
//
case 1:
$_SESSION[$consumerKey]['oauth_state'] = 2;
$_SESSION[$consumerKey]['oauth_tokens'] = $oauth->getRequestToken();
$stateStr = 'getAuthURL';
$authURL = $oauth->getAuthorizeUrl();
break;
//
// State 2: get OAuth tokens
//
case 2:
$oauth->setToken($_SESSION[$consumerKey]['oauth_tokens']);
$_SESSION[$consumerKey]['oauth_state'] = 3;
$_SESSION[$consumerKey]['oauth_tokens'] = $oauth->getAccessToken();
$stateStr = 'getOAuthTokens';
// go to State 3 without break to set OAuth tokens
//
// State 3: set OAuth tokens
//
case 3:
$oauth->setToken($_SESSION[$consumerKey]['oauth_tokens']);
$stateStr = 'setOAuthTokens';
break;
}
return array(
'state' => $stateStr,
'authURL' => $authURL,
'oauth' => $oauth
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment