racenationcc / laravel-dropbox
一个 Laravel Dropbox v2 包
Requires
- guzzlehttp/guzzle: ^6|^7
- illuminate/support: ^5.5|^5.6|^5.7|^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
README
由 dcblogdev/laravel-dropbox 分支而来
一个用于与 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 密钥和 App 密码到您的 .env 文件中
DROPBOX_CLIENT_ID=
DROPBOX_SECRET_ID=
现在输入您想要的重定向 URL。这是您的应用将用于连接到 Dropbox API 的 URL。
一个常见的 URL 是 https://domain.com/dropbox/connect
安装
通过 Composer
composer require racenationcc/laravel-dropbox
配置
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="racenationcc\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 */ 'refreshToken' => env('DROPBOX_REFRESH_TOKEN', ''), /** * Set access token, when set will bypass the oauth2 process */ 'accessToken' => env('DROPBOX_ACCESS_TOKEN', ''), /** * The root folder of where the dropbox API will write too */ 'rootFolder' => env('DROPBOX_ROOT_FOLDER', '/'), /** * 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="racenationcc\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 - 短期
您可以通过生成访问令牌来绕过 oauth2 流程,Dropbox 不再发布长期访问令牌,但以下方法对于快速测试应用上的权限更改非常有用。
在您的应用页面上生成访问令牌后,将其输入 .env 文件中
DROPBOX_ACCESS_TOKEN=
绕过 Oauth2 - 长期
要绕过长期 Oauth,您可以保存您的刷新令牌。这些方法有参数可以强制它们使用此刷新令牌获取新的访问令牌(目前这会在每次请求时获取新的访问令牌)。
要生成刷新令牌
- 转到授权 URL,用您的 https://www.dropbox.com/oauth2/authorize?client_id={APPKEY}&response_type=code&token_access_type=offline
- 登录并复制授权码
- 使用 Curl 将授权码交换为访问令牌和刷新令牌,用相应的值替换 {AUTHCODE}、{APPKEY} 和 {APPSECRET}
curl https://api.dropbox.com/oauth2/token \
-d code={AUTHCODE} \
-d grant_type=authorization_code \
-u {APPKEY}:{APPSECRET}
- 从响应中获取刷新令牌并将其保存到您的 .env 文件中
DROPBOX_REFRESH_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'); }); });
或使用中间件路由,如果用户没有 graph 令牌,则自动重定向以进行身份验证
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是可选的,可以传递额外的头部信息。
$useToken设置为true时,可选的,将使用授权头部,默认为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 racenationcc\Dropbox\Models\DropboxToken;
文件
此包提供了一种简洁的方式来处理文件。
要处理文件,首先调用->files(),然后调用一个方法。
导入命名空间
use racenationcc\Dropbox\Facades\Dropbox;
检查文件是否存在
返回true或false
Dropbox::files()->doesFileExist($path = '')
列出内容
列出给定路径的文件和文件夹
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)
上传流
将数据流作为文件上传。注意此方法仅支持最多150MB的上传。
Dropbox::files()->uploadStream($path, $fileName, $fileContents)
下载文件
通过传递包含文件的文件夹路径从Dropbox下载文件。
Dropbox::files()->download($path)
移动文件夹/文件
移动接受4个参数
$fromPath - 提供现有文件夹/文件的路径 $toPath - 提供现有文件夹/文件的新路径,必须以/开头 $autoRename - 如果存在冲突,让Dropbox服务器尝试自动重命名文件以避免冲突。此字段的默认值为false。$allowOwnershipTransfer - 允许所有者移动,即使这会导致要移动的内容的所有权转移。这不适用于复制。此字段的默认值为false。
Dropbox::files()->move($fromPath, $toPath, $autoRename = false, $allowOwnershipTransfer = false);
谢谢
感谢原作者dcblogdev/laravel-dropbox,这是从该仓库分叉的dave@dcblog.dev
贡献
欢迎贡献,并将得到充分认可。
通过在Github上的Pull Requests接受贡献https://github.com/racenationcc/laravel-dropbox