forked from sa4250mnpo70/dropbox-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorize.php
executable file
·96 lines (80 loc) · 2.99 KB
/
authorize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env php
<?php
require_once __DIR__."/../lib/Dropbox/strict.php";
if (PHP_SAPI !== "cli") {
throw new \Exception("This program was meant to be run from the command-line and not as a web app. Bad value for PHP_SAPI. Expected \"cli\", given \"".PHP_SAPI."\".");
}
// NOTE: You should be using Composer's global autoloader. But just so these examples
// work for people who don't have Composer, we'll use the library's "autoload.php".
require_once __DIR__.'/../lib/Dropbox/autoload.php';
use \Dropbox as dbx;
if ($argc === 1) {
echoHelp($argv[0]);
die;
}
if($argc !== 3) {
fwrite(STDERR, "Expecting exactly 2 arguments, got ".($argc - 1)."\n");
fwrite(STDERR, "Run with no arguments for help\n");
die;
}
$argAppInfoFile = $argv[1];
$argAuthFileOutput = $argv[2];
try {
list($appInfoJson, $appInfo) = dbx\AppInfo::loadFromJsonFileWithRaw($argAppInfoFile);
}
catch (dbx\AppInfoLoadException $ex) {
fwrite(STDERR, "Error loading <app-info-file>: ".$ex->getMessage()."\n");
die;
}
// This is a command-line tool (as opposed to a web app), so we can't supply a redirect URI.
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "examples-authorize", "en");
$authorizeUrl = $webAuth->start();
echo "1. Go to: $authorizeUrl\n";
echo "2. Click \"Allow\" (you might have to log in first).\n";
echo "3. Copy the authorization code.\n";
echo "Enter the authorization code here: ";
$authCode = \trim(\fgets(STDIN));
list($accessToken, $userId) = $webAuth->finish($authCode);
echo "Authorization complete.\n";
echo "- User ID: $userId\n";
echo "- Access Token: $accessToken\n";
$authArr = array(
"access_token" => $accessToken,
);
if (array_key_exists('host', $appInfoJson)) {
$authArr['host'] = $appInfoJson['host'];
}
$json_options = 0;
if (defined('JSON_PRETTY_PRINT')) {
$json_options |= JSON_PRETTY_PRINT; // Supported in PHP 5.4+
}
$json = json_encode($authArr, $json_options);
if (file_put_contents($argAuthFileOutput, $json) !== false) {
echo "Saved authorization information to \"$argAuthFileOutput\".\n";
}
else {
fwrite(STDERR, "Error saving to \"$argAuthFileOutput\".\n");
fwrite(STDERR, "Dumping to stderr instead:\n");
fwrite(STDERR, $json);
fwrite(STDERR, "\n");
die;
}
function echoHelp($command) {
echo "\n";
echo "Usage: $command <app-info-file> <auth-file-output>\n";
echo "\n";
echo "<app-info-file>: A JSON file with information about your API app. Example:\n";
echo "\n";
echo " {\n";
echo " \"key\": \"Your Dropbox API app key\",\n";
echo " \"secret\": \"Your Dropbox API app secret\",\n";
echo " }\n";
echo "\n";
echo " Get an API app key by registering with Dropbox:\n";
echo " https://dropbox.com/developers/apps\n";
echo "\n";
echo "<auth-file-output>: If authorization is successful, the resulting API\n";
echo " access token will be saved to this file, which can then be used with\n";
echo " other example programs, such as \"examples/account-info.php\".\n";
echo "\n";
}