Django + Angular + Django-allauth

5
我正在使用Django作为后端,Angular作为前端创建Web应用程序。
Angular运行在Yeoman栈上的localhost:9000上,而Django运行在localhost:8000上。我使用grunt-contrib-proxy将所有的$http调用从Angular重定向到Django端口的/api。例如,如果Angular请求localhost:9000/api/hello,则会重定向到localhost:8000/api/hello,并由Django提供服务。
我计划设置Django Rest Framework来为所有Angular请求提供/api路径服务。
到目前为止一切都好。
现在,我已经配置好并且正在工作的Django-allauth安装用于进行Oauth身份验证到第三方服务。它在使用纯旧版Django时有效,但我不知道如何与Angular一起使用。
唯一想到的事情是通过django rest framework提供allauth视图,但是身份验证后的重定向怎么办?我无法理解。
是最好放弃这种方法,直接从前端(Angular)进行Oauth身份验证吗?
编辑: 我成功地从Angular调用了登录视图。
  • In grunt-contrib-proxy I've added the account context and rewrite rule:

    context:  ['/api', '/accounts'],
    rewrite: {
      '^/api': '/api',
      '^/account': '/accounts'
    }
    
  • I've made an ajax call from angular, asking for the allaluth login view (for example for 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是我的补充说明)。我认为我在做某件完全错误的事情。


你有解决这个问题吗?我正在尝试将一个使用allauth的项目移植到Angular,这是我最大的难点。 - Fergal Moran
1
@FergalMoran 不,我现在放弃了。我转而使用Python Social Auth + Django Rest Framework。我使用了这篇博客文章作为起点,祝你好运! - Leonardo
谢谢,莱昂纳多。我想这也是我必须走的路。那篇博客文章看起来很棒,谢谢! - Fergal Moran
嗨,你找到解决方案了吗? - Reza GH
1个回答

1

你需要添加一个

Origin: http://localhost:9000 

将 Angular 发送到 Django 的请求头中。

还要确保运行 Django 的服务器返回一个

Access-Control-Allow-Origin: * 

点击此处获取更多信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接