暂无分类
暂无标签
发布于2020-09-30 19:03 阅读(875) 评论(0) 点赞(2) 收藏(5)
0
1
2
3
4
5
6
7
# 获取用户授权信息
class Get_UserInfo(APIView):
def post(self, request):
code = request.data.get('code') # 获取前端传来的code
print("code---", code)
request_url = "https://api.weixin.qq.com/sns/oauth2/access_token"
request_url += "?appid=" + corn.app_id # 微信公众号给开发者的app_id
request_url += "&secret=" + corn.app_secret # 微信公众号给开发者的app_secret
request_url += "&code=" + code # 前端传来的code
request_url += "&grant_type=authorization_code"
data = requests.get(request_url).json() # 访问这个链接,获取用户openID、access_token
print(data)
access_token = data.get("access_token") # 拿到上一步的里边的access_token
# print(data)
openid = data.get("openid") # 拿到链接里的openid
url = "https://api.weixin.qq.com/sns/userinfo?access_token={ACCESS_TOKEN}&openid={OPENID}&lang=zh_CN"
这里的access_token和openid是上边get获取到的
request = url.format(ACCESS_TOKEN=access_token, OPENID=openid)
now_data = requests.get(request).json() # requests访问这个链接就可以获取到用户详细信息
now_openid = now_data.get('openid') # 用户openid
headimgurl = now_data.get('headimgurl') # 用户头像
return Response(now_data) # 将用户信息返回给前端(如果你要的话)
到这里就完事了
0
1
2
3
4
5
作者:你太美丽
链接: https://www.pythonheidong.com/blog/article/555170/e1b65590d8a2c3c07ffa/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2019 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系z452as@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!