首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用pCloud API下载文件

使用pCloud API下载文件
EN

Stack Overflow用户
提问于 2022-09-17 23:15:15
回答 1查看 255关注 0票数 1

我试图与pCloud API交友,在Bash中使用curl,

创建pCloud应用程序并获取其$clientid$clientsecret之后,我可以在以下站点获得接受请求的临时访问令牌:

代码语言:javascript
运行
复制
echo "https://my.pcloud.com/oauth2/authorize?client_id=$clientid&response_type=code"

在给定$temptok令牌时,我将使用以下内容获得永久承载令牌:

代码语言:javascript
运行
复制
permtok=$(curl "https://api.pcloud.com/oauth2_token?client_id=$clientid&client_secret=$clientsecret&code=$temptok" | jq -r '.access_token')

此时,我可以使用他们的API方法,发布这里

例如,用户信息列表文件夹方法,它提供:

代码语言:javascript
运行
复制
curl "https://api.pcloud.com/userinfo?access_token=$permtok"
curl "https://api.pcloud.com/listfolder?access_token=$permtok&path=/"   

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

代码语言:javascript
运行
复制
curl "https://api.pcloud.com/file_open?access_token=$permtok&path=/foo.txt&flags=0x0040"                      
{
    "result": 0,
    "fd": 1,
    "fileid": 1234567890
}

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

代码语言:javascript
运行
复制
curl "https://api.pcloud.com/file_size?access_token=$permtok&fd=1"

我知道错误:

代码语言:javascript
运行
复制
{
    "result": 1007,
    "error": "Invalid or closed file descriptor."
}

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

EN

回答 1

Stack Overflow用户

发布于 2022-09-18 03:17:16

下载文件的概述命令

我可以通过浏览器下载带有getfolderpublink链接的文件。

curl可以下载文件。但pCloud网站上并没有记载这一点。我是通过浏览器调试窗口(F12)找到的。

我知道下载API也不是真正的下载。它只获取文件的元数据。

代码语言:javascript
运行
复制
https://api.pcloud.com/getfilelink?fileid={my-file-id}&auth={my-auth}'

Curl下载文件

代码语言:javascript
运行
复制
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得到8月ID

代码语言:javascript
运行
复制
https://my.pcloud.com/oauth2/authorize?client_id={my_client_id}&response_type=code

2获取访问令牌& Auth代码

代码语言:javascript
运行
复制
https://u.pcloud.com/oauth2/authorize?client_id=9xxxxxx7&response_type=code&auth={auth_id}

Auth代码很重要,Access Token也很重要

代码语言:javascript
运行
复制
auth=wt9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxgX

使用代码获取访问令牌。

代码语言:javascript
运行
复制
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"'

响应

代码语言:javascript
运行
复制
{
    "result": 0,
    "userid": 18905223,
    "locationid": 1,
    "token_type": "bearer",
    "access_token": "lKxxxxxxxxxxxxxx-My-Token-xxxxxxxxxxxxxxxxxG7"
}

3在终端指定带有令牌名称的环境变量。

代码语言:javascript
运行
复制
$ token="lKxxxxxxxxxxxxxx-My-Token-xxxxxxxxxxxxxxxxxG7"

4通过获取列表-文件夹API获取文件信息

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

代码语言:javascript
运行
复制
curl -L -X GET 'https://api.pcloud.com/listfolder?path=/' \
-H "Content-Type: application/json; charset=utf-8" \
-H "Authorization: Bearer $token" | jq
代码语言:javascript
运行
复制
{
  "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获取文件信息(包括文件大小)

代码语言:javascript
运行
复制
curl -L -X GET 'https://api.pcloud.com/stat?fileid=43338896472' \
-H "Content-Type: application/json; charset=utf-8" \
-H "Authorization: Bearer $token" | jq
代码语言:javascript
运行
复制
{
  "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信息
代码语言:javascript
运行
复制
"link": "https://u.pcloud.link/publink/show?code=XZ9xxxxxxxxxxsss6Sk"
代码语言:javascript
运行
复制
curl -L -X GET 'https://api.pcloud.com/getfilepublink?fileid=43338896472' \
-H "Content-Type: application/json; charset=utf-8" \
-H "Authorization: Bearer $token" | jq
代码语言:javascript
运行
复制
{
  "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得到下载元数据API -步骤6部分的相同结果

8获取文件路径和主机URL

Host name数组将取决于文件的属性(默认文件或个人文件)。

代码语言:javascript
运行
复制
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"
代码语言:javascript
运行
复制
{
    "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"
    ]
}

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

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

代码语言:javascript
运行
复制
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"

我可以通过浏览器二下载。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73759126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档