1.、註冊一個新浪應用,得到appkey和secret,以及token,將這些資訊寫入設定檔sina_weibo_config.ini,內容如下,僅舉例:
[userinfo]CONSUMER_KEY=8888888888CONSUMER_SECRET=777777f3feab026050df37d711200000TOKEN=2a21b19910af7a4b1962ad6ef9999999TOKEN_SECRET=47e2fdb0b0ac983241b0caaf45555555
2.、調用新浪微博的Open Api, 編碼:
#!/usr/bin/env python# -*- coding: utf-8 -*-from weibopy.auth import OAuthHandlerfrom weibopy.api import APIimport ConfigParserdef 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、微博內容: