Python SDK for Weibo API.
pip install pyweiboRegister the Sina Weibo account, and establish a new app in open.weibo.com.
Get
App KeyandApp Secretfrom 应用信息 -> 基本信息Set
Redirect URIin 应用信息 -> 高级信息 -> OAuth2.0授权设置 as:
Basic usage
frompyweiboimportAuthauth=Auth()When running, you need to manually input App Key and App Token:
App Key: <Your App Key>
App Secret: <Your App Secret>Next the web browser will automatically open the redirect uri.
Copy the uri to the console, and the token will be generated:
Input the url or the token: <The URL or token>Finally, the token will also be stored in ./token.json.
As for advanced usages, you can specify:
The way to read app key and secret from the local file:
LocalFileReader('./app.json')The redirect uri, default is https://api.weibo.com/oauth2/default.html
Basic usage
frompyweiboimportAuth, Client# Get the tokenauth=Auth()
token=auth.token.token# Start the clientclient=Client()
data=client.statuses.home_timeline.get(access_token=token)The APIs and their parameters can be referred from official docs.
You can access the api call like class attributes, where the last method call must be
getorpost.
client.api_name_1.api_name_2.get(param1=value1, param2=value2)- Also you can use dict index like:
client[api_name_1][api_name_2].get(param1=value1, param2=value2)- The return value is a
TextDict, where you can access the attributes directly like:
user=data.statuses[0].user- Also you can upload the picture with the parameter
pic:
withopen('image.png', 'rb') asf:
client.statuses.upload_pic.post(pic=f)- For api like
2/statuses/uploadof which the url is https://upload.api.weibo.com/2/statuses/upload.json, useUploadClientinstead ofClient:
frompyweiboimportUploadClientclient=UploadClient()
withopen('image.png', 'rb') asf:
client.statuses.upload.post(status='Image', pic=f)Project:Thesharing/pyweibo
Establish new issue if there is any question or advice.