I am trying to connect and pull tacking data from FedEx using there API.
I have been successful in connecting to and getting a response from https://apis-sandbox.fedex.com//oauth/token and getting the token.
When I then try to connect and get the tracking data I am getting "NOT.AUTHORIZED.ERROR" error response.
string input = "{\r\n \"trackingInfo\": [\r\n {\r\n \"trackingNumberInfo\": {\r\n \"trackingNumber\": \"794843185271\"\r\n }\r\n }\r\n ],\r\n \"includeDetailedScans\": true\r\n}";
var client = new RestClient("https://apis-sandbox.fedex.com");
var request = new RestRequest("/oauth/token",Method.Post);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&scope=all&client_id=MyClientId&client_secret=MyClientSecert", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.WriteLine(" ");
Console.WriteLine("#########################");
Console.WriteLine(" ");
rff = JsonSerializer.Deserialize<ResponceFromFedex>(response.Content);
string token = rff.access_token;
request = new RestRequest("/track/v1/trackingnumbers", Method.Post);
request.AddHeader("Authorization", "bearer " + token);
request.AddHeader("X-locale", "en_US");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", input, ParameterType.RequestBody);
response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.ReadLine();
I am very new to this API stuff and have no idea what I am doing, Any help would be greatly appreciated
I should add that I have been able to make the call using PostMan, I just don't know to to translate that to C#
I have tried tweaking setting and have not been able to get a proper response
To finaly get this to work I had to add a Default Query Parameter to the client
client.AddDefaultQueryParameter("access_token", token);
Here is a full look at the code
string input = "{\r\n \"trackingInfo\": [\r\n {\r\n \"trackingNumberInfo\": {\r\n \"trackingNumber\": \"794843185271\"\r\n }\r\n }\r\n ],\r\n \"includeDetailedScans\": true\r\n}";
var client = new RestClient("https://apis-sandbox.fedex.com");
var request = new RestRequest("/oauth/token",Method.Post);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&scope=all&client_id=MyClientId&client_secret=MyClientSecert", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.WriteLine(" ");
Console.WriteLine("#########################");
Console.WriteLine(" ");
rff = JsonSerializer.Deserialize<ResponceFromFedex>(response.Content);
string token = rff.access_token;
request = new RestRequest("/track/v1/trackingnumbers", Method.Post);
//Line of code that was added.
client.AddDefaultQueryParameter("access_token", token);
request.AddHeader("Authorization", "bearer " + token);
request.AddHeader("X-locale", "en_US");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", input, ParameterType.RequestBody);
response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.ReadLine();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.