django 微信网页授权认证api的步骤详解


Posted in Python onJuly 30, 2019

微信网页授权认证

根据微信官方文档,网页授权需要四个步骤,

- 用户同意授权-获取code
- 通过code 获取网页授权access_token
- 通过code 获取网页授权access_token
- 刷新token
- 拉去用户信息scope为snsapi_userinfo
-检验授权凭证 access_token是否有效

1 授权

url="https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=openid_required#wechat_redirect"1

这是授权地址

scope=snsapi_userinfo

弹出授权页面,可以通过`openid`获取到昵称,头像,用户信息,不需要关注就能获取用户信息

scope=snsapi_base

不弹出页面,直接跳转,只能获取openid1

def r_oauth(request):
  #授权
  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))
 #转为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))
  # 设置cookie 将用户信息保存到cookie里面
  response.set_cookie("userinfo",json.dumps(d),max_age=7 * 24 * 3600) # 设置过期时间7 天
  return response

当前在这之前需要进行公众号配置,微信网页授权开发文档

在django 里面我们需要配置appid 和app_secret

url 也要配置

url(r'^r_oauth/$', views.r_oauth), # 授权 
 url(r'^user/$', views.get_user_info), # 获取用户信息

总结

以上所述是小编给大家介绍的django 微信网页授权认证api的步骤详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
python自动安装pip
Apr 24 Python
跟老齐学Python之大话题小函数(2)
Oct 10 Python
Python 使用PIL numpy 实现拼接图片的示例
May 08 Python
pytorch使用Variable实现线性回归
May 21 Python
pytorch的梯度计算以及backward方法详解
Jan 10 Python
Pytorch使用MNIST数据集实现基础GAN和DCGAN详解
Jan 10 Python
Django用户身份验证完成示例代码
Apr 03 Python
Pytorch数据拼接与拆分操作实现图解
Apr 30 Python
Keras设置以及获取权重的实现
Jun 19 Python
python3.8动态人脸识别的实现示例
Sep 21 Python
Python-openpyxl表格读取写入的案例详解
Nov 02 Python
Python Pandas 如何shuffle(打乱)数据
Jul 30 #Python
python tkinter实现屏保程序
Jul 30 #Python
python pandas 时间日期的处理实现
Jul 30 #Python
Django 反向生成url实例详解
Jul 30 #Python
Python Pandas数据中对时间的操作
Jul 30 #Python
python tkinter实现彩球碰撞屏保
Jul 30 #Python
详解python pandas 分组统计的方法
Jul 30 #Python
You might like
杏林同学录(七)
2006/10/09 PHP
使用 eAccelerator加速PHP代码的目的
2007/03/16 PHP
PHP简单判断字符串是否包含另一个字符串的方法
2016/03/25 PHP
php实现微信企业付款到个人零钱功能
2018/10/09 PHP
JQuery中form验证出错信息的查看方法
2013/10/08 Javascript
button没写type=button会导致点击时提交
2014/03/06 Javascript
ECMAScript6中Map/WeakMap详解
2015/06/12 Javascript
js实现的二分查找算法实例
2016/01/21 Javascript
BootStrap中关于Select下拉框选择触发事件及扩展
2016/11/22 Javascript
关于Vue.js 2.0的Vuex 2.0 你需要更新的知识库
2016/11/30 Javascript
基于ajax与msmq技术的消息推送功能实现代码
2016/12/26 Javascript
解决AjaxFileupload 上传时会出现连接重置的问题
2017/07/07 Javascript
jQuery封装placeholder效果实现方法,让低版本浏览器支持该效果
2017/07/08 jQuery
解决Layui中layer报错的问题
2019/09/03 Javascript
js实现直播点击飘心效果
2020/08/19 Javascript
[50:28]LGD女子学院第三期 DOTA2复仇之魂教学
2013/12/24 DOTA
[02:19]2018年度DOTA2最佳核心位选手-完美盛典
2018/12/17 DOTA
python使用多线程不断刷新网页的方法
2015/03/31 Python
Python通过poll实现异步IO的方法
2015/06/04 Python
python爬虫实战之最简单的网页爬虫教程
2017/08/13 Python
python的Crypto模块实现AES加密实例代码
2018/01/22 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
2018/07/18 Python
Python3.6.2调用ffmpeg的方法
2019/01/10 Python
Python文件路径名的操作方法
2019/10/30 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
2020/01/14 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
2020/02/07 Python
新加坡网上美容店:Hermo新加坡
2019/06/19 全球购物
short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
2014/09/26 面试题
巾帼建功标兵事迹材料
2014/05/11 职场文书
高中班级口号
2014/06/09 职场文书
课内比教学心得体会
2014/09/09 职场文书
2014小学数学教研组工作总结
2014/12/06 职场文书
邀请函模板
2015/02/02 职场文书
元旦主持词开场白
2015/05/29 职场文书
nginx安装以及配置的详细过程记录
2021/09/15 Servers
与Windows10相比Windows11有哪些改进?值不值得升级?
2021/11/21 数码科技