点击这里 进入 微博开放平台html
建立网页应用, 此处不须要进行审核便可使用测试环境python
此处一些信息是很重要的东西, 好比 App_key 以及 App_Serertmysql
回调页面也进行设置, 以后要用此接口进行信息回传git
基于 OAuth2.0 协议进行认证, 点击此文档github
受权以及tokensql
点击这里 官方说明数据库
经过阅读官方说明得知是 能够以 get/post 方式进行请求. URL 的生成和支付宝相似, 可是不须要加密django
所以会简单不少, 注意看必填字段便可api
直接使用拼接便可生成 url 进行访问微信
def get_auth_url(): weibo_auth_url = "https://api.weibo.com/oauth2/authorize" redirect_url = "http://127.0.0.1:8000/complete/weibo/" auth_url = weibo_auth_url + "?client_id={0}&redirect_uri={1}".format("3470xxx2804", redirect_url) print(auth_url)
根据生成的 url 访问, 会跳转到 微博提供的登陆受权页面,而后登陆后
会跳转到一个连接地址, 而且参数会提供一串 code
点击这里查看 官方文档说明
查阅官方说明可见, 此接口方式必须是 post 方式且须要提供以前 受权接口拿到的 token 才能够能够进行使用
利用 request 进行 post 的请求
def get_access_token(code="cbf6cccccccccccc6f8e7df1a9dd1c7"): access_token_url = "https://api.weibo.com/oauth2/access_token" import requests re_dict = requests.post(access_token_url, data={ "client_id": "347nnnnn04", "client_secret": "836c9d55a8xxxxxxxa5e2d08b77c8c31bd1", "grant_type": "authorization_code", "code": code, "redirect_uri": "http://127.0.0.1:8000/complete/weibo/" }) pass
请求回传中能够拿到一串认证信息 , 包括 uid,以及 access_token , 至此算是受权登陆成功, 基于这些信息才能够进行其余必须登陆后的访问操做
编写一个访问用户信息的接口进行生成操做, 用户信息接口官方文档以下 点击这里
访问成功后的回传数据, 可见测试成功
别人造的轮子又大又圆
social-auth-app-django 组件已经集成好了咱们上面的那一堆繁琐的操做
github 点这里
官方文档 点这里
From pypi: $ pip install social-auth-app-django
And for MongoEngine ORM: $ pip install social-auth-app-django-mongoengine
Add the application to INSTALLED_APPS setting, for default ORM: INSTALLED_APPS = ( ... 'social_django', ... )
And for MongoEngine ORM: INSTALLED_APPS = ( ... 'social_django_mongoengine', ... )
使用 social-auth-app-django 组件必需要将数据库的 引擎改成 innodb 否则会没法使用
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': "ytshop", 'USER': 'root', 'PASSWORD': "123456", 'HOST': "127.0.0.1", 'OPTIONS': {'init_command': 'SET storage_engine=INNODB;'} } }
此组件是带有数据表的, 所以须要进行数据库迁移,直接进行 migrate 便可, 源码中自带了 migrations
./manage.py migrate
增长了5张表
在 settings.py 中配置第三方认证方式
social_core.backends 中涵盖了全世界的主流的平台, 包括知乎豆瓣等
AUTHENTICATION_BACKENDS = ('social_core.backends.weibo.WeiboOAuth2', # 微博 'social_core.backends.weixin.WeixinOAuth2', # 微信 'social_core.backends.qq.QQOAuth2', # qq 'django.contrib.auth.backends.ModelBackend', )
urlpatterns = patterns('', ... url('', include('social_django.urls', namespace='social')) ... )
TEMPLATES = [ { ... 'OPTIONS': { ... 'context_processors': [ ... 'social_django.context_processors.backends', 'social_django.context_processors.login_redirect', ... ] } } ]
SOCIAL_AUTH_TWITTER_KEY = 'foobar' SOCIAL_AUTH_TWITTER_SECRET = 'bazqux'
配置参数须要进行更改, TWITTER 改为对应平台的大写
以微博为例 须要输入 key 以及 Secret
SOCIAL_AUTH_WEIBO_KEY = '34XXXXX2804' SOCIAL_AUTH_WEIBO_SECRET = '836c9d55a88XXXX77c8c31bd1'
# 第三方登陆后自动跳转 SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/index/'
django 和 rest-framework 的登陆流程有所不一样,
social-auth-app-django 对 django 的兼容性更好一些,可是对DRF 有所不一样
须要进行必定程度的处理, 存放进去用户以及相关的 token
须要作手脚的是下图中的部分
最后返回的时候将此代码进行更改成下面的代码
# return backend.strategy.redirect(url)
from rest_framework_jwt.serializers import jwt_payload_handler, jwt_encode_handler
payload = jwt_payload_handler(user) reponse = backend.strategy.redirect(url) reponse.set_cookie("name", user.name if user.name else user.username, max_age=24 * 3600) reponse.set_cookie("token", jwt_encode_handler(payload), max_age=24 * 3600) return reponse