如何在不强制用户登录所用 Dropbox 帐户的情况下使用 Dropbox Chooser

How to use Dropbox Chooser without forcing the user to login to the Dropbox account used

我正在将 Dropbox Chooser 放在一起。这个想法是站点用户可以访问 Dropbox 按钮,并且可以下载他们想要的任何文件。 当我登录到有问题的帐户时,这工作正常,但我不能要求用于登录到该帐户。我不知道需要更改什么,使用的域已添加到 Dropbox 应用程序设置中,这是我的测试代码:

<!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>

试试这个 this link

可能对您有所帮助。 首先你必须注册你的应用程序, 然后 Dropbox 会为您提供一些密钥: 将其保存在您应用的 authorize.php 文件中,例如

<?php
$access_token = array (
  "oauth_token_secret" => "abcdefghilmnopqr",
  "oauth_token" => "stuvwxyzabcdefgh",
  "uid" => "1234567"
);

您的第一次申请是 运行,index.php 文件中的以下条件将为真:

<?php
if (!isset($access_token)) {
    header("Location: authorize.php");
    exit;
}

authorize.php 会自动为您管理。

当您的应用想要访问最终用户的 Dropbox 中的文件时,将使用选择器。如果您要做的是从您自己的 Dropbox 中向用户提供文件,您应该改用 Core API

根据之前的回答,我尝试在 http://www.sitepoint.com/access-dropbox-using-php/ 实施该解决方案,但是该解决方案允许我创建文件和文件夹列表,但不能为任何文件创建临时下载 link。

我结束了 https://github.com/phpmasterdotcom/AccessDropboxUsingPHP 代码起源的地方,并通过一些定制能够创建一个系统,显示以面包屑样式访问的文件夹,使文件夹可点击,以便用户可以导航到子文件夹并返回并在文件中生成 link。

这是我所做的:

  1. 已在 https://github.com/phpmasterdotcom/AccessDropboxUsingPHP

    下载文件
    1. 创建了一个投递箱应用程序(在上面的第一个 link 中有很好的说明)
    2. 我创建了一个使用 sample.php 作为基础的文件,它将列出文件和文件夹,我将其命名为 index.php 在这里,您将需要更改应用程序详细信息。 请注意,Dropbox 请求 URL 和 https.

    // 这两行只是为了启用错误报告和禁用输出缓冲(不要在你的应用程序中包含它!) error_reporting(E_ALL); ini_set('display_errors', 1); //enable_implicit_flush(); // -- 不需要的东西结束

    // 如果您的 Dropbox 中有很多文件,这可能需要一些时间,因此请禁用最大。执行时间处理时间 set_time_limit(0);

    require_once("DropboxClient.php");

    // 您必须在 https://www.dropbox.com/developers/apps 创建一个应用程序并在下面输入详细信息: $dropbox = new DropboxClient(数组( 'app_key' => "xxxxxxxxxxxxxxxxxx", 'app_secret' => "yyyyyyyyyyyyyyyyyyyyyy", 'app_full_access' => 是的, ),'en');

    // 首先尝试加载现有的访问令牌 $access_token = load_token("access"); 如果(!空($access_token)){ $dropbox->SetAccessToken($access_token); //回声"loaded access token:"; //print_r($access_token); } elseif(!empty($_GET['auth_callback'])) // 我们来自 dropbox 的授权页面吗? { // 然后加载我们之前创建的请求令牌 $request_token = load_token($_GET['oauth_token']); 如果(空($request_token))死('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']);
    

    }

    // 检查是否需要访问令牌 如果(!$dropbox->IsAuthorized()) { // 将用户重定向到 Dropbox 授权页面 $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']); 死("Authentication required. Click here."); }

    //设置请求的当前路径 $path = '';//从根文件夹开始 如果 (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 .= ' &gt;&gt; <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); //然后遍历文件 returned 分离文件夹和文件 如果(空($文件)) { echo "

    抱歉,所选文件夹中当前没有文件

    ";

                }
                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('/', ' &gt;&gt; ', $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 else
    

    函数store_token($token, $name) { 如果(!file_put_contents("tokens/$name.token",序列化($token))) die('
    无法存储令牌!确保目录tokens存在且可写!'); }

    函数load_token($名称) { 如果(!file_exists("tokens/$name.token"))return空; return @unserialize(@file_get_contents("tokens/$name.token")); }

    函数delete_token($名称) { @unlink("tokens/$name.token"); }

    ?>

    1. 最后我创建了一个名为 download_file 的文件,它创建了可下载的 link:

    // 这两行只是为了启用错误报告和禁用输出缓冲(不要在你的应用程序中包含它!) error_reporting(E_ALL); ini_set('display_errors', 1); //enable_implicit_flush(); // -- 不需要的东西结束

    // 如果您的 Dropbox 中有很多文件,这可能需要一些时间,因此请禁用最大。执行时间处理时间 set_time_limit(0);

    require_once("DropboxClient.php");

    // 您必须在 https://www.dropbox.com/developers/apps 创建一个应用程序并在下面输入详细信息: $dropbox = new DropboxClient(数组( 'app_key' => "xxxxxxxxxxxxxxxxxxxxxxx", 'app_secret' => "yyyyyyyyyyyyyyyyyyyyyy", 'app_full_access' => 是的, ),'en');

    // 首先尝试加载现有的访问令牌 $access_token = load_token("access"); 如果(!空($access_token)){ $dropbox->SetAccessToken($access_token); //回声"loaded access token:"; //print_r($access_token); } elseif(!empty($_GET['auth_callback'])) // 我们来自 dropbox 的授权页面吗? { // 然后加载我们之前创建的请求令牌 $request_token = load_token($_GET['oauth_token']); 如果(空($request_token))死('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']);
    

    }

    // 检查是否需要访问令牌 如果(!$dropbox->IsAuthorized()) { // 将用户重定向到 Dropbox 授权页面 $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']); 死("Authentication required. Click here."); }

    //设置请求的当前路径

    if (isset($_GET['path']))
    {
        $path = $_GET['path'];
    
        header("Location: ". $dropbox->GetLink($path));
    
    }
    else
    {
        echo "<p>No path passed on to the script</p>";
    
    }
    

    函数store_token($token, $name) { 如果(!file_put_contents("tokens/$name.token",序列化($token))) die('
    无法存储令牌!确保目录tokens存在且可写!'); }

    函数load_token($名称) { 如果(!file_exists("tokens/$name.token"))return空; return @unserialize(@file_get_contents("tokens/$name.token")); }

    函数delete_token($名称) { @unlink("tokens/$name.token"); }