使用百度網盤API上傳備份文件

http://www.haiyun.me/tag/%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98/


Linux VPS使用百度網盤API上傳備份文件

發佈時間:August 25, 2013 // 分類:備份存儲 // 8 Comments

最近百度網盤將空間升級到了永久1TB,鑑於百度的實力用做數據備份空間不錯,不過百度網盤沒有Linux下的客戶端,上傳管理文件需通過百度開放雲平臺訪問PCS資源的系列接口。
1.首先加入百度開發者:http://developer.baidu.com/dev#/create
2.任意創建一個應用獲取API Key並開通PCS API權限,開放API > API管理 > API列表 > API服務 > PCS API > 開啓
3.通過剛剛新建應用的API Key獲取device code和user_code

1
2
curl -k -L -d "client_id=<api_key>&response_type=device_code&scope=basic,netdisk"\
"https://openapi.baidu.com/oauth/2.0/device/code"

4.在瀏覽器打開https://openapi.baidu.com/device輸入獲取到的user_code並連接,然後通過device code獲取refresh_token和access_token:

1
2
curl -k -L -d "grant_type=device_token&code=<device_code>&client_id=<api_key>&client_secret=<api_secret>" \
"https://openapi.baidu.com/oauth/2.0/token"

5.此時通過access_token就可對網盤文件進行操作了,獲取到access_token的有效期爲30天,過期後通過refresh_token重新獲取access_token

1
2
3
curl -k -L -d "grant_type=refresh_token&refresh_token=<refresh_token>&client_id=<api_key>\
&client_secret=<api_secret>"\
"https://openapi.baidu.com/oauth/2.0/token"

查看網盤配額:

1
2
curl -k -L "https://pcs.baidu.com/rest/2.0/pcs/quota?method=\
info&access_token=<access_token>"

上傳文件:

1
2
curl -k -L -F "[email protected]" "https://c.pcs.baidu.com/rest/2.0/pcs/file?method=upload&\
access_token=<access_token>&path=/apps/pcsupload/haiyun.me.tar.gz"

下載文件:

1
2
curl -k -O "https://d.pcs.baidu.com/rest/2.0/pcs/file?method=download&access_token=<access_token>&\
path=/apps/pcsupload/haiyun.me.tar.gz"

刪除文件:

1
2
curl -k -L "https://pcs.baidu.com/rest/2.0/pcs/file?method=delete&access_token=<access_token>\
&path=/apps/pcsupload/haiyun.me.tar.gz"

複製文件:

1
2
curl -k -L "https://c.pcs.baidu.com/rest/2.0/pcs/file?method=copy&access_token=<access_token>\
&from=/apps/pcsupload/haiyun.me.tar.gz&to=/apps/pcsupload/www.haiyun.me.tar.gz"

列出目錄內文件:

1
2
curl -k -L "https://pcs.baidu.com/rest/2.0/pcs/file?method=list&access_token=<access_token>\
&path=/apps/pcsupload/"

更多操作:http://developer.baidu.com/wiki/index.php?title=docs/pcs/rest/file_data_apis_list


發佈了36 篇原創文章 · 獲贊 12 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章