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计算最大优先级队列实例
Dec 18 Python
Python中使用gzip模块压缩文件的简单教程
Apr 08 Python
python threading模块操作多线程介绍
Apr 08 Python
详解Python实现按任意键继续/退出的功能
Aug 19 Python
Python字典简介以及用法详解
Nov 15 Python
Python学习入门之区块链详解
Jul 25 Python
Python实现的凯撒密码算法示例
Apr 12 Python
pandas获取groupby分组里最大值所在的行方法
Apr 20 Python
Python实现的ftp服务器功能详解【附源码下载】
Jun 26 Python
python openpyxl使用方法详解
Jul 18 Python
python爬虫 爬取58同城上所有城市的租房信息详解
Jul 30 Python
解决python DataFrame 打印结果不换行问题
Apr 09 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
基于pear auth实现登录验证
2010/02/26 PHP
PHP类继承 extends使用介绍
2014/01/14 PHP
PHP 开发者该知道的 5 个 Composer 小技巧
2016/02/03 PHP
PHP调用接口API封装的例子
2019/10/11 PHP
PHP读取文件或采集时解决中文乱码
2021/03/09 PHP
JavaScript TO HTML 转换
2006/06/26 Javascript
浅谈Javascript事件模拟
2012/06/27 Javascript
JQuery操作tr和td内容的方法实例
2013/03/06 Javascript
JS实现弹出浮动窗口(支持鼠标拖动和关闭)实例详解
2015/08/06 Javascript
JavaScript中通过提示框跳转页面的方法
2016/02/14 Javascript
学习JavaScript图片预加载模块
2016/11/07 Javascript
angular.js实现购物车功能
2017/10/23 Javascript
使用sessionStorage解决vuex在页面刷新后数据被清除的问题
2018/04/13 Javascript
详解Vue、element-ui、axios实现省市区三级联动
2019/05/07 Javascript
对vuex中store和$store的区别说明
2020/07/24 Javascript
彻底搞懂并解决vue-cli4中图片显示的问题实现
2020/08/31 Javascript
基于p5.js 2D图像接口的扩展(交互实现)
2020/11/30 Javascript
Python3基础之条件与循环控制实例解析
2014/08/13 Python
详解Python3的TFTP文件传输
2018/06/26 Python
在Python中输入一个以空格为间隔的数组方法
2018/11/13 Python
Numpy之random函数使用学习
2019/01/29 Python
python TF-IDF算法实现文本关键词提取
2019/05/29 Python
python 基于TCP协议的套接字编程详解
2019/06/29 Python
pandas读取CSV文件时查看修改各列的数据类型格式
2019/07/07 Python
Python修改列表值问题解决方案
2020/03/06 Python
django之从html页面表单获取输入的数据实例
2020/03/16 Python
Python爬虫之Selenium库的使用方法
2021/01/03 Python
如何利用python 读取配置文件
2021/01/06 Python
CSS3实现背景透明文字不透明的示例代码
2018/06/25 HTML / CSS
Lovedrobe官网:英国领先的大码服装品牌
2019/09/19 全球购物
大学生毕业求职自荐书范文
2014/02/04 职场文书
《中国的气候》教学反思
2014/02/23 职场文书
挂职自我鉴定
2014/02/26 职场文书
家长通知书家长意见
2015/06/03 职场文书
《比的意义》教学反思
2016/02/18 职场文书
python Tkinter的简单入门教程
2021/04/11 Python