youngfan007 发表于 2015-4-23 09:16:49

python调用weibo SDK发微博

  准备利用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仓库地址
  https://github.com/zhu327/weibo2rss
页: [1]
查看完整版本: python调用weibo SDK发微博