-
微博开放平台,注册和添加应用。https://open.weibo.com/connect
-
前端生成链接,调用微博接口。获取code
import requests, urllib # 请求地址 url = 'https://api.weibo.com/oauth2/authorize?' # 请求数据 data = { 'client_id': '3801557366', 'redirect_uri': 'http://127.0.0.1:8080/callback_weibo/' } # 拼接成一个完成请求地址,前端浏览器调用此链接,然后就会到你的回调地址 requst_url = url + urllib.parse.urlencode(data)
-
在前端的回调接口中,获取code,并将code传递到后端接口。后端调
https://api.weibo.com/oauth2/access_token
通过code获取uid和access_token
.
文档https://open.weibo.com/wiki/Oauth2/access_token
。uid用户id,用于存储在数据库中。access_token,用户登录的token,用于调用微博的其它接口。url = "https://api.weibo.com/oauth2/access_token" data = { "client_id": "3801557366", "client_secret": "4ee10860a72fd3f607c74fba43c83364", "grant_type": "authorization_code", "code": "df8a1b7d0c763fa5a0bab3dde1362ed1", "redirect_uri": "http://127.0.0.1:8080/callback_weibo/" } res = requests.post(url=url, data=data).text dic = json.loads(res) # 获取uid uid = dic['uid'] # 获取token access_token = dic['access_token']
-
从微博接口,获取用户的详细信息。
url = "https://api.weibo.com/oauth2/get_token_info" data = { "access_token": access_token } res = requests.post(url=url, data=data).text