新浪微博API OAuth 2 Python客户端

fmms 12年前
     sinaweibopy是一个支持OAuth 2.0的新浪微博Python SDK,是新浪微博官方推荐的Python SDK,支持Python 2.x,不依赖其他第三方包,总代码不超过200行!    <h2>使用简介</h2>    <p>注册微博App后,可以获得app key和app secret,然后定义网站回调地址:</p>    <pre class="brush:python; toolbar: true; auto-links: false;">from weibo import APIClient  APP_KEY = '1234567' # app key APP_SECRET = 'abcdefghijklmn' # app secret CALLBACK_URL = 'http://www.example.com/callback' # callback url</pre>在网站放置“使用微博账号登录”的链接,当用户点击链接后,引导用户跳转至如下地址:    <br />    <pre class="brush:python; toolbar: true; auto-links: false;">client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) url = client.get_authorize_url() # TODO: redirect to url</pre>    <p></p>    <p>用户授权后,将跳转至网站回调地址,并附加参数code=abcd1234: </p>    <pre class="brush:python; toolbar: true; auto-links: false;"># 获取URL参数code: code = your.web.framework.request.get('code') client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) r = client.request_access_token(code) access_token = r.access_token # 新浪返回的token,类似abc123xyz456 expires_in = r.expires_in # token过期的UNIX时间:http://zh.wikipedia.org/wiki/UNIX%E6%97%B6%E9%97%B4 # TODO: 在此可保存access token client.set_access_token(access_token, expires_in)</pre>然后,可调用任意API:    <br />    <pre class="brush:python; toolbar: true; auto-links: false;">print client.get.statuses__user_timeline() print client.post.statuses__update(status=u'测试OAuth 2.0发微博') print client.upload.statuses__upload(status=u'测试OAuth 2.0带图片发微博', pic=open('/Users/michael/test.png'))</pre>    <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1325645364437" target="_blank">http://www.open-open.com/lib/view/home/1325645364437</a></p>