第三方登錄開(kāi)發(fā)模式及auto2.0簡(jiǎn)介
微博登錄
第三方登錄就是跳轉(zhuǎn)到第三方的登錄頁(yè)面,只能通過(guò)第三方,不能自己設(shè)置第三方帳號(hào)的登錄頁(yè)面。防止其用戶(hù)信息泄漏。

專(zhuān)門(mén)用于第三方登錄的協(xié)議 auth2.0
進(jìn)入微博開(kāi)放平臺(tái)->
創(chuàng)建應(yīng)用->
回調(diào)url必須通過(guò)設(shè)置頁(yè)面進(jìn)行設(shè)置然后傳參,支付寶可以直接通過(guò)url傳參。
需要用到的微博接口:
| 接口 | 說(shuō)明 |
|---|---|
| OAuth2/authorize | 請(qǐng)求用戶(hù)授權(quán)Token |
| OAuth2/access_token | 獲取授權(quán)過(guò)的Access Token |
微博獲取權(quán)限的接口文檔:
http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E
微博api(文檔中有這個(gè)選項(xiàng))
http://open.weibo.com/wiki/2/users/show
以上這些只是介紹了如何獲取微博的用戶(hù)權(quán)限以及信息,并未介紹如何在自己的應(yīng)用中注冊(cè)用戶(hù)(通過(guò)獲取的用戶(hù)信息新建用戶(hù)),所以只完成了一半,源碼如下:
微博獲取用戶(hù)信息分為以下三步。
get_code-> get token -> get user_show
# -*- coding: utf-8 -*-
__author__ = 'tomtiddler'
'''此文件僅用于測(cè)試,不用于生產(chǎn)過(guò)程'''
import requests
def get_auth_url():
weibo_auth_url = "https://api.weibo.com/oauth2/authorize"
redirect_url = "http://127.0.0.1/complete/weibo/"
auth_url = weibo_auth_url+"?client_id={client_id}&redirect_uri={redirect_url}".\
format(client_id=2473478422, redirect_url=redirect_url)
print(auth_url)
def get_access_token(code):
access_token_url = "https://api.weibo.com/oauth2/access_token"
re_dict = requests.post(access_token_url, data={
"client_id": 2473478422,
"client_secret": "b1380a5ad6afcdfda02a35adc880ca20",
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "http://127.0.0.1/complete/weibo/",
})
pass
# b'{"access_token":"2.00NODyRDQj95hCab6215930dxxbwOE","remind_in":"157679999","expires_in":157679999,"uid":"3013908301","isRealName":"true"}'
def get_user_info(access_token="", uid=""):
user_url = "https://api.weibo.com/2/users/show.json?access_token={access_token}&uid={uid}".format(access_token=access_token, uid=uid)
print(user_url)
if __name__ == "__main__":
# get_auth_url()
# http://127.0.0.1/complete/weibo/?code=7bee2c5f37ab843efa3da97dc647ac59
# get_access_token("aa61f05322c79bcfc45c4fcadbc129cf")
get_user_info(access_token="2.00NODyRDQj95hCab6215930dxxbwOE", uid="3013908301")
第三方登錄
social_django集成第三方登錄
$ pip install social-auth-app-django
INSTALLED_APPS = (
...
'social_django',
...
)
./manage.py migrate ()它自身的app已經(jīng)集成了makemagerations。
加入
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth',
'social_core.backends.google.GoogleOpenId',
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.google.GoogleOAuth',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.yahoo.YahooOpenId',
...
'django.contrib.auth.backends.ModelBackend',
)
url配置
urlpatterns = patterns('',
...
url('', include('social_django.urls', namespace='social'))
...
)
template中加入,后端api,感覺(jué)不需要
TEMPLATES = [
{
...
'OPTIONS': {
...
'context_processors': [
...
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
...
]
}
}
]
關(guān)于回調(diào),ali由于有異步通知,而異步通知指向的是服務(wù)器api,所以需要啟動(dòng)服務(wù)器才能接收回調(diào)。而ali的同步和weibo的同步回調(diào)指向的是瀏覽器,所以可以設(shè)定回調(diào)ip為本地ip服務(wù)器。
setting中配置第三方相關(guān)key以及回調(diào)uri
拷貝social_core源碼并修改
有個(gè)疑問(wèn),拷貝出源碼后不需要?jiǎng)h除環(huán)境中的social_core包嗎?暫時(shí)沒(méi)刪除,感覺(jué)可能會(huì)有影響