我试图从外部API令牌,这作品时,我尝试登录到Web应用程序,当我尝试发送通过邮递员的请求。对于这个要求,我需要设置的内容类型x-www-form-urlencoded
。我知道如何做到这一点,但它似乎总是返回400错误的请求,并告诉我,grant_type是无效的。所以踢这个任务了,这里的作品的代码,这将在登录被称为:
代码如下: -
var _login = function (loginData) {
var data = ["grant_type=password&username=", loginData.userName, "&password=", loginData.password].join('');
var deferred = $q.defer();
$http.post([serviceBase, "token"].join(''), data, { headers: { "Content-Type": "application/x-www-form-urlencoded" } }).success(function (response) {
console.log("login response", response);
localStorageService.set(_keyAuthorizationData, { token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
deferred.resolve(response);
}).error(function (err, status) {
_logOut();
deferred.reject(err);
});
return deferred.promise;
};
loginData将通过从登录表单数据来填充。
这是我的其他调用外部API返回一个400错误的请求
var _transferPersoon = function (portal, data) {
var externalAPI = "";
if (portal == "portal1") {
externalAPI = "https://urltoportal/webapi/";
} else if (portal == "portal2") {
externalAPI = "https://urltoportal/webapi/";
} else if (portal == "portal3") {
externalAPI = "https://urltoportal/webapi/";
} else if (portal == "portal4") {
externalAPI = "https://urltoportal/webapi/";
} else {
externalAPI = serviceBase;
}
var tokenData = {
username: "cactustransfer",
password: "bbbbbb",
grant_type: "password"
};
var data = ["grant_type=password&username=", "transferaccount", "&password=", "password"].join('');
$http.post([externalAPI, "token"].join(''), data, { headers: { "Content-Type": "application/x-www-form-urlencoded" } }).success(function (response) {
return response.access_token;
})
}
这是返回,并在谷歌浏览器中显示的错误:
{错误: “unsupported_grant_type”}
这个请求被发送,并通过我的ASP.NET Web API此认证的中间件通过:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
Cactus.Business.DataModel.GEBRUIKER gebruiker = null;
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] {"*"});
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
/*
* Authenticatie methode voor het verplaatsen van personeel
* Deze kan niet uitgevoerd worden als het request IP niet in het lijstje van hosts staat.
* Dit is een extra beveiliging.
*/
if (context.UserName == "cactustransfer")
{
if (!hostVerify.IsValidHost(context.Request.RemoteIpAddress))
{
using (UnitOfWork work = new UnitOfWork())
{
gebruiker = work.GebruikerRepository.ValidateUser("transferaccount", "password");
}
}
}
if (gebruiker == null)
{
using (UnitOfWork work = new UnitOfWork())
{
gebruiker = work.GebruikerRepository.ValidateUser(context.UserName, context.Password);
if (gebruiker == null)
{
context.SetError("invalid_grant",
"The username or password is incorrect, or you have insufficient rights", context.Request.RemoteIpAddress);
return;
}
}
}
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
identity.AddClaim(new Claim("providerID", gebruiker.gebruikerId.ToString()));
//identity.AddClaim(new Claim("providerID", gebruiker.persoon.ToString()));
context.Validated(identity);
}
这里是我的要求,我在邮递员,其结果正在测试:
我已经确认并尝试这些解决方案如下:
How do I POST urlencoded form data with $http without jQuery?
https://github.com/thephpleague/oauth2-server/issues/261
注:我在邮差使用的用户名实际上是一样的transferaccount用户名
编辑:
下面是在谷歌浏览器中显示的请求报头,第一种是登录,这传送给本地API,所述第二上正试图发送到外部API 的请求
更新:
所有关于功能处理数据机智外部API的已被转移到当地的ASP.NET Web API的。这样,我知道如何使它发挥作用。但是,这是不是一个真正的解决方案,而是周围的工作。
尝试这个:
$http.post([externalAPI, "token", "?", data].join(''), {}, { headers: { "Content-Type": "application/x-www-form-urlencoded" } }).success(function (response) {
return response.access_token;
})