Python: 傳送新浪微博(使用oauth2)
阿新 • • 發佈:2019-02-06
1、申請個應用,得到App Key和App Secret
2、設定授權回撥頁地址,如下,如果不設定,打開回調頁時會報錯“error:redirect_uri_mismatch”,這一點一定要注意。
3、程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- from weibo import * def press_sina_weibo(): APP_KEY = '220567XXXX' APP_SECRET = '47970abb02d8f7b4d600d98e9bXXXXXX' CALLBACK_URL = 'https://api.weibo2.com/oauth2/default.html' client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) print client.get_authorize_url() r = client.request_access_token(raw_input("input code:").strip()) client.set_access_token(r.access_token, r.expires_in) print client.post.statuses__update(status=u'測試一下發送微博') if __name__ == '__main__': press_sina_weibo()
4、測試:
執行後,會輸出一個URL,在位址列中開啟,得到token,然後輸入後就可傳送了。