The Python SDK has been installed in the previous section. You can introduce the Weibo module in the program to write Weibo programs. The function of the following program is to use this module to send a new microblog with the specific code first:
#! /Usr/bin/Python "introduces the python SDK package" Import Weibo "Three required information for authorization. app_key and app_secret are allocated when an application is created, call_back is set on the application settings page. [Note] the call_back address used for authorization must be consistent with the call_back address set in the application. Otherwise, the redirect_uri_mismatch error may occur. "App_key = '2017 ...... 'app _ secret = '91a57c ...... 'Call _ back = 'HTTP: // bingbingrobot.sinaapp.com/'def run (): # apiclient of the Weibo module is a class for authorization and API operations. First, it defines a class object and the input parameter is app_key, app_secret, call_backclient = Weibo. apiclient (app_key, app_secret, call_back) # obtain this application (app_key is unique) and provide the user with the URL of urlauth_url = client. get_authorize_url () # print the URL authorized by the user and copy the URL to the browser. The server returns a URL containing a code field (1) print "auth_url:" + auth _ URL # enter this code value (2) code = raw_input ("input the retured Code:") # obtain access_token through this code. r is the returned Authorization result, for specific parameters refer to the official documentation: # http://open.weibo.com/wiki/Oauth2/access_tokenr = client. request_access_token (CODE) # Set access_token and expire_in to the client object client. set_access_token (R. access_token, R. expires_in) # the above steps are the authorization process. Now the client can call the interface to perform Weibo operations at will. The following code is to send a new microblog with the content entered by the user while true: print "ready! Do you 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 content :') if content: # Call the interface to send a new thin image. The status parameter is the Weibo content client. statuses. update. post (status = content) print "Send succesfully! "Break; else: Print" error! Empty content! "If choice = 'n' or choice = 'N': breakif _ name _ =" _ main _ ": Run ()
Figure 1
Figure 2
After the program is successfully executed, you can view the newly published weibo on Weibo, as shown in figure 3.
Figure 3
Authorization interface and API call
Because the classes and functions provided by the SDK are not described in detail, the authorization process is simple and fixed. You can view the module's source code file Weibo. PY, find the python module installation directory in the system, you can find this file, Weibo in my system (UBUNTU. py is located at:/usr/local/lib/python2.7/dist-packages /.
Although there is no API description for Weibo operations, there are some rules. Now Sina Weibo open platform API documentation (http://open.weibo.com/wiki/%E5%BE% AE %E5%8D%9AAPI) to find the need to call the API, for example, the above need to send a microblog, find the function of the API documentation (http://open.weibo.com/wiki/2/statuses/update), content figure 4.
Figure 4
The URL accessed by this API is displayed. Replace "/" in the URL with "/" of the client object. ", then we can see the HTTP Request Method of this API. If post is used, the post interface is called. If it is get, the get interface is called. If the call method of this API is client. statuses. update. post (status = content), parameters are given in the form of key = value. Specific parameters are described in the API documentation in great detail. Therefore, we can see that API calls are very convenient.
There are a lot of apps that need to repeatedly authorize Weibo using sdks on the Internet, and most of them are similar. The steps are to authorize and then call the API, in addition, the authorization process is to copy the URL to the browser, and then obtain the access_token by inputting the obtained code. So here is a question (this is also a question raised by many friends on the Internet): Do I have to perform an authorization every time I send a microblog to run the program? Isn't that troublesome? In addition, the official documents also show that the authorization can last for a period of time (the test level is one day), but the re-authorization is required for every running of the program, isn't it necessary? The answer is: there is no need to authorize the program to run each time. The last successful authorization will continue to be valid as long as it does not exceed the validity period. How can this problem be solved? Continue in the next section.
By: