无效的请求,当我指向我的URL http:// localhost:59185 / api / values ...希望取回我的访问令牌而不是得到Error

时间:2019-06-10 14:42:40

标签: c# .net asp.net-web-api access-token

我在Visual Studio中有一个Web API项目,我将一个Web URL重定向到我的本地主机,我有我的模型类和Values Controller,当前正在尝试从我的API获取响应。

邮递员图片(邮递员如何获取凭据)Click here

在运行该项目时,我遇到了一条错误消息 网址-> 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类。

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();
        }

1 个答案:

答案 0 :(得分:0)

查看您的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;
            }
        }
    }
}