Python使用新浪微博API发送微博的例子
1、注册一个新浪应用,得到appkey和secret,以及token,将这些信息写入配置文件sina_weibo_config.ini,内容如下,仅举例:
[userinfo]
consumer_key=8888888888
consumer_secret=777777f3feab026050df37d711200000
token=2a21b19910af7a4b1962ad6ef9999999
token_secret=47e2fdb0b0ac983241b0caaf45555555
2、调用新浪微博的open api,编码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from weibopy.auth import oauthhandler
from weibopy.api import api
import configparser
def press_sina_weibo():
'''
调用新浪微博open api实现通过命令行写博文,功能有待完善
author: socrates
date:2012-02-06
新浪微博:@没耳朵的羊
'''
sina_weibo_config = configparser.configparser()
#读取appkey相关配置文件
try:
sina_weibo_config.readfp(open('sina_weibo_config.ini'))
except configparser.error:
print 'read sina_weibo_config.ini failed.'
#获取需要的信息
consumer_key = sina_weibo_config.get("userinfo","consumer_key")
consumer_secret =sina_weibo_config.get("userinfo","consumer_secret")
token = sina_weibo_config.get("userinfo","token")
token_sercet = sina_weibo_config.get("userinfo","token_secret")
#调用新浪微博openapi(python版)
auth = oauthhandler(consumer_key, consumer_secret)
auth.settoken(token, token_sercet)
api = api(auth)
#通过命令行输入要发布的内容
weibo_content = raw_input('please input content:')
status = api.update_status(status=weibo_content)
print "press sina weibo successful, content is: %s" % status.text
if __name__ == '__main__':
press_sina_weibo()
3、 运行效果:
命令行输入:
4、微博发送成功效果:
推荐阅读