0

I have a web api that is working great in test using an access token / bearer authentication. I authenticate and make requests using HttpClient. Easy.

Here is the basic web client setup. The base address is a constant that I change when moving to production.

    public static HttpClient GetClient()
    {

        HttpClient Client = new HttpClient();

        Client.BaseAddress = new Uri(Ics2Constants.ICS2APIBaseAddress);
        Client.DefaultRequestHeaders.Accept.Clear();
        Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        return Client;
    }

I build the token request login info like so:

       var values = new Dictionary<string, string>();
        values.Add("grant_type", "password");
        values.Add("username", "niceUser");
        values.Add("password", "NiCePaSsWord");
        var loginContent = new FormUrlEncodedContent(values);

And then I make the request for the access token:

        var loginResponse = await client.PostAsync("/Token", loginContent);

In test mode, perfect. I can get my access token, pass it back on subsequent requests. All is good.

When I move to production. I get a bad request 400 on the request for access token. I do have the base address right because if I take off the authorize attribute I can get data back.

Something is different about the request for access token in production, but I have no clue what to change.

pStan
  • 1,084
  • 3
  • 14
  • 36

1 Answers1

0

Well, the answer ended up being two part:

1) Issue with the web host. They had a corruption on their end.

2) After they fixed their issue I still received a 404 (not found)... so I had to take out the "/" in my PostAsync. So the new line looks like so:

var loginResponse = await client.PostAsync("Token", loginContent);  

It worked in debug on the local side with the "/", but the production side was not happy.

I'm up and working now. :)

pStan
  • 1,084
  • 3
  • 14
  • 36
  • It is an intresting note. I have some trouble here http://stackoverflow.com/questions/36937328/get-asp-net-mvc5-webapi-token-fails-sometimes?noredirect=1#comment61433985_36937328 and will check your solution – NoWar Apr 29 '16 at 11:47