django 微信網(wǎng)頁授權(quán)認證api的步驟詳解
微信網(wǎng)頁授權(quán)認證
根據(jù)微信官方文檔,網(wǎng)頁授權(quán)需要四個步驟,
- 用戶同意授權(quán)-獲取code
- 通過code 獲取網(wǎng)頁授權(quán)access_token
- 通過code 獲取網(wǎng)頁授權(quán)access_token
- 刷新token
- 拉去用戶信息scope為snsapi_userinfo
-檢驗授權(quán)憑證 access_token是否有效
1 授權(quán)
這是授權(quán)地址
scope=snsapi_userinfo
彈出授權(quán)頁面,可以通過`openid`獲取到昵稱,頭像,用戶信息,不需要關(guān)注就能獲取用戶信息
scope=snsapi_base
不彈出頁面,直接跳轉(zhuǎn),只能獲取openid1
def r_oauth(request):
#授權(quán)
url="https://open/weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userifo&state=openid_required#wechat_redirect"
redirect_uri="http://pypages.gongchang.com/user/"
redirect_uri=urllib.quote(redirect_uri)
return redirect(url.format(app_id,redirect_uri) #format拼接url
def get_userinfo(request):
#獲取用戶信息
code=request.GET.get("code")
if not code:
return HttpResponse("not find code")
token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
# 通過code 可以獲取到access_token ,但是code 只能獲取道一次獲取token 的時候可能要刷新,不然會獲取不到token
data=requests.get(token_url.format(app_id,app_secret,code))
#轉(zhuǎn)為json 格式的字符串
data=json.loads(data.content.decode("utf-8"))
#獲取access_token
access_token=data['access_token']
open_id=data['openid']
refresh_token=data['refresh_token']
if not access_token or not open_id:
return None # 判斷是否有token 和open_id
# 用戶的url
user_url="https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh-CN"
d=requests.get(user_url.format(access_token,open_id)
d=d.content.decode("utf-8")
if not d:
return None
d=json.loads(d)
if d.has_key("errcode") and d["errcode"]==40001:
#token過期解決
refresh_toekn_url="https://api.weixin.qq.com/sns/oauth2/refresh_token?appi={0}&grant_type=refresh_type=refresh_token&refresh_token={1}"
r_d=requests.get(refresh_token_url.format(app_id,refresh_token))
r_d=json.loads(r_d.content.decode("utf-8"))
access_token=r_d["access_token"]
d=requests.get(user_url.format(access_token,open_id))
d=d.content.decode("utf-8")
response=HttpResponse(json.dumps(d))
# 設(shè)置cookie 將用戶信息保存到cookie里面
response.set_cookie("userinfo",json.dumps(d),max_age=7 * 24 * 3600) # 設(shè)置過期時間7 天
return response
當前在這之前需要進行公眾號配置,微信網(wǎng)頁授權(quán)開發(fā)文檔
在django 里面我們需要配置appid 和app_secret
url 也要配置
url(r'^r_oauth/$', views.r_oauth), # 授權(quán) url(r'^user/$', views.get_user_info), # 獲取用戶信息
總結(jié)
以上所述是小編給大家介紹的django 微信網(wǎng)頁授權(quán)認證api的步驟詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python環(huán)境功能強大的pip-audit安全漏洞掃描工具
這篇文章主要為大家介紹了python環(huán)境中功能強大的pip-audit安全漏洞掃描工具的功能介紹及安裝使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
解決python打開https出現(xiàn)certificate verify failed的問題
這篇文章主要介紹了解決python打開https出現(xiàn)certificate verify failed的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Python Counting Bloom Filter原理與實現(xiàn)詳細介紹
這篇文章主要介紹了Python Counting Bloom Filter原理與實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-10-10
pytorch 運行一段時間后出現(xiàn)GPU OOM的問題
這篇文章主要介紹了pytorch 運行一段時間后出現(xiàn)GPU OOM的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

