This article mainly introduces the Python script for automatically sending images to Weibo, the easiest way to automatically send Weibo messages is to call the API of Sina Weibo (because it is just a simple Weibo message, so there is no need to use its SDK ). Reference development document http://open.weibo.com/wiki/API for coding
Create an application
To use Weibo APIs, you must first have an application. You can register an application on the site. The main purpose of registering an application is to obtain MY_APPKEY and MY_ACCESS_TOKEN,
Get access_token
You must log on to the API to obtain the access_token. Reference
First, call the https://api.weibo.com/oauth2/authorizeinterface and unzip code.
This interface has three required parameters:
• Client_id: The AppKey allocated when the application is applied.
• Redirect_url: the callback address set in the application creation.
• Response_type: Response type, which can be set to code
The specific approach is to open the https://api.weibo.com/oauth2/authorize in the browser? Client_id = 123050457758183 & redirect_uri = http://www.example.com/response&response_type=code. This method will go to the authorization page. After authorization, it will go to http://www.example.com/response&code=codeto record the codein the URL.
Next, call the https://api.weibo.com/oauth2/access_tokeninterface and define access_token.
This interface has the following required parameters:
• Client_id: The AppKey allocated when the application is applied.
• Client_secret: The AppSecret allocated when the application is applied.
• Grant_type: Request type. Enter authorization_code.
• Code: the code value obtained by calling authorize.
• Redirect_uri: the callback address set in the application creation.
The specific method is to construct a POST request, find the access_token in the returned data, and save it. Specific Python code:
Import requestsurl_get_token = "https://api.weibo.com/oauth2/access_token" # construct the POST parameter playload = {"client_id": "fill in your", "client_secret": "fill in your", "grant_type ": "authorization_code", "code": "CODE obtained above", "redirect_uri": "Your callback address"} # POST request r = requests. post (url_get_token, data = playload) # print r. text
If it is normal, the following json data will be returned:
{"Access_token": "What we want to write down", "remind_in": "157679999", "expires_in": 157679999, "uid": "1739207845 "}
According to the returned data, the value of access_token is what we need. Here, the value of remind_in is the validity period of access_token, measured in seconds. We can see that this time has been 3 or 4 years and is enough for us to use it.
Publish a microblog with pure text
Call the API https://api.weibo.com/2/statuses/update.jsonto write a table. Its details are as follows:
Required:
• Access_token: The access_token we obtained in the previous step.
• Status: the content of the Weibo text to be published must be URLencode with no more than 140 Chinese Characters
Code:
# Url_post_a_text = "https://api.weibo.com/2/statuses/update.json" # construct the POST parameter playload = {"access_token": "fill in your", "status ": "This is a text test @ TaceyWong"} # POST request, posting text Weibo r = requests. post (url_post_a_text, data = playload)
If it is normal, the following result will be returned:
Posting Weibo posts with images
Call the API http://open.weibo.com/wiki/2/statuses/uploadsend table image microblogs, as shown below:
Required parameters:
• Access_token: The access_token we obtained in the previous step.
• Status: the content of the Weibo text to be published must be URLencode with no more than 140 Chinese Characters
• Pic: The image to be published, which is encoded using multipart/form-data.
Specific Code:
# Url_post_pic = "https://upload.api.weibo.com/2/statuses/upload.json" # Build a text POST parameter playload = {"access_token": "2.0086xhtbiql1_ed67706b6c8tq8xde", "status": "Test: post a text with a pic & AT someone @ meatballs "} # parameters for Constructing Binary multipart/form-data encoding files = {" pic ": open (" logo.png ", "rb")} # POST request, posting Weibo r = requests. post (url_post_pic, data = playload, files = files)
If it is normal, the result will be as follows:
Note: For specific use of requests, see [requests documentation] (http://docs.python-requests.org/en/master)