Sina Micro-blog Python SDK notes-Weibo (i) __python

Source: Internet
Author: User

The Python SDK is already installed in the previous section, and you can introduce the Weibo module in the program to write the microblog program. The function of the following program is to use the module to send a new microblog, first open the specific code:

#! /usr/bin/python "" "" "" "" "" "" "" "" "" "" "" "" "" "" Import Weibo "" "" "" "" Authorization required three information, App_key, App_secret allocated for the creation of the application, Call_back in the Application Settings page.
"Note" The Call_back address used in this authorization must be consistent with the Call_back set in the application, otherwise a redirect_uri_mismatch error will occur.
"" "App_key = ' 2024 ... '
App_secret = ' 91a57c ... ' Call_back = ' http://bingbingrobot.sinaapp.com/' def run (): class that #weibo模块的APIClient是进行授权, API operations, first define a class object, and pass in the parameter App_ke Y, App_secret, call_back client = Weibo.
	Apiclient (App_key, App_secret, Call_back) #获取该应用 (App_key is unique) the URL provided to the user for authorization Auth_url = Client.get_authorize_url ()  #打印出用户进行授权的url, the URL is copied to the browser, and the server returns a URL that contains a code field (as shown in Figure 1) print "Auth_url:" + Auth_url #输入该code值 (as shown in Figure 2) code = Raw_input ("Input the Retured Code:") #通过该code获取access_token, R is the return authorization result, the specific parameter reference Official document: # Http://open.weibo.com/wiki/Oauth 2/access_token r = Client.request_access_token (code) #将access_token和expire_in设置到client对象 Client.set_access_token

	(R.access_token, r.expires_in) #以上步骤就是授权的过程, now the client is free to call the interface for microblogging operations, the following code is to use the content of user input to send a new microbloggingWhile True:print "ready! Do your want to send a new Weibo? (y/n) "choice = Raw_input () if choice = = ' Y ' or choice = = ' Y ': content = raw_input (' Input the Your new Weibo T: ' If content: #调用接口发一条新微薄, the status parameter is the microblogging content client.statuses.update.post (Status=co
				ntent) print "Send succesfully!"
			Break Else:print "error!
		Empty content! " if choice = = ' n ' or choice = = ' n ': break if __name__ = = "__main__": Run ()

Figure 1

Figure 2
After successful execution of the program, you will be able to see the new microblog that you just sent in the microblog, as shown in Figure 3.

Figure 3
about the invocation of the authorization interface and API interface

Since the SDK provides classes and functions without a detailed description, the authorization process is simple and fixed, you can view the module's source code file weibo.py, find the system of the Python module installation directory, you can find the file, my system (Ubuntu) weibo.py located in:/ Usr/local/lib/python2.7/dist-packages/.

The API for microblogging operations, although there is no description of the interface, but there are certain rules. Now, Sina Weibo's Open Platform API document (Http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI) finds the API that needs to be invoked, such as the need to send a microblog to find the API documentation for the feature (http:// Open.weibo.com/wiki/2/statuses/update), as shown in Figure 4.


Figure 4

The

sees the URL that the API accesses, replaces "/" in the URL with the "." of the client object, and then sees the HTTP request method of the API, if the post then calls the Post interface, and if it is a get, calls the Get interface. If the API is invoked as follows: Client.statuses.update.post (status=content), the parameters are given in key=value form, and the specific parameters are described in detail in the API documentation. Therefore, it can be seen that the API calls are very convenient. about the need for duplicate authorization similar programs with SDK tweets are available on the web, and much of the same, steps are authorized, and then the API is invoked, and the process of authorization is to copy the URL to the browser. The resulting code input is then fetched Access_token. So here's a question (and a lot of friends on the Internet): Is there a license to run the program every time you send a microblog? Isn't that a lot of trouble? And the official documentation also shows that authorization can last a period of time (the test level is 1 days), but here every time to run the program to be delegated, is not necessary. The answer is: there is no need to authorize each run of the program, and the last successful authorization remains valid as long as it does not expire. Specific how to operate it. The next section continues.
By:  

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.