The simplest way to automatically tweet Weibo is simply to call the Sina Weibo API (because it's just a simple tweet, there's no need to use its SDK). Reference development document Http://open.weibo.com/wiki/API for code writing
Create an App
To use the API of Weibo, you need to have an application first. Any application is OK, you can register a site application registration. The primary purpose of the registration application is to obtain My_appkey and My_access_token,
Get Access_token
The call to the API requires login authorization to get access_token. Reference
First, call the Https://api.weibo.com/oauth2/authorize interface and get code.
There are three required parameters for this interface:
client_id: The Appkey that is assigned when the application is applied.
Redirect_url: To create a callback address set in your app
response_type: Response type, can be set to code
The specific approach is to open the https://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http://in the browser Www.example.com/response&response_type=code. The method goes to the authorization page, which then goes to Http://www.example.com/response&code=CODE, recording the code in the URL.
Next, call the Https://api.weibo.com/oauth2/access_token interface and get the Access_token.
The interface has the following required parameters:
client_id: The Appkey that is assigned when the application is applied.
Client_secret: The Appsecret that is assigned when the application is applied.
Grant_type: Type of request, fill in Authorization_code
Code: Call the code value obtained by authorize.
Redirect_uri: To create a callback address set in your app
The practice is to build a POST request, and then find the Access_token in the returned data and save it. Specific Python code:
If normal, the following JSON data is returned:
{"Access_token": "We want to note", "remind_in": "157679999", "expires_in": 157679999, "UID": "1739207845"}
Based on the returned data, the value of Access_token is what we want. Where the value of remind_in is the validity of Access_token, in seconds, we can see that this time is 3, 4 years long, enough for us to use.
Publish Pure text Weibo
Call Interface Https://api.weibo.com/2/statuses/update.json published text Weibo, the parameters are as follows
One of the necessary:
Access_token: That's the access_token we got in the last step.
Status: To publish the microblog text content, must do urlencode, content not more than 140 characters
Specific code:
If normal, there will be a result to the following
Post a tweet with pictures
Call Interface Http://open.weibo.com/wiki/2/statuses/upload Post a picture Weibo with the following parameters:
One of the required parameters:
Access_token: That's the access_token we got in the last step.
Status: To publish the microblog text content, must do urlencode, content not more than 140 characters
Pic: The image to be published, using the Multipart/form-data encoding method
The specific code:
#发表图文微博的接口url_post_pic = "Https://upload.api.weibo.com/2/statuses/upload.json" #构建文本类POST参数playload ={"Access_ Token ":" 2.0086xhtbiqlh7ed67706b6c8tq8xde "," status ":" Test:post a text with a pic & at someone@ meatball 覠 "} #构建二进制multipart/ Form-data encoded Parameters files={"pic": Open ("Logo.png", "RB")} #POST请求, published Weibo r = requests.post (Url_post_pic,data=playload,files = Files
If it works, the result will look like this:
Note:please refer to [Requests document] (http://docs.python-requests.org/en/master/) for specific usage of requests.