1. Register a Sina application, get the appkey, secret, and token, and write the information to the configuration file sina_weibo_config.ini. The content is as follows, for example only:
[Plain]
[Userinfo]
Consumer_keys = 8888888888
CONSUMER_SECRET = 7777f3feab026050df37d711200000
TOKEN = 2a21b19910af7a4b1962ad6ef9999999
TOKEN_SECRET = 47e2fdb0b0ac983241b0caaf45555555
[Userinfo]
Consumer_keys = 8888888888
CONSUMER_SECRET = 7777f3feab026050df37d711200000
TOKEN = 2a21b19910af7a4b1962ad6ef9999999
TOKEN_SECRET = 47e2fdb0b0ac983241b0caaf45555555
2. Call the Open Api of Sina Weibo, code:
[Python]
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
From weibopy. auth import OAuthHandler
From weibopy. api import API
Import ConfigParser
Def press_sina_weibo ():
'''''
The function of calling Sina Weibo Open Api to write blog posts through command line is to be improved
Author: socrates
Date: 2012-02-06
Sina Weibo: @ yang without ears
'''
Sina_weibo_config = ConfigParser. ConfigParser ()
# Reading configuration files related to appkey
Try:
Sina_weibo_config.readfp (open ('sina _ weibo_config.ini '))
Failed t ConfigParser. Error:
Print 'read sina_weibo_config.ini failed .'
# Obtain required information
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 ")
# Call Sina Weibo OpenApi (python version)
Auth = OAuthHandler (consumer_key, consumer_secret)
Auth. setToken (token, token_sercet)
Api = API (auth)
# Enter the content to be published through the command line
Weibo_content = raw_input ('Please input content :')
Status = api. update_status (status = weibo_content)
Print "Press sina weibo o successful, content is: % s" % status. text
If _ name _ = '_ main __':
Press_sina_weibo ()
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
From weibopy. auth import OAuthHandler
From weibopy. api import API
Import ConfigParser
Def press_sina_weibo ():
'''
The function of calling Sina Weibo Open Api to write blog posts through command line is to be improved
Author: socrates
Date: 2012-02-06
Sina Weibo: @ www.2cto.com without ears
'''
Sina_weibo_config = ConfigParser. ConfigParser ()
# Reading configuration files related to appkey
Try:
Sina_weibo_config.readfp (open ('sina _ weibo_config.ini '))
Failed t ConfigParser. Error:
Print 'read sina_weibo_config.ini failed .'
# Obtain required information
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 ")
# Call Sina Weibo OpenApi (python version)
Auth = OAuthHandler (consumer_key, consumer_secret)
Auth. setToken (token, token_sercet)
Api = API (auth)
# Enter the content to be published through the command line
Weibo_content = raw_input ('Please input content :')
Status = api. update_status (status = weibo_content)
Print "Press sina weibo o successful, content is: % s" % status. text
If _ name _ = '_ main __':
Press_sina_weibo ()
3. effect:
Command Line input:
4. Weibo content:
From Socrates Column