zanderwar/lumen-dropbox

Lumen Dropbox v2 包

v1.0.0 2022-06-04 06:02 UTC

This package is auto-updated.

Last update: 2024-09-04 11:12:15 UTC


README

这是dcblocdev/laravel-dropbox的一个想要继承者,以兼容Lumen 8/9。

所有工作归原作者所有。我只是一个普通人,一个简单的人...需要一些改变。

Latest Version on Packagist Total Downloads

Laravel包,用于与Dropbox v2 API一起工作。

Dropbox API文档可以在以下位置找到:https://www.dropbox.com/developers/documentation/http/documentation

应用程序注册

要在Dropbox API中使用应用程序,需要在https://www.dropbox.com/developers/apps创建一个应用程序。

创建一个新的应用程序,选择Dropbox API或Dropbox Business API。然后选择所需的访问类型,无论是应用文件夹(用于隔离到单个文件夹)还是完整Dropbox。

接下来,将APP密钥和应用密钥复制并粘贴到您的.env文件中

DROPBOX_CLIENT_ID=
DROPBOX_SECRET_ID=

现在输入您想要的重定向URL。这是您的应用程序将用于连接到Dropbox API的URL。

一个常见的URL是https://domain.com/dropbox/connect

安装

通过Composer

composer require zanderwar/lumen-dropbox:^1.0

配置

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Zanderwar\Dropbox\DropboxServiceProvider" --tag="config"

发布后,config/dropbox.php配置文件包含,请确保发布此文件并将作用域更改为您Dropbox应用的作用域,在Dropbox应用控制台中。

<?php

return [

    /*
    * set the client id
    */
    'clientId' => env('DROPBOX_CLIENT_ID'),

    /*
    * set the client secret
    */
    'clientSecret' => env('DROPBOX_SECRET_ID'),

    /*
    * Set the url to trigger the oauth process this url should call return Dropbox::connect();
    */
    'redirectUri' => env('DROPBOX_OAUTH_URL'),

    /*
    * Set the url to redirecto once authenticated;
    */
    'landingUri' => env('DROPBOX_LANDING_URL', '/'),

    /**
     * Set access token, when set will bypass the oauth2 process
     */
    'accessToken' => env('DROPBOX_ACCESS_TOKEN', ''),

    /**
     * Set access type, options are offline and online
     * Offline - will return a short-lived access_token and a long-lived refresh_token that can be used to request a new short-lived access token as long as a user's approval remains valid.
     *
     * Online - will return a short-lived access_token
     */
    'accessType' => env('DROPBOX_ACCESS_TYPE', 'offline'),

    /*
    set the scopes to be used
    */
    'scopes' => 'account_info.read files.metadata.write files.metadata.read files.content.write files.content.read',
];

迁移

您可以使用以下命令发布迁移

php artisan vendor:publish --provider="Zanderwar\Dropbox\DropboxServiceProvider" --tag="migrations" 迁移发布后,您可以通过运行迁移来创建tokens表

php artisan migrate

.ENV配置确保您已经在.env文件中设置了以下内容

DROPBOX_CLIENT_ID=
DROPBOX_SECRET_ID=
DROPBOX_OAUTH_URL=https://domain.com/dropbox/connect
DROPBOX_LANDING_URL=https://domain.com/dropbox
DROPBOX_ACCESS_TYPE=offline

绕过Oauth2您可以通过在dropbox应用中生成访问令牌并将其输入到.env文件中来绕过oauth2过程

DROPBOX_ACCESS_TOKEN=

用法

此包将令牌与当前认证用户关联。这意味着必须在连接/断开连接路由上启用auth中间件。或者,您可以在用户返回之前使用Passport::actingAs($user)在调用connect()之前。

如果您需要将任意数据传递给dropbox(这可能有助于防止CSRF攻击或包含对后处理有用的其他信息),则可以在Dropbox::connect(string $state = null)方法中使用state参数。这将导致dropbox在用户认证后向您返回此值。

注意

  • 如果打算使用state参数,则您的路由不得使用dropbox.auth中间件。
  • 如果直接设置访问代码,不要依赖于Dropbox::getAccessToken()。

路由示例

$router->group([/** Middleware etc */], function(\Laravel\Lumen\Routing\Router $router) {
    $router->get('/dropbox/connect', function() {
        if (! request()->has('code')) {
            // user is heading to dropbox
            $arbitraryData = ['hello' => 'world'];
            return \Zanderwar\Dropbox\Dropbox::connect(json_encode($arbitraryData));     
        }
        
        // user is returning from dropbox
        $state = request()->input('state');
        
        if (! $state) {
            throw new \RuntimeException('The state parameter is missing from the request.');
        }
        
        // do stuff with $state...
        // e.g. Passport::actingAs(Decrypt::json($state)->user_id)
        
        // finalise the account internally, by calling the connect method again. This will fetch
        // an access/refresh token from dropbox. This behaviour is only exhibited when the `code`
        // query param is in the URL.
        return \Zanderwar\Dropbox\Dropbox::connect();
    });

    $router->get('/dropbox/disconnect', function() {
        return Dropbox::disconnect('path/to/redirect/to');
    });

});

一旦认证,您可以调用Dropbox::使用以下动词

Dropbox::get($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::post($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::put($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::patch($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::delete($endpoint, $array = [], $headers = [], $useToken = true)

$array不是始终必需的,其需求由被调用的端点确定,请参阅API文档以获取更多详细信息。

当使用时,$headers是可选的,可以传递额外的标题。

当设置为true时,$useToken是可选的,将使用授权标题,默认为true。

这些期望传递API端点,URL https://api.dropboxapi.com/2/ 已提供,仅应使用此URL之后的端点,例如

Dropbox::post('users/get_current_account')

中间件

为了仅允许认证用户访问,您可以注册以下中间件并将其分配给您的路由。

bootstrap/app.php

app()->middleware([
    'dropbox.auth' => \Zanderwar\Dropbox\Http\Middleware\DropboxAuthMiddleware::class
]);

routes/web.php

$router->group(['middleware' => ['dropbox.auth']], function (\Laravel\Lumen\Routing\Router $router) {
    // your routes
});

要访问令牌模型,请参考此ORM模型

use Zanderwar\Dropbox\Models\DropboxToken;

文件

此包提供了一种处理文件的方式。

要处理文件,首先调用 ->files(),然后调用一个方法。

导入命名空间

use Zanderwar\Dropbox\Facades\Dropbox;

列出内容

列出指定路径的文件和文件夹

Dropbox::files()->listContents($path = '')

列出内容继续

使用前一个listContents调用的游标分页处理下一组文件夹/文件。

Dropbox::files()->listContentsContinue($cursor = '')

删除文件夹/文件 传递文件/文件夹的路径,在删除文件夹时,所有子项都将被删除。

Dropbox::files()->delete($path)

创建文件夹 传递要创建的文件夹的路径。

Dropbox::files()->createFolder($path)

搜索文件 每个单词都将用于搜索文件。

Dropbox::files()->search($query)

上传文件 通过传递文件夹路径和文件名上传文件到Dropbox。注意,此方法仅支持最多150MB的上传。

Dropbox::files()->upload($path, $file)

下载文件 通过传递包含文件的文件夹路径从Dropbox下载文件。

Dropbox::files()->download($path)

移动文件夹/文件 移动接受4个参数

$fromPath - 提供现有文件夹/文件的路径 $toPath - 提供现有文件夹/文件的新路径,必须以/开头 $autoRename - 如果存在冲突,让Dropbox服务器尝试自动重命名文件以避免冲突。此字段的默认值为false。 $allowOwnershipTransfer - 即使移动内容会导致所有权转移,也允许所有者移动。这不适用于复制。此字段的默认值为false。

Dropbox::files()->move($fromPath, $toPath, $autoRename = false, $allowOwnershipTransfer = false);

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

欢迎贡献,并将得到充分认可。

贡献可以通过Github上的Pull Requests进行。

拉取请求

  • 记录任何行为变化 - 确保readme.md和其他相关文档保持最新。

  • 考虑我们的发布周期 - 我们试图遵循SemVer v2.0.0。随机破坏公共API不是可选项。

  • 每个功能一个拉取请求 - 如果您要完成多项任务,请发送多个拉取请求。

安全性

如果您发现任何安全相关的问题,请通过reece.alexander@gmail.com发送电子邮件,而不是使用问题跟踪器。

许可协议

许可协议。请参阅许可文件以获取更多信息。