You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel调用Dropbox的createSharedLinkWithSettings报400 Bad Request错误

问题:Dropbox createSharedLinkWithSettings接口返回400 Bad Request错误

问题背景

我想把本地图片存到Dropbox云存储,获取永久URL存入数据库,但发现默认生成的URL只有4小时有效期。尝试用createSharedLinkWithSettings接口获取永久链接时,一直触发400 Bad Request错误。

错误信息

Spatie\Dropbox\Exceptions\BadRequest {#453
  #message: ""
  #code: 0
  #file: "C:\\xampp\\htdocs\\vendor\\spatie\\dropbox-api\\src\\Client.php"
  #line: 658
  +dropboxCode: null
  +response: GuzzleHttp\Psr7\Response {#429
    -reasonPhrase: "Bad Request"
    -statusCode: 400
    -headers: array:11 [
      "Content-Type" => array:1 [
        0 => "text/plain; charset=utf-8"
      ]
      "Cache-Control" => array:1 [
        0 => "no-cache"
      ]
      "Content-Security-Policy" => array:1 [
        0 => "sandbox allow-forms allow-scripts"
      ]
      "X-Content-Type-Options" => array:1 [
        0 => "nosniff"
      ]
      "X-Dropbox-Request-Url" => array:1 [
        0 => "/2/sharing/create_shared_link_with_settings"
      ]
      "Date" => array:1 [
        0 => "Sun, 05 May 2024 13:07:17 GMT"
      ]
      "Server" => array:1 [
        0 => "envoy"
      ]
      "Content-Length" => array:1 [
        0 => "284"
      ]
      "Vary" => array:1 [
        0 => "Accept-Encoding"
      ]
      "X-Dropbox-Response-Origin" => array:1 [
        0 => "far_remote"
      ]
      "X-Dropbox-Request-Id" => array:1 [
        0 => "c04575fab67542119e65e2702ed72711"
      ]
    ]
    -headerNames: array:11 [
      "content-type" => "Content-Type"
      "cache-control" => "Cache-Control"
      "content-security-policy" => "Content-Security-Policy"
      "x-content-type-options" => "X-Content-Type-Options"
      "x-dropbox-request-url" => "X-Dropbox-Request-Url"
      "date" => "Date"
      "server" => "Server"
      "content-length" => "Content-Length"
      "vary" => "Vary"
      "x-dropbox-response-origin" => "X-Dropbox-Response-Origin"
      "x-dropbox-request-id" => "X-Dropbox-Request-Id"
    ]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#428
      -stream: stream resource @597
        wrapper_type: "PHP"
        stream_type: "TEMP"
        mode: "w+b"
        unread_bytes: 0
        seekable: true
        uri: "php://temp"
        options: []
      }
      -size: 284
      -seekable: true
      -readable: true
      -writable: true
      -uri: "php://temp"
      -customMetadata: []
    }
  }
  trace: {
    C:\\xampp\\htdocs\\vendor\\spatie\\dropbox-api\\src\\Client.php:658 {
      Spatie\Dropbox\Client->determineException(ClientException $exception): Exception …
      › if (in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
      ›     return new BadRequest($exception->getResponse());
      › }
    }
}

POST请求数据

_token  "lIJmrP9fuVyIukzhAa4zQZMh4pjOb9jQoSEqI60m"
product_name    ""
product_desc    ""
product_image   "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAJYCAMAAACJuGjuAAADAFBMVEUAAADj4t6xR1u5uLnDk468doC0Y2/azM7CvrfSzMfn4NDJyMfQy8Hq5dfk3MrPjZPFxMXCwsPBv8Dk5OdwnFbMbXeet2jOY3P1793UlZ/IS2TFSmLISmLDRV3k3czj28ro4tDn383l38/r5dLLTGXi2cbo5NTm4tLs59TDSF/q48/m3cnz0Mfp4czt6dbHR1/l28fq59fh18LOxrjOSWK/RFvt6trLw7S4Qllnq0TJV2nQybzx69rUzL3Wz8Dd1sfLYG/Iv6/esabet63QT2jaqZ/NanbPdHzfv7PYoZrSh4rZ0sSuPlRnChXQfoPYrKXUkJDFj4Tjzr/hx7rxwrd6qkNruUbLmo+cMETVmJekN03wyr5uo0O+uayPLEHOk4fTn5Pc077Uybjm1MWKsEJuDR69iX+3OlHQpZysLkeLslmAL0O3s…NCkqBAbzM5wpjlXm65NT7fal/b45M8iCV76093sXPydDMl4Mj09zQLYiZLKvr+hDextaANgsVVr8OCNUL5hUZvc7unJ95cBW+VVwALbctS09Br5N/qZ8hPnRa/kqBH5pvFtuouIdWKfE8atCgqMKmI7653nPJPcgpUl+t0/hQpWi+9MzVCmsIv5UrtGtC990sA+opGjjEud8SC/i9n6BrexVfN70iJqvrtGaNXCS8gq8iXk9fI3d3k1QTscf3b//Qu4W0X/FpW8Jt++O//zn62X1q3WdR/2/7iLiAVUzHxxuZyJBFuZiNBNx/EeFV2cjI5qU3Ae/AE1EmsNxFpe1sVz5Dcgv82OnVTSph9z6KYQa7pBWNwYOud/glbt72E0uzHErT/7zJ3d4m645Xbgnp9+Qn/xf4va7v/xt7j7ttu7hVe/t0vHNAAAMAjAMv+md2CCkFZDk+uoMLUKAAAAAAAAAAAAAAAAtj1FK+Qc7cGDsgAAAABJRU5ErkJggg=="
category_id ""
product_condition   "0"
product_content "Chưa+có+thông+tin+gì+cả."
publish "1"
product_status  "1"

相关代码

public function uploadImage($base64Image)
{
    if (!isset($base64Image)) return null;
    $imageData = base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', $base64Image));
    $filename = 'image_' . uniqid() . '.png';
    $result = Storage::disk('dropbox')->put($filename, $imageData);

    if ($result) {
        $adapter = Storage::disk('dropbox')->getAdapter();
        $client = $adapter->getClient();
        $url = '/' . $filename;

        $settings = [
                'requested_visibility' => 'public',
        ];
        $link = $client->createSharedLinkWithSettings('/' . preg_replace('#^data:image/\\w+;base64,#i', '', $base64Image), $settings);
        dd($link);

    } else {
        return $this->failure([], 'Somthing go wrong ways');
    }
}

解决方案

问题核心是调用createSharedLinkWithSettings时传入的参数错误——你传了处理后的base64字符串,而不是Dropbox上的文件路径。正确做法是用刚上传的$filename对应的路径:

修改接口调用部分:

// 错误写法:传入了处理后的base64内容
// $link = $client->createSharedLinkWithSettings('/' . preg_replace('#^data:image/\\w+;base64,#i', '', $base64Image), $settings);

// 正确写法:使用上传的文件路径
$link = $client->createSharedLinkWithSettings('/' . $filename, $settings);

额外注意事项:

  • 确认Dropbox应用已开启sharing.write权限,否则无法创建共享链接
  • 如果文件已存在共享链接,可能返回409冲突错误,可添加force: true参数强制覆盖:
$settings = [
    'requested_visibility' => 'public',
    'force' => true
];

内容的提问来源于stack exchange,提问作者Hau Chung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:02:01