django 微信網(wǎng)頁授權(quán)認(rèn)證api的步驟詳解
微信網(wǎng)頁授權(quán)認(rè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
-檢驗(yàn)授權(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
當(dāng)前在這之前需要進(jìn)行公眾號配置,微信網(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)認(rèn)證api的步驟詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Python socket連接中的粘包、精確傳輸問題實(shí)例分析
這篇文章主要介紹了Python socket連接中的粘包、精確傳輸問題,結(jié)合實(shí)例形式分析了Python socket連接中的粘包、精確傳輸相關(guān)問題原因、解決方案與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
使用Python編寫一個在Linux下實(shí)現(xiàn)截圖分享的腳本的教程
這篇文章主要介紹了使用Python編寫一個在Linux下實(shí)現(xiàn)截圖分享的腳本的教程,利用到了scrot和urllib2庫,需要的朋友可以參考下2015-04-04
Selenium+Python 自動化操控登錄界面實(shí)例(有簡單驗(yàn)證碼圖片校驗(yàn))
今天小編就為大家分享一篇Selenium+Python 自動化操控登錄界面實(shí)例(有簡單驗(yàn)證碼圖片校驗(yàn)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python turtle繪圖教程之七段數(shù)碼管顯示數(shù)字和字母
這篇文章主要給大家介紹了關(guān)于Python turtle繪圖教程之七段數(shù)碼管顯示數(shù)字和字母的相關(guān)資料,Python是一種流行的編程語言,可用于編寫各種類型的程序,在數(shù)碼管顯示器上數(shù)字8由7條不同的線條組成,需要的朋友可以參考下2023-10-10
python機(jī)器學(xué)習(xí)混淆矩陣及confusion?matrix函數(shù)使用
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)混淆矩陣confusion_matrix函數(shù)使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

