我們可以使用新浪微博提供的微盤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()