Dropbox API - Error: expected list got dict
Dropbox API - Error: expected list got dict
我目前正在构建一个例程,需要从一个特定的 Dropbox 文件夹下载文件,将它们发送到另一台服务器,然后将它们移动到 Dropbox 上的另一个文件夹。
我正在使用 Dropbox 的 /files/move_batch
API 端点来执行此操作。
这是发送到 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
值应该是 list
或 dict
,每个要移动的文件一个,每个文件都包含 from_path
和 to_path
.不过,您的代码提供的 entries
值是单个 dict
。 (在 PHP 中,您可以使用 array
关键字同时创建 list
和 dict
。)
将其分解后更容易查看和使用。这是一个可以做到这一点的工作示例。
<?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),
我目前正在构建一个例程,需要从一个特定的 Dropbox 文件夹下载文件,将它们发送到另一台服务器,然后将它们移动到 Dropbox 上的另一个文件夹。
我正在使用 Dropbox 的 /files/move_batch
API 端点来执行此操作。
这是发送到 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
值应该是 list
或 dict
,每个要移动的文件一个,每个文件都包含 from_path
和 to_path
.不过,您的代码提供的 entries
值是单个 dict
。 (在 PHP 中,您可以使用 array
关键字同时创建 list
和 dict
。)
将其分解后更容易查看和使用。这是一个可以做到这一点的工作示例。
<?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),