Authentication Error while trying to get the DHL Interface to run - c#

I've been trying to get the DHL interface up and running for some time now.
Unfortunately, there is already a lack of authentication.
Request:
client_id == AppID
client_secret = AppToken
var client = new RestClient("https://api.dhlecs.com/auth/v4/accesstoken");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", client_id);
request.AddParameter("client_secret", client_secret);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Response:
{"type":"https://api.dhlecs.com/docs/errors/401.0000007", "title":"Invalid
credentials"}
Does anyone have any idea why it doesn't work? Or am I calling something wrong?

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

Get OAuth2.0 Bearer Token from AuthURL and ClientID

In Postman I am able to generate the Bearer token using
Callback URL
Auth URL
Client ID
How do I do the same using C# code?
This is the code I am running but the response body does not have the bearer token.
var client = new RestClient("AuthURL");
var requestToken = new RestRequest(Method.POST);
requestToken.AddHeader("cache-control", "no-cache");
requestToken.AddHeader("content-type", "application/x-www-form-urlencoded");
requestToken.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=123", ParameterType.RequestBody);
IRestResponse response = client.Execute(requestToken);
string res = response.Content;
where is your username and password?
var client = new RestClient("http://example.com");
client.Authenticator = new HttpBasicAuthenticator(userName, password);
var request = new RestRequest("resource", Method.GET);
client.Execute(request);
might be a duplicate of this question
RestSharp HttpBasicAuthentication - example

RestSharp body always empty

I need to call a Post service with RestSharp
var client = new RestClient(url);
var request = new RestRequest(url,Method.POST);
request.AddHeader("Content-type", "application/json");
request.AddJsonBody(new { grant_type = "client_credentials", client_id = clientIdParam, client_secret = appReg_clientSecret, ressource = _ressource }
);
var response = client.Post(request);
The problem is that the request's body is always null and the json body is added as a parameter
Any ideas?
https://login.microsoftonline.com/{{tenantId}}/oauth2/token
seems to not accept json, even with the content-type set as "application/json"
My solution:
var client = new RestClient("https://login.microsoftonline.com");
var request = new RestRequest("/{{tenantId}}/oauth2/token", Method.POST);
var requestBody = $"grant_type=client_credentials&client_id={clientIdParam}&client_secret={appReg_clientSecret}&resource={_resource}";
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", requestBody, ParameterType.RequestBody);
var response = client.Execute(request);

C# RESTSharp client connect Drupal Rest_server Access denied for user anonymous

I was search this article Consuming Drupal RestApi with c#
And http://tylerfrankenstein.com/code/drupal-services-csrf-token-firefox-poster
I has question in the cookie and token. I have test in poster with firefox and successful on post the created article .Tamper Data has the request header.
tamper data
nid: "129342"
uri: http://www.tsghy.com.cn/services/node/129342
Postman created the post code
var client = new RestClient("http://www.tsghy.com.cn/services/node");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "5c28c9d6-d640-a4f0-a549-b6018e62907d");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-csrf-token", "s0Z17LT7neX_K6grHgoJCUPR6VcL2QxRlNLmbRWeExE");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "type=article&title=test%201", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
I will crazy,
And replace to the C# ,i has Access denied for user anonymous.this my code with below :
First I login the Drupal with Rest
private login_user2 loginAsync2(string username, string password)
{
try
{
RestClient client = new RestClient(base_url2);
var request = new RestRequest("user/login.json", Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
client.Authenticator = new SimpleAuthenticator("username",username,"password",password);
var restResponse = client.Execute(request);
var content = restResponse.Content;
if (restResponse.StatusCode==System.Net.HttpStatusCode.OK)
{
login_user2 loginuser = JsonConvert.DeserializeObject<login_user2>(content.ToString());
request = new RestRequest("session/token", Method.GET);
restResponse = client.Execute(request);
loginuser.session_token = restResponse.Content.ToString();
return loginuser;
}
else {
return null;
}
}
catch (Exception ex) { throw ex; }
}
I have question at the login/user->token and session/token ,which the difference?
Second,Post the create data:
RestClient client = new RestClient(base_url2);
var request = new RestRequest("node", Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json; charset=UTF-8");
request.AddHeader("Accept", "application/json");
request.AddHeader("cookie", "Drupal.toolbar.collapsed=0; "+current_user2.session_name+"="+current_user2.sessid+"; has_js=1");
request.AddHeader("x-csrf-token",current_user2.session_token);
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
request.AddParameter("application/json", myjobject, ParameterType.RequestBody);
var queryresult = client.Execute(request);
yes,I found the key,restful has bug at the request.addheader(cookie,cookie);
change to :request.AddParameter(session_name,session_id,parametertype.cookie);

Converting HttpClient to RestSharp

I have Httpclient functions that I am trying to convert to RestSharp but I am facing a problem I can't solve with using google.
client.BaseAddress = new Uri("http://place.holder.nl/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",access_token);
HttpResponseMessage response = await client.GetAsync("api/personeel/myID");
string resultJson = response.Content.ReadAsStringAsync().Result;
This Code is in my HttpClient code, which works good, but I can't get it to work in RestSharp, I always get Unauthorized when using RestSharp like this:
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest();
client.Authenticator = new HttpBasicAuthenticator("Bearer", access_token);
request.AddHeader("Accept", "application/json");
request.Resource = "api/personeel/myID";
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
Am I missing something with authenticating?
This has fixed my problem:
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest("api/personeel/myID", Method.GET);
request.AddParameter("Authorization",
string.Format("Bearer " + access_token),
ParameterType.HttpHeader);
var response = client.Execute(request);
Upon sniffing with Fiddler, i came to the conclusion that RestSharp sends the access_token as Basic, so with a plain Parameter instead of a HttpBasicAuthenticator i could force the token with a Bearer prefix
Try this
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest("api/personeel/myID",Method.Get);
request.AddParameter("Authorization",$"Bearer {access_token}",ParameterType.HttpHeader);
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
If anyone happens on this, it looks like as of V 106.6.10 you can simply add default parameters to the client to save yourself from having to add your Auth token to every request method:
private void InitializeClient()
{
_client = new RestClient(BASE_URL);
_client.DefaultParameters.Add(new Parameter("Authorization",
string.Format("Bearer " + TOKEN),
ParameterType.HttpHeader));
}

Categories