Getting a NOT.AUTHORIZED.ERROR when trying to call fedex api tracking - c#

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();

Related

Is there a way to convert this Twilio CURL to RestClient?

I would like to know how to convert this Twilio CURL code to RestClient
I am stuck in the request.AddParamenter() I have not idea how to format it order to pass the Twilio SID,Token, From, To and Body Text Message.
curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json \
-data-urlencode "Body=Hi there" \
-data-urlencode "From=+15017122661" \
-data-urlencode "To=+15558675310" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
To this:
var client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("cache-control", "no-cache");
request.AddParameter("application/x-www-form-urlencoded", "bodykey=bodyval", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
This is my code now that compile 100% and after I run the code I get a response "Complete" no error message and no entry at twilio dashboard either, it does not send the text message, any idea your help will be much appreciate.
RestClient client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/ACet53f18a4734c339488c1845e619dd9g/Messages.json");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("ACet53f18a4734c339488c1845e619dd9g:daskshdsjkahkashd90ud09as8dasjkhdsa9"));
request.AddHeader("Authorization", "Basic " + base64authorization);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("Body", "Hello World");
request.AddParameter("From", "+15017122661"); //- My Twilio number goes
request.AddParameter("To", "+15017122661");
IRestResponse response = client.Execute(request);
string ResStatus = response.ResponseStatus.ToString(); //- Complete after successfully run.
You can send a RestClient request like this.
RestClient client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"));
request.AddHeader("Authorization", $"Basic {base64authorization}");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("Body", "Hi");
request.AddParameter("From", "+15017122661");
request.AddParameter("To", "+ 15558675310");
IRestResponse response = client.Execute(request);

Post Http call working on postman but code not working in C#

I have an issue with postman and c# code. I have two post calls to an API that must in the end make a callback to another API (webhook).
I tried to launch the two calls through Postman and i do obtain correctly the callback response. My issue is that when I use this code I do not have any callback response but I obtain 200 message from the server I call. I have the same issue with all implementations of the http post calls I sent and get the same issue.
public static void Main(string[] args)
{
// first call
var client = new RestClient("http://xxxxxxx/emails/attachments/");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization",
"Bearer theToken");
request.AddFile("attachment",
"C:/Users/..../pdf.pdf");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var attachmentId = response.Content;
// second call
client = new RestClient("http://xxxxxxx/emails/");
client.Timeout = -1;
request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization",
"Bearer theToken");
request.AddParameter("application/json",
"{\r\n \"subject\": \"email with attachment\",\r\n \"body\": \"Hi !!!l\\r\\n\",\r\n \"received_on\": \"2021-05-24T14:07:01.5874416+02:00\",\r\n \"author\": {\r\n \"name\": \"dvera#mymail.fr\",\r\n \"smtp_address\": \"\"\r\n },\r\n \"sender\": {\r\n \"name\": \"dvera#mymail.fr\",\r\n \"smtp_address\": \"\"\r\n },\r\n \"to\": [\r\n {\r\n \"name\": \"dvera#mymail.fr\",\r\n \"smtp_address\": \"\"\r\n }\r\n ],\r\n \"cc\": [\r\n {\r\n \"name\": \"\",\r\n \"smtp_address\": \"\"\r\n }\r\n ],\r\n \"attachments\": [\r\n " +
attachmentId + "\r\n ]\r\n}\r\n", ParameterType.RequestBody);
response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
Any idea about what's going wrong ? the weird thing is that I have a 200 response for each calls i make.
I solved my issue with this code:
using (var client = new HttpClient())
{
using (var form = new MultipartFormDataContent())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var file = File.OpenRead(attachmentPath);
byte[] fileBytes = new byte[file.Length];
form.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Length), "attachment", Path.GetFileName(attachmentPath));
HttpResponseMessage response = await client.PostAsync(url, form);
response.EnsureSuccessStatusCode();
var output = await response.Content.ReadAsStringAsync();
PostEmailAttachmentResponse returnValue = new PostEmailAttachmentResponse();
returnValue.Id = Int32.Parse(output);
return returnValue;
}
}
My previous code wasn't returning error message when i sent attachment. there was issue on the server side which was not returning error.

RestSharp Post Request Array Object

I've been looking for a way to use RestSharp C# to send POST request with the JSON below.
[{"Guid": "ccda117f-a9b5-4f54-ab4a-07a9cf403ef7"}]
It starts with the bracket and I don't see any reference similar to this.
Can you provide any link or sample code that I can use for reference?
Thanks!
var client2 = new RestClient("https://test.rest.com/api/v2/notifications/channels/" + channelId + "/subscriptions");
var request2 = new RestRequest(Method.POST);
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("authorization", "Bearer " + access_token);
request2.AddHeader("Content-Type", "application/json");
var myJson = "[{\"Guid\": \"ccda117f-a9b5-4f54-ab4a-07a9cf403ef7\"}]";
request2.AddParameter("application/json", myJson, ParameterType.RequestBody);
IRestResponse response2 = client2.Execute(request2);

Change transition in jira using c#

I'm trying to change status in jira using c# by put method.
in the rest API documentation I've saw that i need to connect like this:
/rest/api/3/workflow/transitions/{transitionId}/properties'
but i i'm not sure of what parameters to change in the line.
how do i find the transition id..
or should i change the word properties with some property..
i'm a fresh student so be Gentle with me.
I have searched all over the web and also tried to ask one of my teachers and a fellow programmer.
this is the code i have written/copied from the web:
Jira jira = Jira.CreateRestClient("http://localhost:8080/", "username", "password);
HttpWebRequest request = WebRequest.Create("http://localhost:8080/") as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
var projectKey = "CLEARN";
string base64Credentials = GetEncodedCredentials();
var client = new RestClient("http://localhost:8080/rest/api/3/workflow/transitions/{transitionId}/properties' ");
var request2 = new RestRequest(Method.PUT);
request2.AddHeader("postman-token", "BL0C-B43C-1ZWZ-NQUW_68b5a15fbd28c0a42df2cb47e8f7831155704df9");
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("Authorization", "Basic " + base64Credentials);
request2.AddHeader("content-type", "application/json");
request2.AddParameter("application/json", "\r\n{\r\n \"update\": {},\r\n \"transition\": {\r\n \"id\": \"10000\"\r\n },\r\n \"fields\": {\r\n \"resolution\": {\r\n \"name\": \"Done\"\r\n }\r\n }\r\n}", ParameterType.RequestBody);
IRestResponse response2 = client.Execute(request2);
for now i get forbidden status code when i'm trying to change values in the url.
this the correct code that made the correct change:
"
string base64Credentials = GetEncodedCredentials();
var client = new RestClient("http://localhost:8080/rest/api/2/issue/clearn-1/transitions/");
var request2 = new RestRequest(Method.POST);
request2.AddHeader("postman-token", "2f8d5f8e-c7a2-4dd6-acc3-2f992f244455");
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("Authorization", "Basic " + base64Credentials);
request2.AddHeader("content-type", "application/json");
request2.AddParameter("application/json", "\r\n{\r\n \"update\": {},\r\n \"transition\": {\r\n \"id\": \"41\"\r\n }}", ParameterType.RequestBody);
"

Converting restsharp to httpclient

I need to convert this restsharp code to c# HttpClient code.
How would I go about that?
var client = new RestClient("http://localhost:61375/token");
var request = new RestRequest(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=password&username=UserName&password=pa%24%24w0rd",
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Categories