dcblogdev/laravel-dropbox

Laravel Dropbox v2 包

v3.0.6 2024-03-12 11:06 UTC

This package is auto-updated.

Last update: 2024-09-13 21:58:07 UTC


README

Latest Version on Packagist Total Downloads

Logo

社区

有一个 Discord 社区。 https://discord.gg/VYau8hgwrm 为了快速帮助,请在适当的频道提问。

简介

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

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 Key 和 App Secret 到您的 .env 文件中

DROPBOX_CLIENT_ID=
DROPBOX_SECRET_ID=

现在输入您想要的回调 URL。这是您的应用将用于连接到 Dropbox API 的 URL。

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

安装

通过 Composer

composer require dcblogdev/laravel-dropbox

配置

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

php artisan vendor:publish --provider="Dcblogdev\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="Dcblogdev\Dropbox\DropboxServiceProvider" --tag="migrations" 迁移发布后,您可以通过运行迁移来创建令牌表

php artisan migrate

确保在 .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=

用法

注意,此包期望用户已登录。

注意:以下示例假设使用的是 oauth2 进行身份验证,并没有直接在 .env 中设置访问令牌。

如果直接设置访问代码,不要依赖于 Dropbox::getAccessToken()

路由示例

Route::group(['middleware' => ['web', 'auth']], function(){
    Route::get('dropbox', function(){

        if (! Dropbox::isConnected()) {
            return redirect(env('DROPBOX_OAUTH_URL'));
        } else {
            //display your details
            return Dropbox::post('users/get_current_account');
        }

    });

    Route::get('dropbox/connect', function(){
        return Dropbox::connect();
    });

    Route::get('dropbox/disconnect', function(){
        return Dropbox::disconnect('app/dropbox');
    });

});

或者使用中间件路由,如果用户没有图形令牌,则自动重定向以进行认证

Route::group(['middleware' => ['web', 'DropboxAuthenticated']], function(){
    Route::get('dropbox', function(){
        return Dropbox::post('users/get_current_account');
    });
});

Route::get('dropbox/connect', function(){
    return Dropbox::connect();
});

Route::get('dropbox/disconnect', function(){
    return Dropbox::disconnect('app/dropbox');
});

认证后,您可以使用 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)

数组不是必需的,其需求取决于被调用的端点,有关更多详细信息,请参阅 API 文档。

当使用时,$headers 是可选的,可以传递额外的头部信息。

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

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

Dropbox::post('users/get_current_account')

中间件

要限制路由只对认证用户开放,有一个名为 DropboxAuthenticated 的中间件路由

将 DropboxAuthenticated 添加到路由以确保用户已认证

Route::group(['middleware' => ['web', 'DropboxAuthenticated'], function()

要访问令牌模型,请引用此 ORM 模型

use Dcblogdev\Dropbox\Models\DropboxToken;

文件

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

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

导入命名空间

use Dcblogdev\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接受贡献。

Pull Requests

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

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

  • 每个功能一个Pull Request - 如果你想做更多的事情,请发送多个Pull Request。

安全性

如果你发现任何与安全相关的问题,请通过dave@dcblog.dev发送电子邮件,而不是使用问题跟踪器。

许可证

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