Search code examples
asp.net-corehttprequest

Get FedEx oAuth token from "Authorization API"


I can get the Fedex "access_token" when I create a Post request from Postman. I add the "client_id" and "client_secret" to request body as Raw string as given in the Documentation (Fedex).

PostMan creates C# Code as follows :

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://apis-sandbox.fedex.com/oauth/token");
request.Headers.Add("content-type", "application/x-www-form-urlencoded");
request.Headers.Add("Cookie",.......some cookie....");
var content = new StringContent("grant_type=client_credentials&client_id=...clientid...&client_secret=....client secret.....", null, "application/x-www-form-urlencoded");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

When I use this code in Visual Studio for testing, on request.Headers.Add("content-type", "application/x-www-form-urlencoded"); I get the error message :

Misused header name, 'content-type'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'

After searching answers I revert my code to this :

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://apis-sandbox.fedex.com/oauth/token");
HttpContent content = new FormUrlEncodedContent(
new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("client_id", "...clientid..."),
new KeyValuePair<string, string>("client_secret", "..client secret....."),
new KeyValuePair<string, string>("grant_type","client_pc_credentials")});
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
content.Headers.ContentType.CharSet = "UTF-8";
client.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await client.PostAsync(new Uri("https://apis-sandbox.fedex.com/oauth/token"), content);
        
return await response.Content.ReadAsStringAsync();

I call this method like var oAuth = MakeApiCallAsync(); This code runs without errors, but it doesn't hit the last code line (return await response.Content.ReadAsStringAsync();) and the flow continues to the calling method and the variable oAuth gets value of (Result = Yet not computed, ..... other properties)

I tried nearly all the methods and I am exhausted, any ideas what is wrong ?


Solution

  • You need to read the response code of your request, something like below. This will let you know if your request is going through successfully or getting any errors.

    var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Post, "https://apis-sandbox.fedex.com/oauth/token");
    HttpContent content = new FormUrlEncodedContent(
    new List<KeyValuePair<string, string>> {
    new KeyValuePair<string, string>("client_id", "...clientid..."),
    new KeyValuePair<string, string>("client_secret", "..client secret....."),
    new KeyValuePair<string, string>("grant_type","client_pc_credentials")});
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    content.Headers.ContentType.CharSet = "UTF-8";
    client.DefaultRequestHeaders.ExpectContinue = false;
    HttpResponseMessage response = await client.PostAsync(new Uri("https://apis-sandbox.fedex.com/oauth/token"), content);
    if (response.IsSuccessStatusCode)
    {
      return await response.Content.ReadAsStringAsync();
    }
     else{
        //api request error handling code   
    }