Python: 发送新浪微博(使用oauth2)

最后更新于:2022-04-01 19:41:48

1、申请个应用,得到App Key和App Secret 2、设置授权回调页地址,如下,如果不设置,打开回调页时会报错“error:redirect_uri_mismatch”,这一点一定要注意。 ![QQ截图20121204003722](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-08_57579361db142.jpg) 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,然后输入后就可发送了。 ![QQ截图20121204004351](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-08_57579362038e3.jpg) ![QQ截图20121204004234](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-08_575793621ac0b.jpg) ![QQ截图20121204004455](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-08_5757936230521.jpg) ![QQ截图20121204004351](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-08_57579362038e3.jpg)
';