django – Python通過訪問令牌進行社交認證驗證失敗
我目前正在開發一個伺服器後臺,並使用Django(1.7),通過使用Python-social-auth來處理社交網路的身份驗證.
我跟著ofollow,noindex" target="_blank">Tutorial 在這個網站上,描述一個簡單的Webapp的過程.
由於伺服器應該只是一個REST-FULL後端,所以我決定在客戶端獲取訪問令牌並將其傳送到伺服器.
伺服器不會與其進行身份驗證.這個過程應該是沒有問題的,甚至在python-social-auth的文件中也是一個例子.
但是,如果我設定了一切,我會收到一個錯誤,說:“後端未找到404”.
這裡是專案的一小部分:
settings.py:(我還包括API_KEY和SECRET)
AUTHENTICATION_BACKENDS = ( #'social.backends.facebook.FacebookOAuth2', 'social.backends.google.GoogleOAuth2', 'social.backends.twitter.TwitterOAuth', 'django.contrib.auth.backends.ModelBackend', )
views.py(用於認證檢視)
from django.contrib.auth import login from social.apps.django_app.utils import psa @psa('social:complete') def register_by_access_token(request, backend): token = request.GET.get('access_token') user = request.backend.do_auth(request.GET.get('access_token')) if user: login(request, user) return 'OK' else: return 'ERROR'
這我從文件複製了海峽,只是改變了backend.do_auth來request.backend.do_auth.這似乎是文件中的錯誤.
urls.py:
... url(r'^register-by-token/(?P<backend>[^/]+)/$', 'register_by_access_token') ...
也如文件中的建議.
我只是試圖讓這個工作只是為了google-oauth,因為有一個簡單的js-lib給你訪問令牌.
這也是非常好的,我傳送一個請求
GET http://localhost:8000/register-by-token/google-oauth2/<access-token>/
如上所述,返回是沒有找到404後端.
我做了一點除錯,發現登入功能引發的錯誤不是後端的do_auth()函式.
因此,實際的認證過程是有效的.我也嘗試使用隨機生成的字串作為令牌,並得到一個相應的錯誤,使用者無法驗證.
有趣的是,使用者甚至擁有一個屬性後端,它應該儲存“social.backends.google.GoogleOAuth2”.
謝謝你,如果你留在我的長篇職位,我希望有人有一個想法可能是錯的:).
期待你的答案.
在你的register_by_access_token檢視中,你在GET引數中獲得access_token
user = request.backend.do_auth(request.GET.get('access_token'))
你的defiend是:
url(r'^register-by-token/(?P<backend>[^/]+)/$', 'register_by_access_token')
所以你需要請求:
GET http://localhost:8000/register-by-token/google-oauth2/?access_token=<access_token>
而你在做:
GET http://localhost:8000/register-by-token/google-oauth2/<access-token>/
你正在傳遞access_token在url引數,這是錯誤的.
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/26252633/python-social-auth-authentication-via-access-token-fails