curl 使用pCloud API下载文件

atmip9wb  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(278)

我正在尝试与pCloudAPI交朋友,在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."
}

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

myzjeezk

myzjeezk1#

概述下载文件的命令x1c 0d1x
我可以通过带有getfolderpublink链接的浏览器下载文件。
curl可以下载文件。但是pCloud网站上没有文档。我是通过浏览器调试窗口(F12)找到的。
我意识到download API也不是真正的下载,它只是获取文件的文件 meta数据。

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代码很重要,并且

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"'

React性

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

3在终端为环境变量分配令牌名

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

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

我将下载一个文件Getting started with pCloud.pdf,我需要从JSON响应中获取fileid。“fileid”是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获取文件信息(包括文件大小)

第一个

  • 在JSON响应中获取link信息

第一个

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

8获取文件路径和主机URL

Host name数组将根据文件的属性(官方默认文件或个人文件)而有所不同
第一个

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"


x1c4d 1x指令集
我可以通过浏览器2下载。

相关问题