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

yipeiwu_com6年前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设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

python的time模块和datetime模块实例解析

这篇文章主要介绍了python的time模块和datetime模块实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1. 将当前...

SublimeText 2编译python出错的解决方法(The system cannot find the file specified)

[Error 2] The system cannot find the file specified 解决方法:1.环境变量path添加:C:\Python32\Tools\Scrip...

对python实现模板生成脚本的方法详解

最近项目需要,针对主项目提取一个小的基础版本,供于在新建项目时使用,所以就有这个python模板生成脚本,其作用如下: 1、通过配置文件来控制模板中的数据、格式化的过滤条件 2、执行后会...

Python2.x利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands...

Python 经典算法100及解析(小结)

1:找出字符串s="aaabbbccceeefff111144444"中,字符出现次数最多的字符 (1)考虑去重,首先将字符串进行过滤去重,这样在根据这些字符进行循环查询时,将会减少循...