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绘图库Matplotlib的安装
Jul 03 Python
详解pyqt5 动画在QThread线程中无法运行问题
May 05 Python
Python3.6简单反射操作示例
Jun 14 Python
Python3连接SQLServer、Oracle、MySql的方法
Jun 28 Python
python 剪切移动文件的实现代码
Aug 02 Python
深入理解Python中的 __new__ 和 __init__及区别介绍
Sep 17 Python
Python自定义函数计算给定日期是该年第几天的方法示例
May 30 Python
python日志模块logbook使用方法
Sep 19 Python
使用python实现男神女神颜值打分系统(推荐)
Oct 31 Python
Python统计时间内的并发数代码实例
Dec 28 Python
Python object类中的特殊方法代码讲解
Mar 06 Python
python自动统计zabbix系统监控覆盖率的示例代码
Apr 03 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
浅析ThinkPHP中execute和query方法的区别
2014/06/13 PHP
php返回字符串中所有单词的方法
2015/03/09 PHP
php实现修改新闻时删除图片的方法
2015/05/12 PHP
php实现的SSO单点登录系统接入功能示例分析
2016/10/12 PHP
使用JavaScript检测Firefox浏览器是否启用了Firebug的代码
2010/12/28 Javascript
解析dom中的children对象数组元素firstChild,lastChild的使用
2013/07/10 Javascript
Jquery实现自定义tooltip示例代码
2014/02/12 Javascript
jQuery获取对象简单实现方法小结
2014/10/30 Javascript
jQuery实现自定义事件的方法
2015/04/17 Javascript
基于jQuery仿淘宝产品图片放大镜代码分享
2020/06/23 Javascript
实例剖析AngularJS框架中数据的双向绑定运用
2016/03/04 Javascript
React.js入门实例教程之创建hello world 的5种方式
2016/05/11 Javascript
JavaScript中cookie工具函数封装的示例代码
2016/10/11 Javascript
yarn与npm的命令行小结
2016/10/20 Javascript
Angular2学习教程之ng中变更检测问题详解
2017/05/28 Javascript
vue的安装及element组件的安装方法
2018/03/09 Javascript
JavaScript DOM元素常见操作详解【添加、删除、修改等】
2018/05/09 Javascript
[01:18:33]Secret vs VGJ.S Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
Windows环境下python环境安装使用图文教程
2018/03/13 Python
pandas.DataFrame 根据条件新建列并赋值的方法
2018/04/08 Python
通过python实现弹窗广告拦截过程详解
2019/07/10 Python
对Django 转发和重定向的实例详解
2019/08/06 Python
jupyter lab的目录调整及设置默认浏览器为chrome的方法
2020/04/10 Python
Python代码注释规范代码实例解析
2020/08/14 Python
详解html5 canvas常用api总结(二)--绘图API
2016/12/14 HTML / CSS
会议开场欢迎词
2014/01/15 职场文书
店长职务说明书
2014/02/04 职场文书
《桃花心木》教学反思
2014/02/17 职场文书
公司员工活动策划方案
2014/08/20 职场文书
网站出售协议书范文
2014/10/10 职场文书
领导四风问题整改措施思想汇报
2014/10/13 职场文书
幼儿园国庆节活动总结
2015/03/23 职场文书
2015年企业工作总结范文
2015/04/28 职场文书
首都博物馆观后感
2015/06/05 职场文书
厉害!这是Redis可视化工具最全的横向评测
2021/07/15 Redis
Android 中的类文件和类加载器详情
2022/06/05 Java/Android