使用Python调用新浪微盘接口,创建自己的云盘应用

我们可以使用新浪微博提供的微盘API接口,开发自己的云盘应用。下面一起来看一下吧。

1.首先到新浪微盘的开发者平台上创建自己的应用,然后可以获得你的APP_KEY和APP_SECRET。


2.新浪微盘采用的是Oauth2.0的认证方式,认证过程大致如下

(1).首先你需要使用GET或POST请求https://auth.sina.com.cn/oauth2/authorize,并带上参数client_id、redirect_uri、response_type,client_id是你的App Key,redirect_uri是你的应用才通过用户授权后的回调地址,其它详细参数说明可查看API文档:http://vdisk.weibo.com/developers/index.php?module=api&action=apidoc#authorize;用浏览器请求地址的效果如下:


(2).在获得认证后,回调地址会收到Code参数,这个code我们之后请求access_token时会要使用


(3).在获得code之后,需要请求https://auth.sina.com.cn/oauth2/access_token,来获得access_token,请求的参数为:

client_id,client_secrey,grant_type,code。client_secret是你的app_secret,client_id是你的app_key,grant_type是请求的类型,这里我们填写‘authorization_code’,然后再加上刚才得到的code参数。

(4).经过第三步,会收到返回的json字符串,然后从json字符串中读取出access_token。


Python代码如下,运行这段代码会弹出浏览器,然后,用户同意授权后,将浏览器地址栏上的code参数值复制下来,输入到终端,程序继续运行,然后会打印出access_token。

auth.py

# coding=utf-8
import urllib,urllib2
import codecs
import webbrowser
import json

appkey = '.你的key...'
appsecret = '..你的app_secret.'
redirect_uri = '.你的回调地址..'
display = 'popup'

class Token():
	"""请用户授权,获取Token"""

	def __init__(self, appkey, appsecret, redirect_uri, display):
		self.appkey = appkey
		self.appsecret = appsecret
		self.redirect_uri = redirect_uri
		self.display = display

	def getToken(self):
		url = ('https://auth.sina.com.cn/oauth2/authorize?client_id=%s&redirect_uri=%s&display=%s')%(self.appkey, self.redirect_uri, self.display)
		webbrowser.open(url)

	def getAccessToken(self):
		self.getToken()
		self.code = raw_input()
		url = 'https://auth.sina.com.cn/oauth2/access_token'
		params = {'client_id':self.appkey, 'client_secret':self.appsecret, 'grant_type':'authorization_code','code':self.code, 'redirect_uri':self.redirect_uri}
		data = urllib.urlencode(params)
		request = urllib2.Request(url, data)
		response = urllib2.urlopen(request)
		result = response.read()
		print result
		m_json = json.loads(result)
		access_token = m_json['access_token']
		print access_token
		return access_token

a = Token(appkey, appsecret, redirect_uri, display)
a.getAccessToken()

3.通过Python代码和用户的access_token与网盘交互

新浪微盘提供了简便易用的Python API可直接使用: https://github.com/CloudSide/VdiskSDK-Python

下面这段代码,是用来实现文件的简单上传下载、查看用户信息等。

其中vdisk是新浪的API文件。

直接运行这段代码,选择一些选项即可完成一些简单的功能。

# coding=utf-8
from auth import Token
from vdisk import Client

appkey = '你的key'
appsecret = '你的secret'
redirect_uri = '你的回调地址'
display = 'popup'

class client:
	def __init__(self, appkey, appsecret, redirect_uri, display):
		m_auth = Token(appkey, appsecret, redirect_uri, display)
		self.access_token = m_auth.getAccessToken()

	def getAccountInfo(self, v_client):
		"""功能:获取用户信息"""
		print v_client.account_info(self.access_token).read()

	def getDelta(self, v_client):
		"""获取用户文件和目录操作变化记录。列表每页固定为 2000 条。"""
		try:
			result = v_client.delta(self.access_token)
			print result.read()
		except:
			print result

	def fileops_create_folder(self, v_client):
		"""创建一个文件夹"""
		try:
			print "请输入文件夹的路径".decode('utf-8')
			path = raw_input()
			result = v_client.fileops_create_folder(self.access_token, path)
			print result.read()
		except Exception,e:
			print Exception,":",e

	def files_put(self, v_client):
		"""使用PUT方法向云盘传递文件"""
		try:
			print "请输入上传的位置:".decode('utf-8')
			cloudPath = raw_input()
			print "请输入需要上传的文件的名称及本地路径".decode('utf-8')
			localPath = raw_input()
			with open(localPath, 'rb') as content:
				result = v_client.files_put(self.access_token, cloudPath, content)
				print result.read()
		except Exception,e:
			print Exception,":",e

	def download_files(self, v_client):
		"""下载文件"""
		try:
			print "请输入云盘上文件存储的位置:".decode('utf-8')
			path = raw_input()
			data = v_client.files(self.access_token, path)
			print "文件下载完毕,请输入文件的保存位置".decode('utf-8')
			localPath = raw_input()
			with open(localPath, 'wb') as f:
				f.write(data.read())
		except Exception,e:
			print Exception,":",e

	def run(self):
		v_client = Client()
		v_client.setRoot('sandbox')
		dic = {"1":self.getAccountInfo, "2":self.getDelta, 
		"4":self.fileops_create_folder, "5":self.files_put, "6":self.download_files}
		while 1:
			print "请输入要做的操作:".decode('utf-8')
			opt = raw_input()
			dic.get(opt)(v_client)

m_client = client(appkey, appsecret, redirect_uri, display)
m_client.run()



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值