Python uses the Sina Weibo API to send a microblog example _python

Source: Internet
Author: User
Tags auth

1, register a SINA application, get Appkey and secret, as well as token, write this information to the configuration file Sina_weibo_config.ini, the contents are as follows, just for example:

Copy Code code as follows:

[UserInfo]
consumer_key=8888888888
consumer_secret=777777f3feab026050df37d711200000
token=2a21b19910af7a4b1962ad6ef9999999
token_secret=47e2fdb0b0ac983241b0caaf45555555


2, call Sina Weibo's open Api, code:
Copy Code code as follows:

#!/usr/bin/env python
#-*-Coding:utf-8-*-

From Weibopy.auth import Oauthhandler
From WEIBOPY.API Import API
Import Configparser

Def Press_sina_weibo ():
    "
    calling Sina Weibo open API implementation to write blog posts through the command line, features need to be improved
    author:socrates
    date:2012-02-06
    Sina Weibo: @ Sheep without ears
    '
    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 edition)
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, the Operation effect:

Command Line Input:
4, micro-Bo sent successful results:

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.