【问题标题】:Download files with the pCloud API使用 pCloud API 下载文件
【发布时间】:2023-01-02 11:13:32
【问题描述】:

我正在尝试与 pCloud API 交朋友,在 Bash 中使用 curl,

一旦我创建了一个 pCloud 应用程序并获得了它的 $clientid$clientsecret,我就可以获得一个临时访问令牌来接受请求:

echo "https://my.pcloud.com/oauth2/authorize?client_id=$clientid&response_type=code"

并给出 $temptok 令牌,我通过以下方式获得永久不记名令牌:

permtok=$(curl "https://api.pcloud.com/oauth2_token?client_id=$clientid&client_secret=$clientsecret&code=$temptok" | jq -r '.access_token')

此时,我可以使用他们的 API 方法,发表于here
例如,userinfolistfolder 方法给出:

curl "https://api.pcloud.com/userinfo?access_token=$permtok"
curl "https://api.pcloud.com/listfolder?access_token=$permtok&path=/"   

但是,我无法下载文件。 根据我的理解,我需要使用file_openfile_read的组合,后者需要文件大小。 当我打开一个文件时,我得到类似于以下内容的输出:

curl "https://api.pcloud.com/file_open?access_token=$permtok&path=/foo.txt&flags=0x0040"                      
{
    "result": 0,
    "fd": 1,
    "fileid": 1234567890
}

使用 file_size 方法的文件描述符时:

curl "https://api.pcloud.com/file_size?access_token=$permtok&fd=1"

我收到错误:

{
    "result": 1007,
    "error": "Invalid or closed file descriptor."
}

下载文件的正确方法是什么?

【问题讨论】:

  • 我更新了我的答案,第一次无法通过 curl 找到下载方法,但第二次找到了。我希望能让你开心。

标签: curl cloud-storage pcloud


【解决方案1】:

概述 下载文件的顺序

我可以使用getfolderpublink 链接通过浏览器下载文件。

curl可以下载文件。但它没有记录在 pCloud 网站上。我通过浏览器调试窗口(F12)找到了它。

我意识到 download API 也不是真正的下载。它只是获取文件的文件元数据。

https://api.pcloud.com/getfilelink?fileid={my-file-id}&auth={my-auth}'

通过 Curl 下载文件

curl -o {download-file-name} -L -X GET 'https://p-def7.pcloud.com/{full path of my file}' 
-H "Content-Type: application/json; charset=utf-8" 
-H "Authorization: Bearer $token"

演示

1 获取授权 ID

https://my.pcloud.com/oauth2/authorize?client_id={my_client_id}&response_type=code

2 获取访问令牌和授权码

https://u.pcloud.com/oauth2/authorize?client_id=9xxxxxx7&response_type=code&auth={auth_id}

Auth代码很重要Access Token

auth=wt9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxgX

使用代码获取访问令牌。

curl -L -X POST 'https://api.pcloud.com/oauth2_token' 
-H 'Content-Type: application/json; charset=utf-8' 
--form 'client_id="9xxxxxxx7"' 
--form 'client_secret="4xxxxxxxxxxxxxxxxX"' 
--form 'code="lKxxxxxxxxxxxxxxxxxxX"'

回复

{
    "result": 0,
    "userid": 18905223,
    "locationid": 1,
    "token_type": "bearer",
    "access_token": "lKxxxxxxxxxxxxxx-My-Token-xxxxxxxxxxxxxxxxxG7"
}

3 在终端分配带有令牌名称的环境变量

$ token="lKxxxxxxxxxxxxxx-My-Token-xxxxxxxxxxxxxxxxxG7"

4 通过getlist-folder API获取文件信息

我将下载其中一个文件Getting started with pCloud.pdf 我需要从 JSON 响应中获取 fileid。 “文件 ID”是 43338896472

curl -L -X GET 'https://api.pcloud.com/listfolder?path=/' 
-H "Content-Type: application/json; charset=utf-8" 
-H "Authorization: Bearer $token" | jq
{
  "result": 0,
  "metadata": {
    "path": "/",
    "name": "/",
    "created": "Sat, 17 Sep 2022 23:58:07 +0000",
    "ismine": true,
    "thumb": false,
    "modified": "Sat, 17 Sep 2022 23:58:07 +0000",
    "id": "d0",
    "isshared": false,
    "icon": "folder",
    "isfolder": true,
    "folderid": 0,
    "contents": [
.... other three default directories
      {
        "name": "Getting started with pCloud.pdf",
        "created": "Sat, 17 Sep 2022 23:58:07 +0000",
        "videocodec": "",
        "thumb": false,
        "modified": "Sat, 17 Sep 2022 23:58:07 +0000",
        "size": 16371465,
        "audiobitrate": 0,
        "fps": "0.00",
        "comments": 0,
        "isfolder": false,
        "height": 0,
        "rotate": 0,
        "fileid": 43338896472,
        "videobitrate": 0,
        "width": 0,
        "hash": 3096725505949383000,
        "duration": "0.00",
        "path": "/Getting started with pCloud.pdf",
        "category": 4,
        "audiosamplerate": 0,
        "id": "f43338896472",
        "isshared": false,
        "ismine": true,
        "audiocodec": "mp3",
        "parentfolderid": 0,
        "contenttype": "application/pdf",
        "icon": "document"
      }
    ]

5 通过stat API获取文件信息(包括文件大小)

curl -L -X GET 'https://api.pcloud.com/stat?fileid=43338896472' 
-H "Content-Type: application/json; charset=utf-8" 
-H "Authorization: Bearer $token" | jq
{
  "result": 0,
  "metadata": {
    "name": "Getting started with pCloud.pdf",
    "created": "Sat, 17 Sep 2022 23:58:07 +0000",
    "videocodec": "",
    "thumb": false,
    "modified": "Sat, 17 Sep 2022 23:58:07 +0000",
    "size": 16371465,
    "audiobitrate": 0,
    "fps": "0.00",
    "comments": 0,
    "isfolder": false,
    "height": 0,
    "rotate": 0,
    "fileid": 43338896472,
    "videobitrate": 0,
    "width": 0,
    "hash": 3096725505949383000,
    "duration": "0.00",
    "category": 4,
    "audiosamplerate": 0,
    "id": "f43338896472",
    "isshared": false,
    "ismine": true,
    "audiocodec": "mp3",
    "parentfolderid": 0,
    "contenttype": "application/pdf",
    "icon": "document"
  }
}

6 获取getfilepublink API

  • 获取JSON响应中的link信息
"link": "https://u.pcloud.link/publink/show?code=XZ9xxxxxxxxxxsss6Sk"
curl -L -X GET 'https://api.pcloud.com/getfilepublink?fileid=43338896472' 
-H "Content-Type: application/json; charset=utf-8" 
-H "Authorization: Bearer $token" | jq
{
  "code": "XZ9bBhVZ0lSVBSVb4jJKDXJAJBBJ0FIOs6Sk",
  "created": "Sun, 18 Sep 2022 01:15:38 +0000",
  "downloadenabled": true,
  "type": 1,
  "modified": "Sun, 18 Sep 2022 01:15:38 +0000",
  "downloads": 1,
  "link": "https://u.pcloud.link/publink/show?code=XZ9xxxxxxxxxxsss6Sk", <- I modified the code
  "result": 0,
  "linkid": 60017201,
  "haspassword": false,
  "traffic": 16371465,
  "views": 20,
  "metadata": {
    "name": "Getting started with pCloud.pdf",
    "created": "Sat, 17 Sep 2022 23:58:07 +0000",
    "videocodec": "",
    "thumb": false,
    "modified": "Sat, 17 Sep 2022 23:58:07 +0000",
    "size": 16371465,
    "audiobitrate": 0,
    "fps": "0.00",
    "comments": 0,
    "isfolder": false,
    "height": 0,
    "rotate": 0,
    "fileid": 43338896472,
    "videobitrate": 0,
    "width": 0,
    "hash": 3096725505949383000,
    "duration": "0.00",
    "category": 4,
    "audiosamplerate": 0,
    "id": "f43338896472",
    "isshared": false,
    "ismine": true,
    "audiocodec": "mp3",
    "parentfolderid": 0,
    "contenttype": "application/pdf",
    "icon": "document"
  }
}

7 获取download metadata API - 与第 6 步部分相同的结果

8 获取文件路径和主机URL

Host name array 会根据文件的属性(官方默认文件或个人文件)而有所不同

curl -L -X GET 'https://api.pcloud.com/getfilelink?fileid=43338896472&auth=wt9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxgX' 
-H "Content-Type: application/json; charset=utf-8" 
-H "Authorization: Bearer $token"
{
    "result": 0,
    "dwltag": "GYYEgtilNwytpYulbsh1UB",
    "hash": 3096725505949383041,
    "size": 16371465,
    "expires": "Sun, 18 Sep 2022 10:53:50 +0000",
    "path": "/cfZRj4OT2Zwk45bAZlKxxxxxxxxxxxxxxxxxxZbRZlJZ0JZKXZmpZSHZY7ZsFZzpZS5ZLa6pViVfwjfcge2gksnF08W9Qwi7/Getting%20started%20with%20pCloud.pdf",
    "hosts": [
        "p-def7.pcloud.com",
        "c432.pcloud.com"
    ]
}

9 最后我可以使用步骤 8 主机名和路径下载

full URL = Host[0] name + path(删除前两个字符/)

curl -o guide.pdf -L -X GET 'https://p-def7.pcloud.com/cfZRj4OT2Zwk45bAZlKxxxxxxxxxxxxxxxxxxZbRZlJZ0JZKXZmpZSHZY7ZsFZzpZS5ZLa6pViVfwjfcge2gksnF08W9Qwi7/Getting%20started%20with%20pCloud.pdf' 
-H "Content-Type: application/json; charset=utf-8" 
-H "Authorization: Bearer $token"

我可以用浏览器二下载。

【讨论】:

  • 你建议下载私人文件必须公开分享!我怀疑 API 是以这种方式设计的。如果有的话,一旦我的文件在公共云上,还需要用他们的 API 进行身份验证吗?
  • 此外,您的授权程序与 docs 中报告的不同。您使用不同的端点u.pcloud.com/*,而不是api.pcloud.com/*,他们说您应该使用重定向或调用 oauth2_token 来获取不记名令牌。
  • 我添加概述命令以下载文件。 u.pcloud.com 只需获取授权码端点。我正在使用 docs.pcloud.com、u.pcloud.com、api.pcloud.com 和 c329.pcloud.com,但都是 pcloud.com 子域。
  • 获取 OAuth 令牌不需要公共链接可下载文件 (u.pcloud.link/publink/show?code=xxx)。我的步骤是通过编程使用带有访问令牌的 pCloud API 下载文件。
  • https://api.pcloud.com/getfilelink?fileid={my-file-id}&amp;auth={my-auth}' 模式对我有用。它返回一个带有文件路径的元数据负载:/anAmazinglyLongStringOfChartacters/fileName.txt 和一个 hosts 数组,其中包含您应该从中下载文件的服务器。诚然,我第一次尝试时错过了hosts——但这个答案完美地展示了它们在第 8 步中的用法:"hosts": ["p-def7.pcloud.com","c432.pcloud.com"](要清楚,我的主机当然是不同的)。请注意,此身份验证不使用 OAuth;这需要初始授权码的用户名和密码
【解决方案2】:

@Bench Vue 提出的解决方案为要下载的文件创建一个公共链接。所以它不适合私人文件。此外,一旦您拥有公共下载链接,利用 API 以及相关的身份验证步骤可能会过度杀伤力。

这是私人文件的解决方案,不应公开共享。

按照here的描述获取你的永久承载oauth2,这是永久的。

# Input pars
permtok="....."
source="/foo bar.pdf"
dest="foo bar.pdf"
endpoint="https://api.pcloud.com"
 
# Encode source path
encsource=$(printf %s "$source" |jq -sRr @uri)
 
# Get file size 
size=$(curl -H "Connection: keep-alive" 
       "$endpoint/file_open?access_token=$permtok&path=$encsource&flags=64" 
       "$endpoint/file_size?access_token=$permtok&fd=1" 
       | jq -s '.[1] | .size')
 
# Download
curl -H "Connection: keep-alive" 
       "$endpoint/file_open?access_token=$permtok&path=$encsource&flags=64" 
       "$endpoint/file_read?access_token=$permtok&fd=1&count=$size" > "$dest"
       

使用listfolder 方法找到正确的源路径,它可能与您的应用程序文件夹相关。

curl "$endpoint/listfolder?access_token=$permtok&path=/"   

$endpoint 可以根据您所在的地区进行更改。

API 服务器是 HTTP 1.1,因此“连接:保持活动”是必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2015-02-06
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多