无效请求,当我指向我的 URL http://localhost:59185/api/values ... 希望取回我的访问令牌而不是得到错误
Invalid request, when I point to my URL http://localhost:59185/api/values ... hoping to get back my access token instead get Error
我在 visual studio 中有一个 Web API 项目,我在其中将 Web URL 重定向到我的本地主机,我有我的模型 class 和 Values Controller,目前试图从我的 API 那里得到回应。
邮递员图像(邮递员如何获取凭据)Click here
在 运行 宁这个项目时,我 运行 进入我的错误消息
URL -> http://localhost:59185/api/values
错误信息:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"error":"invalid_request","error_description":"grant_type is required for issuance"}
</string>
它告诉我 grant_type 是发行所必需的,但很明显我已经确定它是我的 Valuescontroller class..
using APICredential.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Script.Serialization;
namespace APICredential.Controllers
{
[RoutePrefix("api")]
public class ValuesController : ApiController
{
[HttpGet, Route("values")]
public async Task<string> Post()
{
Credentials cred = new Credentials()
{
username = "admin@encompass:BE11200822",
password = "S*******",
grant_type = "password", //Gran_type Identified here
client_id = "gpq4sdh",
client_secret = "secret",
scope = "lp"
};
try {
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");
HttpRequestMessage request = new HttpRequestMessage
(HttpMethod.Post, "v1/token")
{
Content = new StringContent(new JavaScriptSerializer().Serialize(cred), Encoding.UTF8, "application/x-www-form-urlencoded")
};
HttpResponseMessage response = await client.SendAsync(request);
//for now, see what response gets you and adjust your code to return the object you need, if the api is returning a serialized json string.. then we can return a string here... like so
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
[HttpGet, Route("values/{id}")]
public string Get(int id)
{
return "Nice! At least we got this one working, Fay... the Id value you entered is: " + id.ToString();
}
查看您的 Postman 屏幕截图,您似乎将内容错误地传递给 API。它应该是这样的:
namespace APICredential.Controllers
{
[RoutePrefix("api")]
public class ValuesController : ApiController
{
[HttpGet, Route("values")]
public async Task<string> Post()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");
var parameters = new Dictionary<string, string>()
{
{"username", "admin@encompass:BE11200822"},
{"password ", "S*******"},
{"grant_type", "password"}, //Gran_type Identified here
{"client_id", "gpq4sdh"},
{"client_secret", "secret"},
{"scope", "lp"}
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/token")
{
Content = new FormUrlEncodedContent(parameters)
};
HttpResponseMessage response = await client.SendAsync(request);
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
}
}
我在 visual studio 中有一个 Web API 项目,我在其中将 Web URL 重定向到我的本地主机,我有我的模型 class 和 Values Controller,目前试图从我的 API 那里得到回应。
邮递员图像(邮递员如何获取凭据)Click here
在 运行 宁这个项目时,我 运行 进入我的错误消息 URL -> http://localhost:59185/api/values
错误信息:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"error":"invalid_request","error_description":"grant_type is required for issuance"}
</string>
它告诉我 grant_type 是发行所必需的,但很明显我已经确定它是我的 Valuescontroller class..
using APICredential.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Script.Serialization;
namespace APICredential.Controllers
{
[RoutePrefix("api")]
public class ValuesController : ApiController
{
[HttpGet, Route("values")]
public async Task<string> Post()
{
Credentials cred = new Credentials()
{
username = "admin@encompass:BE11200822",
password = "S*******",
grant_type = "password", //Gran_type Identified here
client_id = "gpq4sdh",
client_secret = "secret",
scope = "lp"
};
try {
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");
HttpRequestMessage request = new HttpRequestMessage
(HttpMethod.Post, "v1/token")
{
Content = new StringContent(new JavaScriptSerializer().Serialize(cred), Encoding.UTF8, "application/x-www-form-urlencoded")
};
HttpResponseMessage response = await client.SendAsync(request);
//for now, see what response gets you and adjust your code to return the object you need, if the api is returning a serialized json string.. then we can return a string here... like so
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
[HttpGet, Route("values/{id}")]
public string Get(int id)
{
return "Nice! At least we got this one working, Fay... the Id value you entered is: " + id.ToString();
}
查看您的 Postman 屏幕截图,您似乎将内容错误地传递给 API。它应该是这样的:
namespace APICredential.Controllers
{
[RoutePrefix("api")]
public class ValuesController : ApiController
{
[HttpGet, Route("values")]
public async Task<string> Post()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.elliemae.com/oauth2/");
var parameters = new Dictionary<string, string>()
{
{"username", "admin@encompass:BE11200822"},
{"password ", "S*******"},
{"grant_type", "password"}, //Gran_type Identified here
{"client_id", "gpq4sdh"},
{"client_secret", "secret"},
{"scope", "lp"}
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/token")
{
Content = new FormUrlEncodedContent(parameters)
};
HttpResponseMessage response = await client.SendAsync(request);
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
}
}