I am trying to use RestSharp in a console application, to connect to an api and get a cookie, but in the response, i keep getting "Unable to connect to remote server".
var client = new RestClient("http://finans-dk.pronestor.com/Api.mvc/v1/Authenticate");
client.Authenticator = new SimpleAuthenticator("login", "????", "password", "????");
var request = new RestRequest("resource", Method.GET);
IRestResponse response = client.Execute(request);
I basically expect RestSharp to call the following:
finans-dk.pronestor.com/Api.mvc/v1/Authenticate?login=???&password=???
This works for me in postman, but not in restsharp.
I have tested with http://ip.jsontest.com, to test if i can connect to any outside apis, this works and get my ip back.
Any ideas???
Change your code to this:
var client = new RestClient("https://finans-dk.pronestor.com/Api.mvc/v1/");
client.Authenticator = new SimpleAuthenticator("login", "????", "password", "????");
var request = new RestRequest("Authenticate", Method.GET);
IRestResponse response = client.Execute(request);
You didn't look at the documentation you linked to properly. In its example the URL is:
http://example.com/resource?username=foo&password=bar
Pay attention to where resource appears. Your URL shows Authenticate in that place instead.
Related
I am trying to use Ocacle's Financial REST API and I'm having trouble making it work in C# in VS2019.
I can confirm the restful call works using Postman, so I know my credentials are fine but I must be missing something trying this with in code.
So URL is like so:
http://MYCLOUDDOMAIN/fscmRestApi/resources/11.13.18.05/ledgerBalances?finder=AccountBalanceFinder;accountCombination=3312-155100-0000-0000-0000-00000,accountingPeriod=Feb-20,currency=USD,ledgerSetName=Ledger,mode=Detail&fields=LedgerName,PeriodName,Currency,DetailAccountCombination,Scenario,BeginningBalance,PeriodActivity,EndingBalance,AmountType,CurrencyType,ErrorDetail
So I stick that in postman, put in my credentials (basic auth) and it works find. In VS I've tried both the RestSharp way and basic HTTPRequest way as follows:
HttpWebRequest r = (HttpWebRequest)WebRequest.Create("/fscmRestApi/resources/11.13.18.05/ledgerBalances?finder=AccountBalanceFinder;accountCombination=3312-155100-0000-0000-0000-00000,accountingPeriod=Feb-20,currency=USD,ledgerSetName=Ledger US,mode=Detail&fields=LedgerName,PeriodName,Currency,DetailAccountCombination,Scenario,BeginningBalance,PeriodActivity,EndingBalance,AmountType,CurrencyType,ErrorDetail");
r.Method = "GET";
string auth = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("Username" + ":" + "Password"));
r.Headers.Add("Authorization", "Basic" + " " + auth);
r.ContentType = "application/vnd.oracle.adf.resourcecollection+json";
using (HttpWebResponse resp = (HttpWebResponse)r.GetResponse())
{
int b = 0;
}
RestSharp:
var client = new RestClient("http://MYCLOUDDOMAIN/fscmRestApi/resources/11.13.18.05/ledgerBalances?finder=AccountBalanceFinder;accountCombination=3312-155100-0000-0000-0000-00000,accountingPeriod=Feb-20,currency=USD,ledgerSetName=Ledger US,mode=Detail&fields=LedgerName,PeriodName,Currency,DetailAccountCombination,Scenario,BeginningBalance,PeriodActivity,EndingBalance,AmountType,CurrencyType,ErrorDetail");
client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("UserName", "Password");
//Tried authorization this way as well.
//JObject AuthRequest = new JObject();
//AuthRequest.Add("Username", "UserName");
//AuthRequest.Add("Password", "Password");
var request = new RestRequest();
request.Method = Method.GET;
request.RequestFormat = DataFormat.Json;
//request.AddParameter("text/json", AuthRequest.ToString(), ParameterType.RequestBody);
request.AddHeader("Content-Type", "application/vnd.oracle.adf.resourcecollection+json");
request.AddHeader("REST-Framework-Version", "1");
var response = client.Get(request);
No matter what I try I am always 401 not authorized. I suspect its some kind of header thing? I can't see the raw request header in postman
I am new to REST. I am used to using WSDLs soap services.
Try this.
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential("username", "password")
};
using (var client = new HttpClient(handler))
{
var result = await client.GetAsync("url");
}
Good luck!
I figured out what the problem was.
In postman, it was fine with the URL I posted being HTTP but in C# code it was not. I switched the URL to HTTPS and it started working just fine.
I've set hallmonitor (OAuth 2.0 compliant service) in a Bronto sandbox, but using RestSharp I can't get the access token to be able to make further calls to the REST API.
I've been able to successfully use curl i.e.
curl -X POST -d "grant_type=client_credentials&client_id=CLIENTID&client_secret=CLIENTSECRET" https://auth.bronto.com/oauth2/token
I've tried a number of variations of the code below, but nothing seems to work, I always get an error response.
{
"error_description": "Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method).",
"error": "unauthorized_client"
}
Simplified sample code
var client = new RestClient("https://auth.bronto.com");
client.Authenticator = new HttpBasicAuthenticator(clientId, secret);
//client.Authenticator = new SimpleAuthenticator(CLIENT_ID, clientId, CLIENT_SECRET, secret);
RestRequest request = new RestRequest("/oauth2/token", Method.POST);
//request.AddHeader("Authorization", "Basic " + client);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter(GRANT_TYPE, CLIENT_CREDENTIALS);
//request.AddParameter(CLIENT_ID, clientId);
//request.AddParameter(CLIENT_SECRET, secret);
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
Has anyone used RestSharp with Bronto REST API to successfully authenticate and get the access token?
Any help is much appreciated.
I have a problem with RestSharp request - it is working when running in Test project but returning "Unable to connect to remote server" when running inside MVC.
var client = new RestClient("https://apiurl");
var request = new RestRequest("Authenticate", Method.POST);
request .AddHeader("Accept", "text/json");
request .AddHeader("Content-Type", "text/json");
var model = new AuthenticationRequest
{
Username = "username",
Password = "passowrd"
};
string body = JsonConvert.SerializeObject(model);
request.AddParameter("text/json", body, ParameterType.RequestBody);
var response = client.Execute(request);
Same code in test is working just fine. But inside MVC on localhost I get "Unable to connect to remote server". Anyone had similar issue?
Thanks in advance for the help!
This is the (modified) snippet that Postman gives for successful call to my page.
var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", "Basic anVyYTp3MmZacmo2eGtBOHJsRWrt");
IRestResponse response = client.Execute(request);
But when placed in my c# app it returns 403 forbidden, while Postman makes it and recieves 200.
The same thing happens when I use httpclient in my app (403).
Use RestClient.Authenticator instead:
var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me")
{
Authenticator = new HttpBasicAuthenticator("User", "Pass")
};
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Edit:
Since the issue (as mentioned in the comments) is the fact that RestSharp doesn't flow the authentication through redirects, I'd suggest going with a combination of HttpClient with a HttpClientHandler where you set the authentication to flow.
[Solution]
Use the below line for the header
Headers.Add("User-Agent: Other");
I want to create/modify an issue on redmine using the PUT/POST methods of restSharp.
I cannot find valuable information about xml PUT/POST using Rest sharp. I tried various methods from restsharp.org like Addbody("test", "subject"); , IRestResponse response = client.Execute(request); but there is no change in Redmine. What am I doing wrong?
POST gives a "Only get, put, and delete requests are allowed." message.
PUT gives a "Only get, post, and delete requests are allowed." message.
My Code
RestClient client = new RestClient(_baseUrl);
client.Authenticator = new HttpBasicAuthenticator(_user, _password);
RestRequest request = new RestRequest("issues/{id}.xml", Method.POST);
request.AddParameter("subject", "Testint POST");
request.AddUrlSegment("id", "5");
var response = client.Execute(request);
The problem was in the serialization. My Issue class contains object of various other classes which was causing a problem in the serialization.
This is how we did it:
RestRequest request = new RestRequest("issues/{id}.xml", Method.PUT);
request.AddParameter("id", ticket.id, ParameterType.UrlSegment);
request.XmlSerializer = new RedmineXmlSerializer();
request.AddBody(ticket);
RestClient client = new RestClient(_baseUrl);
client.Authenticator = new HttpBasicAuthenticator(_user, _password);
IRestResponse response = client.Execute(request);
Your code looks ok to me, I'm unsure if you need this but we added this header when using RestSharp for json against a WebAPI host:
request.AddHeader("Accept", "application/xml");