Python使用百度API上传文件到百度网盘代码分享
关于如何获取 access_token 这个可以自己查百度开放的oauth 2.0 的 api。这里不做介绍。
第三方 python 库
# coding:utf-8
import urllib
import urllib2
__author__ = 'administrator'
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
register_openers()
def upload(filename):
"""
通过百度开发者 api 上传文件到百度云
"""
datagen, headers = multipart_encode({"file": open("e:\\phptest\\test1\\%s"%filename, "rb")})
baseurl = "https://pcs.baidu.com/rest/2.0/pcs/file?"
args = {
"method": "upload",
"access_token": "0.a2834e35964a7b0704242wef160507c1.2592000.1386326697.1060338330-1668780",
"path": "/apps/resourcesharing/%s"%filename
}
encodeargs = urllib.urlencode(args)
url = baseurl + encodeargs
print(url)
request = urllib2.request(url, datagen, headers)
result = urllib2.urlopen(request).read()
print(result)
upload("host.txt")