您现在的位置是: 网站首页 >Django >Vue+Django REST framework前后端分离生鲜电商 Django

【Vue+DRF生鲜电商】34.第三方登录(微博、qq和微信)之微博登录登录测试

admin2019年10月30日 11:21 Django | Html | JavaScript | Vue 1788人已围观

Vue+Django REST framework前后端分离生鲜电商简介 Vue+Django REST framework 打造前后端分离的生鲜电商项目(慕课网视频)。 Github地址:https://github.com/xyliurui/DjangoOnlineFreshSupermarket ; Django版本:2.2、djangorestframework:3.9.2。 前端Vue模板可以直接联系我拿。

## 微博登录 可以搜索微博开放平台, https://open.weibo.com/connect 注册个人信息 ### 注册微博开放平台 创建应用 ![BLOG_20191030_112937_37](/media/blog/images/2019/10/BLOG_20191030_112937_37.png "博客图集BLOG_20191030_112937_37.png") 然后在 我的应用 中就可以看到创建的应用,测试的时候不用通过审核也可以。 在我的应用-应用信息-基本信息 可以看到App Key、App Secret等信息,这是之后要使用的 在我的应用-应用信息-高级信息 里面填写授权回调也,可以根据自己的实际填写 ![BLOG_20191030_112930_57](/media/blog/images/2019/10/BLOG_20191030_112930_57.png "博客图集BLOG_20191030_112930_57.png") 在我的应用-应用信息-测试信息 将自己的账号添加到测试账号中,未审核应用只能通过测试账号登录。 ![BLOG_20191030_112925_77](/media/blog/images/2019/10/BLOG_20191030_112925_77.png "博客图集BLOG_20191030_112925_77.png") ### 微博授权登录 可以访问 https://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6 查看官方的文档 **接口文档** | 接口 | 说明 | | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | | [OAuth2/authorize](https://open.weibo.com/wiki/Oauth2/authorize "Oauth2/authorize") | 请求用户授权Token | | [OAuth2/access_token](https://open.weibo.com/wiki/index.php?title=Oauth2/access_token&action=edit&redlink=1 "Oauth2/access token(尚未撰写)") | 获取授权过的Access Token | | [OAuth2/get\_token\_info](https://open.weibo.com/wiki/Oauth2/get_token_info "Oauth2/get token info") | 授权信息查询接口 | | [OAuth2/revokeoauth2](https://open.weibo.com/wiki/Oauth2/revokeoauth2 "Oauth2/revokeoauth2") | 授权回收接口 | | [OAuth2/get\_oauth2\_token](https://open.weibo.com/wiki/index.php?title=Oauth2/get_oauth2_token&action=edit&redlink=1 "Oauth2/get oauth2 token(尚未撰写)") | OAuth1.0的Access Token更换至OAuth2.0的Access Token | 在 apps/users 创建一个 oauth_weibo.py 文件,用于测试微博登录 #### oauth2/authorize【GET】 文档 https://open.weibo.com/wiki/Oauth2/authorize **请求参数** | | 必选 | 类型及范围 | 说明 | | ------------ | ----- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | client_id | true | string | 申请应用时分配的AppKey。 | | redirect_uri | true | string | 授权回调地址,站外应用需与设置的回调地址一致,站内应用需填写canvas page的地址。 | | scope | false | string | 申请scope权限所需参数,可一次申请多个scope权限,用逗号分隔。[使用文档](https://open.weibo.com/wiki/Scope "Scope") | | state | false | string | 用于保持请求和回调的状态,在回调时,会在Query Parameter中回传该参数。开发者可以用这个参数验证请求有效性,也可以记录用户请求授权页前的位置。这个参数可用于防止跨站请求伪造(CSRF)攻击 | **返回数据** | 返回值字段 | 字段类型 | 字段说明 | | --------- | ------- | ------------------------------------------------------------- | | code | string | 用于第二步调用oauth2/access_token接口,获取授权后的access token。 | | state | string | 如果传递参数,会回传该参数。 | **示例** ``` //请求 https://api.weibo.com/oauth2/authorize?client\_id=123050457758183&redirect\_uri=http://www.example.com/response&response_type=code //同意授权后会重定向 http://www.example.com/response&code=CODE ``` ![BLOG_20191030_112915_67](/media/blog/images/2019/10/BLOG_20191030_112915_67.png "博客图集BLOG_20191030_112915_67.png") #### oauth2/access_token【POST】 文档 https://open.weibo.com/wiki/Oauth2/access_token **请求参数** | | 必选 | 类型及范围 | 说明 | | ------------- | ---- | --------- | -------------------------------- | | client_id | true | string | 申请应用时分配的AppKey。 | | client_secret | true | string | 申请应用时分配的AppSecret。 | | grant_type | true | string | 请求的类型,填写authorization_code | **grant\_type为authorization\_code时** | | 必选 | 类型及范围 | 说明 | | --- | --- | --- | --- | | code | true | string | 调用authorize获得的code值。 | | redirect_uri | true | string | 回调地址,需需与注册应用里的回调地址一致。 | **返回数据** ```json { "access_token": "ACCESS_TOKEN", "expires_in": 1234, "remind_in":"798114", "uid":"12341234" } ``` | 返回值字段 | 字段类型 | 字段说明 | | ------------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | access_token | string | 用户授权的唯一票据,用于调用微博的开放接口,同时也是第三方应用验证微博用户登录的唯一票据,第三方应用应该用该票据和自己应用内的用户建立唯一影射关系,来识别登录状态,不能使用本返回值里的UID字段来做登录识别。 | | expires_in | string | access_token的生命周期,单位是秒数。 | | remind_in | string | access\_token的生命周期(该参数即将废弃,开发者请使用expires\_in)。 | | uid | string | 授权用户的UID,本字段只是为了方便开发者,减少一次user/show接口调用而返回的,第三方应用不能用此字段作为用户登录状态的识别,只有access_token才是用户授权的唯一票据。 | #### users/show【GET】 文档 https://open.weibo.com/wiki/2/users/show **请求参数** | | 必选 | 类型及范围 | 说明 | | --- | --- | --- | --- | | access_token | true | string | 采用OAuth授权方式为必填参数,OAuth授权后获得。 | | uid | false | int64 | 需要查询的用户ID。 | | screen_name | false | string | 需要查询的用户昵称。 | **注意事项** - 参数uid与screen_name二者必选其一,且只能选其一; - 接口升级后,对未授权本应用的uid,将无法获取其个人简介、认证原因、粉丝数、关注数、微博数及最近一条微博内容。 **返回数据** ```json { "id": 1404376560, "screen_name": "zaku", "name": "zaku", "province": "11", "city": "5", "location": "北京 朝阳区", "description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。", "url": "http://blog.sina.com.cn/zaku", "profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1", "domain": "zaku", "gender": "m", "followers_count": 1204, "friends_count": 447, "statuses_count": 2908, "favourites_count": 0, "created_at": "Fri Aug 28 00:00:00 +0800 2009", "following": false, "allow_all_act_msg": false, "geo_enabled": true, "verified": false, "status": { "created_at": "Tue May 24 18:04:53 +0800 2011", "id": 11142488790, "text": "我的相机到了。", "source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>", "favorited": false, "truncated": false, "in_reply_to_status_id": "", "in_reply_to_user_id": "", "in_reply_to_screen_name": "", "geo": null, "mid": "5610221544300749636", "annotations": [], "reposts_count": 5, "comments_count": 8 }, "allow_all_comment": true, "avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1", "verified_reason": "", "follow_me": false, "online_status": 0, "bi_followers_count": 215 } ``` | 返回值字段 | 字段类型 | 字段说明 | | -------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | id | int64 | 用户UID | | idstr | string | 字符串型的用户UID | | screen_name | string | 用户昵称 | | name | string | 友好显示名称 | | province | int | 用户所在省级ID | | city | int | 用户所在城市ID | | location | string | 用户所在地 | | description | string | 用户个人描述 | | url | string | 用户博客地址 | | profile\_image\_url | string | 用户头像地址(中图),50×50像素 | | profile_url | string | 用户的微博统一URL地址 | | domain | string | 用户的个性化域名 | | weihao | string | 用户的微号 | | gender | string | 性别,m:男、f:女、n:未知 | | followers_count | int | 粉丝数 | | friends_count | int | 关注数 | | statuses_count | int | 微博数 | | favourites_count | int | 收藏数 | | created_at | string | 用户创建(注册)时间 | | following | boolean | 暂未支持 | | allow\_all\_act_msg | boolean | 是否允许所有人给我发私信,true:是,false:否 | | geo_enabled | boolean | 是否允许标识用户的地理位置,true:是,false:否 | | verified | boolean | 是否是微博认证用户,即加V用户,true:是,false:否 | | verified_type | int | 暂未支持 | | remark | string | 用户备注信息,只有在查询用户关系时才返回此字段 | | status | object | 用户的最近一条微博信息字段 [详细](http://open.weibo.com/wiki/%E5%B8%B8%E8%A7%81%E8%BF%94%E5%9B%9E%E5%AF%B9%E8%B1%A1%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84#.E5.BE.AE.E5.8D.9A.EF.BC.88status.EF.BC.89) | | allow\_all\_comment | boolean | 是否允许所有人对我的微博进行评论,true:是,false:否 | | avatar_large | string | 用户头像地址(大图),180×180像素 | | avatar_hd | string | 用户头像地址(高清),高清头像原图 | | verified_reason | string | 认证原因 | | follow_me | boolean | 该用户是否关注当前登录用户,true:是,false:否 | | online_status | int | 用户的在线状态,0:不在线、1:在线 | | bi\_followers\_count | int | 用户的互粉数 | | lang | string | 用户当前的语言版本,zh-cn:简体中文,zh-tw:繁体中文,en:英语 | #### 认证整个过程代码 ```python # apps/users/oauth_weibo.py import json import re import requests from urllib.parse import urlencode, parse_qs class OAuth_Weibo(object): def __init__(self, client_id, client_secret, redirect_uri, state): self.client_id = client_id # 申请应用时分配的AppKey。 self.client_secret = client_secret # 申请的密钥 self.redirect_uri = redirect_uri # 授权回调地址,站外应用需与设置的回调地址一致,站内应用需填写canvas page的地址。 self.state = state # 防跨域攻击,随机码 self.access_token = '' # 获取到的token,初始化为空 self.uid = '' # 记录用户id def get_auth_url(self): """ 登录时,获取认证的url,跳转到该url进行github认证 :return: https://api.weibo.com/oauth2/authorize?client_id=********&redirect_uri=http://1270.0.1:8000/oauth/weibo_check&state=****** """ auth_url = "https://api.weibo.com/oauth2/authorize" params = { 'client_id': self.client_id, # 申请应用时分配的AppKey。 'redirect_uri': self.redirect_uri, # 授权回调地址,站外应用需与设置的回调地址一致,站内应用需填写canvas page的地址。 'state': self.state # 不可猜测的随机字符串。它用于防止跨站点请求伪造攻击。 } url = "{}?{}".format(auth_url, urlencode(params)) # urlencode将字典拼接成url参数 # print(url) return url def get_access_token(self, code): """ 认证通过后,生成code,放在url中,视图中获取这个code,调用该函数,post提交请求token,最终得到token :param code: get_auth_url这一步中认证通过后,跳转回来的url中的code,10分钟过期。 :return: """ access_token_url = 'https://api.weibo.com/oauth2/access_token' data = { 'client_id': self.client_id, # 申请应用时分配的AppKey。 'client_secret': self.client_secret, # 申请应用时分配的AppSecret。 'grant_type': 'authorization_code', # 请求的类型,填写authorization_code 'code': code, # 调用authorize获得的code值,请求get_auth_url的地址返回的值。 'redirect_uri': self.redirect_uri, # 授回调地址,需需与注册应用里的回调地址一致。 } r = requests.post(access_token_url, data=data) # print(r.text) # '{"access_token":"2.00Ph1u5ChenG9C41634ac9ad_Q9o2D","remind_in":"157679999","expires_in":157679999,"uid":"2200323657","isRealName":"true"}' res = json.loads(r.text) if 'error' in res: # token错误 print(res['error_description']) else: self.access_token = res['access_token'] self.uid = res['uid'] return self.access_token def get_user_info(self): """ 根据token和uid获取用户信息 :return: """ user_info_url = 'https://api.weibo.com/2/users/show.json' params = {'access_token': self.access_token, 'uid': self.uid} # 根据token获取用户信息 r = requests.get(user_info_url, params=params) # print(r.json()) return r.json() if __name__ == '__main__': app_key = '224xxx919' app_secret = 'bd43cebdxxxxxxxxc093xxxxxxxx513a' redirect_uri = 'http://127.0.0.1:8000/oauth/weibo_check' state = 'hj*&(hkjhfs76^hJHKULKG89798we' oauth = OAuth_Weibo(client_id=app_key, client_secret=app_secret, redirect_uri=redirect_uri, state=state) auth_url = oauth.get_auth_url() print(auth_url) # 访问该页面进行授权认证 # 完成后跳回本地URL:http://127.0.0.1:8000/oauth/weibo_check?state=hj%2A%26%28hkjhfs76%5EhJHKULKG89798we&code=3b108579d3f025811ca22e79d4b62bed # 本地获取到url的参数值,判断return_state是否和以前的state相等 return_state = 'hj%2A%26%28hkjhfs76%5EhJHKULKG89798we' return_code = '3b108579d3f025811ca22e79d4b62bed' # 使用code来获取token,code只能使用一次,使用后失效 access_token = oauth.get_access_token(code=return_code) # 将token和uid保存在类中 print(access_token) user_info = oauth.get_user_info() # 根据token和uid获取用户信息,最终用户把这些信息保存在session中, print(user_info) # 通过 user_info['name'] 获取该用户昵称 ``` 可以根据自己的使用进行修改。

很赞哦! (2)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行2077天
  • 系统信息:Linux
  • 后台程序:Python: 3.8.10
  • 网站框架:Django: 3.2.6
  • 文章统计:256 篇
  • 文章评论:60 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 公众号:微信扫描二维码,关注我们
  • QQ群:QQ加群,下载网站的学习源码
返回
顶部
标题 换行 登录
网站