1

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();
        }
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
fay
  • 13
  • 9
  • Is this how the documentation says to create a token? Serializing the credentials to JSON in the message body? Are you sure the API isn't expecting the `grant_type` to be in a header? – amura.cxg Jun 10 '19 at 15:06
  • let me take a image of postman to show you what postman took, ill edit this – fay Jun 10 '19 at 15:19
  • Your API expects `application/x-www-form-urlencoded`. Eventhough you are setting the correct content type in the header, you are posting a JSON string, because `JavascriptSerializer` creates JSON. Maybe this question helps to correctly create your body https://stackoverflow.com/questions/5665558/c-sharp-httpwebrequest-of-type-application-x-www-form-urlencoded-how-to-send – derpirscher Jun 10 '19 at 15:53

1 Answers1

0

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;
            }
        }
    }
}
fay
  • 13
  • 9
amura.cxg
  • 2,348
  • 3
  • 21
  • 44
  • let me give this a shot – fay Jun 10 '19 at 15:35
  • So it did not work, ill update my code to show you what I just did – fay Jun 10 '19 at 15:40
  • Another thing you may want to check is that you're sending the correct headers. Looking at your screenshot it looks like there are 9 headers sent to the API. They may or may not be important (hard to tell without seeing them) – amura.cxg Jun 10 '19 at 15:40
  • I checked all and looks like headers are all in there – fay Jun 10 '19 at 15:44
  • After trying your code I got a different error but it seems like it picked things up just error after it, error_description":" password parameter is required for grant_type=password","error":"invalid_request – fay Jun 10 '19 at 15:59
  • Hey if I wanted to access values, how would I do that? with the access of this accesstoken... as asked in this link .. https://stackoverflow.com/questions/56531267/access-token-which-i-am-able-to-grab-now-i-want-to-grab-some-basic-info-like-bo – fay Jun 10 '19 at 18:45