Weibo applications written in python

Source: Internet
Author: User
This article mainly introduces the microblog application compiled based on python. it is a specific application developed for the Weibo Open Platform SDK and has very practical value, for more information, see the examples in this article to describe weibo applications written based on python. The details are as follows:

Before writing your own weibo application, you must apply for the public key and private key of the application on the weibo open platform.
Download the python SDK, open the example directory, and encode it like oauthSetTokenUpdate. py,

The code is as follows:

#-*-Coding: UTF-8 -*-

From weibopy. auth import OAuthHandler
From weibopy. api import API


Consumer_key = 'application key'
Consumer_secret = 'app secret'

Auth = OAuthHandler (consumer_key, consumer_secret)
Auth_url = auth. get_authorization_url ()
Print 'Please authorize: '+ auth_url
Verifier = raw_input ('pin: '). strip ()
Auth. get_access_token (verifier)
Api = API (auth)

Status = api. update_status (status = 'Hello World', lat = '12. 3', long = '45. 6 ') # note that the status must be a UTF-8-encoded string, latitude and longitude can be not written
Print status. id
Print status. text

When you run this program, a URL link is displayed. you can open this link in your browser and grant the access permission. then, you will get a string of PIN codes. After this PIN code is entered, a push message is sent, and the user's Access token key and Access token secret are displayed. The whole process is very simple:

Use your consumer_key and consumer_secret to create an OAuthHandler object auth.
Instruct the user to access auth. get_authorization_url () and authorize the application.
Get the user's PIN code and use auth. get_access_token () to get the user's Access token key and Access token secret.
Use auth to create an API object api.
The method used to call the api. for example, update_status () is the push method. For more information, see the API documentation.

Of course, it is too silly to ask the user to enter the PIN code every time, so auth is used. after get_access_token () obtains the Access token key and Access token secret, you can directly use them to create API objects:

The code is as follows:

#-*-Coding: UTF-8 -*-

From weibopy. auth import OAuthHandler
From weibopy. api import API


Consumer_key = 'application key'
Consumer_secret = 'app secret'
Token = 'User's Access token key'
TokenSecret = 'User's Access token secret'

Auth = OAuthHandler (consumer_key, consumer_secret)
Auth. setToken (token, tokenSecret)
Api = API (auth)

Status = api. update_status (status = 'get it done ~ ')

I hope this article will help you with Python programming.

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.