问题
I am putting a Dropbox Chooser together. The idea is that site users will have access to the Dropbox button and will be able to download any file they want. This is working fine when I login to the account in question but I can't ask used to login to this account. I can't figure out what needs to be changed, the domain used is added to the Dropbox app settings, here is my test code:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="xxxxxxxxxxxx"></script>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
</head>
<body>
<div id="container"></div>
<script>
var options = {
linkType: "direct",
multiselect: true,
success: function(files) {
for(i = 0; i< files.length; i++){
var link = document.createElement('a');
var br = document.createElement('br');
link.href = files[i].link;
link.textContent = files[i].link;
document.getElementById('container').appendChild(br);
document.getElementById('container').appendChild(link);
}
}
};
var button = Dropbox.createChooseButton(options);
Dropbox.choose(options);
document.getElementById('container').appendChild(button);
file = {
// Name of the file.
name: "filename.txt",
// URL to access the file, which varies depending on the linkType specified when the
// Chooser was triggered.
link: "https://...",
// Size of the file in bytes.
bytes: 464,
// URL to a 64x64px icon for the file based on the file's extension.
icon: "https://...",
// A thumbnail URL generated when the user selects images and videos.
// If the user didn't select an image or video, no thumbnail will be included.
thumbnailLink: "https://...?bounding_box=75&mode=fit",
};
</script>
</body>
</html>
回答1:
try this this link
it may help you . first you have to register your app , then Dropbox will provide you some keys: keep that in authorize.php file of your app e.g
<?php
$access_token = array (
"oauth_token_secret" => "abcdefghilmnopqr",
"oauth_token" => "stuvwxyzabcdefgh",
"uid" => "1234567"
);
The first time your application is run, the following condition in the index.php file will be true:
<?php
if (!isset($access_token)) {
header("Location: authorize.php");
exit;
}
the authorize.php will manage automatically for you .
回答2:
The Chooser is used when your app wants to gain access to a file in the end user's Dropbox. If what you're trying to do is offer users files from your own Dropbox, you should probably use the Core API instead.
回答3:
Following previous answers I have tried to implement the solution at http://www.sitepoint.com/access-dropbox-using-php/ however this solution allowed me to create a list of files and folders but not create a temporary download link for any files.
I ended going to https://github.com/phpmasterdotcom/AccessDropboxUsingPHP where the codes originated and with a little tailoring was able to create a system showing the folder accessed in a breadcrumb style, have the folders clickable so users can navigate to subfolders and back and have a link generated to a file.
Here is what I have done:
Downloaded the files at https://github.com/phpmasterdotcom/AccessDropboxUsingPHP
- Created a drop box app (there is good instructions on doing so on the first link above)
I have created a file using sample.php as a base that will list the files and folders, I have called it index.php and here it is, you will need to change the app detail. Please note that Dropbox request a URL with an https.
- DropPHP sample *
- http://fabi.me/en/php-projects/dropphp-dropbox-api-client/ *
- @author Fabian Schlieper
- @copyright Fabian Schlieper 2012
- @version 1.1
- @license See license.txt * */
// these 2 lines are just to enable error reporting and disable output buffering (don't include this in you application!) error_reporting(E_ALL); ini_set('display_errors', 1); //enable_implicit_flush(); // -- end of unneeded stuff
// if there are many files in your Dropbox it can take some time, so disable the max. execution time set_time_limit(0);
require_once("DropboxClient.php");
// you have to create an app at https://www.dropbox.com/developers/apps and enter details below: $dropbox = new DropboxClient(array( 'app_key' => "xxxxxxxxxxxxxxxxxx", 'app_secret' => "yyyyyyyyyyyyyyyyyyyyyy", 'app_full_access' => true, ),'en');
// first try to load existing access token $access_token = load_token("access"); if(!empty($access_token)) { $dropbox->SetAccessToken($access_token); //echo "loaded access token:"; //print_r($access_token); } elseif(!empty($_GET['auth_callback'])) // are we coming from dropbox's auth page? { // then load our previosly created request token $request_token = load_token($_GET['oauth_token']); if(empty($request_token)) die('Request token not found!');
// get & store access token, the request token is not needed anymore $access_token = $dropbox->GetAccessToken($request_token); store_token($access_token, "access"); delete_token($_GET['oauth_token']);}
// checks if access token is required if(!$dropbox->IsAuthorized()) { // redirect user to dropbox auth page $return_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?auth_callback=1"; $auth_url = $dropbox->BuildAuthorizeUrl($return_url); $request_token = $dropbox->GetRequestToken(); store_token($request_token, $request_token['t']); die("Authentication required. Click here."); }
//set the current path requested $path = '';//start on the root folder if (isset($_GET['path'])) { $path = $_GET['path'];
} $dir = '<a href="https://secure.mydomain.com/mydropbox/index.php">Root</a>'; if (strpos($path,'/')!== false) { $rowDir = explode('/',$path); $increment_path = ''; if (count($rowDir) !=0) { foreach($rowDir AS $dl) { if ($dl !='') { $increment_path .= '/'. $dl; $dir .= ' >> <a href="https://secure.mydomain.com/mydropbox/index.php?path='.$increment_path.'">'.$dl.'</a>' ; } } }//end if }//end if echo '<p class="path"><strong>Current Path:</strong> ' . $dir . '</p>';$files = $dropbox->GetFiles($path,false); //then loop through the files returned separting folders and files if (empty($files)) { echo "
Sorry, there is currently no files in the folder selected
";} else { //loop through the array returned showing the folders first followed by the files $thisFiles = '';//store files $thisDir = '';//store directories foreach($files AS $file) { //see if it is a file or directory //we remove the first slash from the path $thisPath = $file->path; $lastMod = $file->modified; //take the time out of the last modified date $lastMod = substr($lastMod,0,strpos($lastMod,':')); $lastMod = substr($lastMod,0,strrpos($lastMod,' ')); //get the files name using the path $fileName = $thisPath; //remove the first / if (substr($fileName,0,1) == '/') { $fileName = substr($fileName,1); } //then replace the further slashes with >> $fileName = str_replace('/', ' >> ', $fileName); if ($file->is_dir==1) { //this is a directory $thisDir .= '<li class="directory"><a href="https://secure.mydomain.com/mydropbox/index.php?path='. $thisPath.'">' . $fileName. "</a></li>"; } else { //in order to avoid tool many calls we create the link in a separate file, this could very easily be done as follows: //$thisFiles .= '<li class="file"><a href="'.$dropbox->GetLink($thisPath).'" target="_blank">' . $fileName. '</a><br><span class="last_update">Modified '.$lastMod.'</span></li>'; $thisFiles .= '<li class="file"><a href="https://secure.mydomain.com/mydropbox/download_file.php?path='.$thisPath .'" target="_blank">' . $fileName. '</a><br><span class="last_update">Modified '.$lastMod.'</span></li>'; } } echo '<ul id="file_list">'.$thisDir . $thisFiles.'</ul>'; }//end elsefunction store_token($token, $name) { if(!file_put_contents("tokens/$name.token", serialize($token))) die('
Could not store token! Make sure that the directorytokensexists and is writable!'); }function load_token($name) { if(!file_exists("tokens/$name.token")) return null; return @unserialize(@file_get_contents("tokens/$name.token")); }
function delete_token($name) { @unlink("tokens/$name.token"); }
?>
finaly I have created a file called download_file which creates the downloadable link:
- DropPHP sample *
- http://fabi.me/en/php-projects/dropphp-dropbox-api-client/ *
- @author Fabian Schlieper
- @copyright Fabian Schlieper 2012
- @version 1.1
- @license See license.txt * */
// these 2 lines are just to enable error reporting and disable output buffering (don't include this in you application!) error_reporting(E_ALL); ini_set('display_errors', 1); //enable_implicit_flush(); // -- end of unneeded stuff
// if there are many files in your Dropbox it can take some time, so disable the max. execution time set_time_limit(0);
require_once("DropboxClient.php");
// you have to create an app at https://www.dropbox.com/developers/apps and enter details below: $dropbox = new DropboxClient(array( 'app_key' => "xxxxxxxxxxxxxxxxxxxxxxx", 'app_secret' => "yyyyyyyyyyyyyyyyyyyyyy", 'app_full_access' => true, ),'en');
// first try to load existing access token $access_token = load_token("access"); if(!empty($access_token)) { $dropbox->SetAccessToken($access_token); //echo "loaded access token:"; //print_r($access_token); } elseif(!empty($_GET['auth_callback'])) // are we coming from dropbox's auth page? { // then load our previosly created request token $request_token = load_token($_GET['oauth_token']); if(empty($request_token)) die('Request token not found!');
// get & store access token, the request token is not needed anymore $access_token = $dropbox->GetAccessToken($request_token); store_token($access_token, "access"); delete_token($_GET['oauth_token']);}
// checks if access token is required if(!$dropbox->IsAuthorized()) { // redirect user to dropbox auth page $return_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?auth_callback=1"; $auth_url = $dropbox->BuildAuthorizeUrl($return_url); $request_token = $dropbox->GetRequestToken(); store_token($request_token, $request_token['t']); die("Authentication required. Click here."); }
//set the current path requested
if (isset($_GET['path'])) { $path = $_GET['path']; header("Location: ". $dropbox->GetLink($path)); } else { echo "<p>No path passed on to the script</p>"; }function store_token($token, $name) { if(!file_put_contents("tokens/$name.token", serialize($token))) die('
Could not store token! Make sure that the directorytokensexists and is writable!'); }function load_token($name) { if(!file_exists("tokens/$name.token")) return null; return @unserialize(@file_get_contents("tokens/$name.token")); }
function delete_token($name) { @unlink("tokens/$name.token"); }
来源:https://stackoverflow.com/questions/29697791/how-to-use-dropbox-chooser-without-forcing-the-user-to-login-to-the-dropbox-acco