Python: Send Sina Weibo via command line

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.