【发布时间】:2016-04-07 00:38:02
【问题描述】:
我成功地使用 curl 请求通过 Django-social-auth 对我的 Django 项目中的用户进行身份验证,并使用 Facebook 令牌返回我个人网站的令牌。很简单,我有一个 Facebook 令牌,我将其转换为 Django 令牌作为回报,但我只是使用 CURL 请求来完成这一壮举。我使用的框架来自 Philip Garnero (1)。
CURL 请求 - curl -X POST -d "grant_type=convert_token&client_id=&client_secret=&backend=facebook&token=" http://localhost:8000/auth/convert-token
当我尝试使用 AJAX 请求执行此操作时,就会出现混乱。 我的ajax设置方式有问题吗?在将我的 facebook 令牌转换为经过身份验证的 Django 令牌之前,我是否需要先获取一个 csrf 令牌?
更新:我在通过代理运行我的 ajax 请求时收到 400 错误 unsupported_grant_type。该请求与我在 curl 命令和 Postman 命令中成功执行的请求相同。
Ajax 请求:
$http({
url: "proxy/url/...",
async: false,
crossDomain: true,
method: "POST",
data: {
"client_id": "...",
"client_secret": "...",
"grant_type": "password",
"username": "...",
"password": "..."
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, headers, config) {
console.log("error status = " + status);
console.log(angular.toJson(data));
});
(1) - https://github.com/PhilipGarnero/django-rest-framework-social-oauth2
【问题讨论】:
-
is there something wrong with the way my ajax is setup?给你同样的问题.. 什么不起作用? -
您在请求中发送空字符串。这看起来像一个问题,但您在 CURL 示例中也是如此。请更清楚您有什么问题。
-
很抱歉缺少开始的信息。我故意将字符串留空以隐藏敏感数据。您可以假设在 curl 请求中成功的相应信息随后被放置在 javascript 中。至于我收到的具体错误,它们都是状态 0。从我目前所读的内容来看,我认为跨域脚本存在问题,但我正在从 apache cordova javascript 文件运行这些脚本(这对我来说没有意义)。
-
只是想我会包含这个,我的测试脚本在一个名为 Postman 的 api 工具中工作。
-
更新:我在通过代理运行我的 ajax 请求时收到 400 错误 unsupported_grant_type。该请求与我在 curl 命令和 Postman 命令中成功执行的请求相同。
标签: angularjs django rest curl oauth2