[英]Django + Angular + Django-allauth
我正在創建一個Web應用程序,使用Django作為后端,Angular作為前端。
Angular在localhost:9000
上的Yeoman堆棧上運行,而Django在localhost:8000
上運行,我正在使用grunt-contrib-proxy
將所有$http
調用從/api
angular重定向到django端口。 因此,例如,如果Angular請求localhost:9000/api/hello
這將重定向到localhost:8000/api/hello
,django將為其提供服務。
我打算設置Django Rest Framework
來為/api
路徑提供所有Angular請求。
到現在為止還挺好。
現在,我已經安裝了Django-allauth
用於對第三方服務進行Oauth
身份驗證。 它使用普通的舊Django工作,但我不知道如何與Angular一起使用它。
唯一想到的是通過django rest框架提供allauth
視圖,但是在認證后重定向呢? 我無法繞過它。
是不是更好地放棄這種方法並直接從前面(Angular)進行Oauth身份驗證?
編輯:
我設法從Angular調用login
視圖
在grunt-contrib-proxy中我添加了帳戶上下文和重寫規則:
context: ['/api', '/accounts'], rewrite: { '^/api': '/api', '^/account': '/accounts' }
我已經從angular進行了ajax調用,要求allaluth登錄視圖(例如對於github): $http.get('/accounts/github/login/?process=login')
問題是我回來了:
XMLHttpRequest cannot load https://github.com/login/oauth/authorize?scope=&state=BlaBla&redirect…ub%2Flogin%2Fcallback%2F&response_type=code&client_id=BlaBlaBla. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. (index):1
(BlaBla是我添加的)。 我想我做錯了
你需要添加一個
Origin: http://localhost:9000
到角度發送到Django的請求中的標頭。
還要確保運行Django的服務器返回一個
Access-Control-Allow-Origin: *
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.