准備利用weibo SDK與Django,寫一個weibo timeline輸出rss的小工具,昨天開始熟悉weibo SDK,先實現一個簡單的腳本搞懂weibo SDK使用方法
參考:
https://github.com/michaelliao/sinaweibopy/wiki/OAuth2-HOWTO
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from weibo import APIClient 5 6 __version__ = '0.1' 7 __author__ = 'zhu327' 8 9 APP_KEY = 'xxx' # 填寫申請的APP_KEY 10 APP_SECRET = 'xxx' # 填寫申請的APP_SECRET 11 CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html' 12 13 ''' posttext.py 調用weibo SDK 命令發微博 ''' 14 15 # 獲取微博授權,手動操作 16 def getclient(): 17 client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) 18 url = client.get_authorize_url() 19 20 print url # 瀏覽器打開該url,取得code='xxx'類似的code輸入 21 22 code = raw_input('Enter code >') 23 24 r = client.request_access_token(code) 25 access_token = r.access_token 26 expires_in = r.expires_in 27 28 client.set_access_token(access_token, expires_in) 29 30 return client 31 32 # 發微博 33 def posttext(client): 34 text = raw_input('Enter text to post >') 35 utext = unicode(text, "UTF-8") 36 client.statuses.update.post(status=utext) 37 38 if __name__ == '__main__': 39 client = getclient() 40 posttext(client)
ps:學習使用git,github倉庫地址