Search code examples
c#.netasp.net-web-apiaccess-token

Invalid request, when I point to my URL http://localhost:59185/api/values ... hoping to get back my access token instead get Error


I have a Web API project in visual studio where I am redirecting a web URL to my localhost, I have my model class and Values Controller and currently trying to get a response from my API.

Postman image (how postman took the credentials) Click here

While running this project I run into a Error message on my URL -> http://localhost:59185/api/values

Error Message:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"error":"invalid_request","error_description":"grant_type is required for issuance"}
</string>

It's telling me grant_type is required for issuance but clearly I have identified it my 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();
        }

Solution

  • Looking at your Postman screenshot it looks like you're passing the content to the API incorrectly. It should be something like this:

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