Dropbox API -错误:预期列表得到字典


Dropbox API - Error: expected list got dict

我目前正在构建一个程序,需要从一个特定的Dropbox文件夹下载文件,将它们发送到另一个服务器,然后将它们移动到Dropbox的另一个文件夹。

我使用/files/move_batch API端点为Dropbox这样做。

下面是发送到API来移动多个文件的参数(好吧,我现在只尝试移动一个文件,因为它仍然不工作):

$params = array(
            'headers'           => array(
                'method'    => 'POST',
                'content-type' => 'application/json; charset=utf-8',
                ),
            'body' => json_encode(array(
                'entries'           => array(
                    'from_path' => self::$files[0],
                    'to_path'   => '/Applications/Archives/' . substr(self::$files[0], strrpos(self::$files[0], '/') + 1),
                    ),
                'autorename'        => true,
                )),
            );

但是我一直得到相同的错误信息:

Error in call to API function "files/move_batch": request body: entries: expected list, got dict

我不知道API中的列表是什么意思,也不知道它应该如何格式化。

entries值应该是dictlist,每个要移动的文件一个,每个都包含from_pathto_path。您的代码将entries值提供为单个dict。(在PHP中,您可以使用array关键字创建list s和dict s。)

当你把它分解成碎片时,它更容易被看到和使用。下面是一个工作示例。

<?php
    $fileop1 = array(
                    'from_path' => "/test_39995261/a/1.txt",
                    'to_path'   => "/test_39995261/b/1.txt"
                );
    $fileop2 = array(
                    'from_path' => "/test_39995261/a/2.txt",
                    'to_path'   => "/test_39995261/b/2.txt"
                );
    $parameters = array(
            'entries' => array($fileop1, $fileop2),
            'autorename' => true,
    );
    $headers = array('Authorization: Bearer <ACCESS_TOKEN>',
                     'Content-Type: application/json');
    $curlOptions = array(
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($parameters),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_VERBOSE => true
        );
    $ch = curl_init('https://api.dropboxapi.com/2/files/move_batch');
    curl_setopt_array($ch, $curlOptions);
    $response = curl_exec($ch);
    echo $response;
    curl_close($ch);
?>

要使用这个批处理端点只移动一个文件,您可以将该行更改为如下内容:

            'entries' => array($fileop1),