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

yipeiwu_com7年前Python基础

微信网页授权认证

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

- 用户同意授权-获取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设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Cpy和Python的效率对比

Python 语言的初学者, 特别是"惊奇者"(也就是那种第一眼就被毫无意义的某些特性吸引, 之后持续说服自己的人)认为 Python 不需要 C 语言的 for 语句, 因为他们能用优...

PyCharm代码提示忽略大小写设置方法

PyCharm代码提示忽略大小写设置方法

如图: 具体见上图! 以上这篇PyCharm代码提示忽略大小写设置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。...

python利用正则表达式提取字符串

前言 正则表达式的基础知识就不说了,有兴趣的可以点击这里,提取一般分两种情况,一种是提取在文本中提取单个位置的字符串,另一种是提取连续多个位置的字符串。日志分析会遇到这种情况,下面我会分...

Python验证企业工商注册码

中国企业工商注册码前六位为行政区代码,中间8位顺序编码,最后一位为根据ISO 7064:1983.MOD 11-2校验码计算出来的检验码,本算法根据最后一位校验码的算法来判断企业注册码是...

python标准日志模块logging的使用方法

python标准日志模块logging的使用方法

最近写一个爬虫系统,需要用到python的日志记录模块,于是便学习了一下。python的标准库里的日志系统从Python2.3开始支持。只要import logging这个模块即可使用。...