新浪微博api sdk
weibop的Python项目详细描述
微博
weibopy是一个weibo python3 sdk,支持python3的请求包。
安装
普通支持python3版本。
pip3 install weibopy
准备
- 首先,需要注册微博开发者账号,并建立一个基于应用的
[微博平台](http://open.weibo.com/index.php)。 - 现在我们可以在微博应用程序信息页面中获得
App Key
和App Secret
。 - 在微博应用设置中设置回调URL。
- 令牌授权。
fromweibopyimportWeiboOauth2client_id="xxx",client_secret="xxx"redirect_url="http://"client=WeiboOauth2(client_id,client_secret,redirect_url)authorize_url=client.authorize_url# 'https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A%2F%2F127.0.0.1%2Fcallback&client_id=123456'
- 在浏览器中打开授权url,并发送获取访问令牌的代码。
client.auth_access(auth_code){"access_token":"ACCESS_TOKEN","expires_in":1234,"remind_in":"798114","uid":"12341234"}
- 如果令牌已过期,则需要使用
refresh_token
获取访问令牌。
注意:此刷新仅提供官方移动应用程序。
用法
如果我们得到了access_token
,现在我们可以调用api函数。
所有模块作为weibo api客户端到处理程序数据的一部分分开。
WeiboOauth2 # weibo Oauth2 authentication
WeiboClient # weibo Client API
WeiboAPIError # weibo api error
WeiboOauth2Error # weibo oath2 error
如果我们想在最近几小时内得到公众的微博。
fromweibopyimportWeiboClientaccess_token=""client=WeiboClient(access_token)result=client.get(suffix="statuses/public_timeline.json"){"statuses":[{"created_at":"Tue May 31 17:46:55 +0800 2011","id":11488058246,"text":"求关注。","source":"<a href='http://weibo.com' rel='nofollow'>新浪微博</a>","favorited":False,"truncated":False,"in_reply_to_status_id":"","in_reply_to_user_id":"","in_reply_to_screen_name":"","geo":None,"mid":"5612814510546515491","reposts_count":8,"comments_count":9,"annotations":[],"user":{"id":1404376560,"screen_name":"zaku","name":"zaku","province":"11","city":"5","location":"北京 朝阳区","description":"人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。","url":"http://blog.sina.com.cn/zaku","profile_image_url":"http://tp1.sinaimg.cn/1404376560/50/0/1","domain":"zaku","gender":"m","followers_count":1204,"friends_count":447,"statuses_count":2908,"favourites_count":0,"created_at":"Fri Aug 28 00:00:00 +0800 2009","following":False,"allow_all_act_msg":False,"remark":"","geo_enabled":False,"verified":False,"allow_all_comment":False,"avatar_large":"http://tp1.sinaimg.cn/1404376560/180/0/1","verified_reason":"","follow_me":False,"online_status":0,"bi_followers_count":215}},],"previous_cursor":0,"next_cursor":11488013766,"total_number":81655}
如何用图片发布微博?
fromweibopyimportWeiboClientaccess_token=""client=WeiboClient(access_token)files={'pic':open('a.png','rb')}client.post("statuses/upload.json",data={"status":"nihao"},files=files)
结果:
{
'biz_feature': 0,
'bmiddle_pic': 'http://wx1.sinaimg.cn/bmiddle/70669a5dly1fescai5ggkj20gf0feq7e.jpg',
'comments_count': 0,
'created_at': 'Wed Apr 19 22:06:20 +0800 2017',
'darwin_tags': [],
'favorited': False,
'geo': None,
'gif_ids': None,
'hasActionTypeCard': 0,
'hot_weibo_tags': [],
'id': 4098369015668443,
'idstr': '4098369015668443',
'in_reply_to_screen_name': '',
'in_reply_to_status_id': '',
'in_reply_to_user_id': '',
'isLongText': False,
'is_show_bulletin': 2,
'mid': '4098369015668443',
'mlevel': 0,
'original_pic': 'http://wx1.sinaimg.cn/large/70669a5dly1fescai5ggkj20gf0feq7e.jpg',
'pic_urls': [{'thumbnail_pic': 'http://wx1.sinaimg.cn/thumbnail/70669a5dly1fescai5ggkj20gf0feq7e.jpg'}],
'positive_recom_flag': 0,
'reposts_count': 0,
'source': '<a href="http://open.weibo.com" rel="nofollow">未通过审核应用</a>',
'source_allowclick': 0,
'source_type': 1,
'text': 'hhha \u200b',
'textLength': 4,
'text_tag_tips': [],
'thumbnail_pic': 'http://wx1.sinaimg.cn/thumbnail/70669a5dly1fescai5ggkj20gf0feq7e.jpg',
'truncated': False,
'user': {'allow_all_act_msg': False,
'allow_all_comment': True,
'avatar_hd': 'http://tva4.sinaimg.cn/crop.320.120.960.960.1024/70669a5djw1eanuztfpewj218g0xcn9c.jpg',
'avatar_large': 'http://tva4.sinaimg.cn/crop.320.120.960.960.180/70669a5djw1eanuztfpewj218g0xcn9c.jpg',
'bi_followers_count': 9,
'block_app': 0,
'block_word': 0,
'city': '2',
'class': 1,
'created_at': 'Tue Dec 07 21:44:06 +0800 2010',
'credit_score': 80,
'description': '',
'domain': 'nooper',
'favourites_count': 3,
'follow_me': False,
'followers_count': 48,
'following': False,
'friends_count': 140,
'gender': 'm',
'geo_enabled': True,
'id': 1885772381,
'idstr': '1885772381',
'insecurity': {'sexual_content': False},
'lang': 'zh-cn',
'location': '北京 西城区',
'mbrank': 0,
'mbtype': 0,
'name': '王小小小小小莫',
'online_status': 0,
'pagefriends_count': 0,
'profile_image_url': 'http://tva4.sinaimg.cn/crop.320.120.960.960.50/70669a5djw1eanuztfpewj218g0xcn9c.jpg',
'profile_url': 'nooper',
'province': '11',
'ptype': 0,
'remark': '',
'screen_name': '王小小小小小莫',
'star': 0,
'statuses_count': 168,
'urank': 13,
'url': '',
'user_ability': 0,
'verified': False,
'verified_reason': '',
'verified_reason_url': '',
'verified_source': '',
'verified_source_url': '',
'verified_trade': '',
'verified_type': -1,
'weihao': ''},
'userType': 590081,
'visible': {'list_id': 0, 'type': 0}}
联系人
如果您对微博api有任何疑问,请联系365504029@qq.com