Reputation: 87
I am replacing WebClient from RestSharper to call the Rest client. Below is the working code from WebClient but the same code is not working from RestClient
WebClient:
using (var client = new WebClient())
{
var reqparm =
new System.Collections.Specialized.NameValueCollection
{
{"client_id", _configurationTickle.ClientId},
{"client_secret", _configurationTickle.ClientSecret},
{"grant_type", "authorization_code"},
{"code", authCode},
{"redirect_uri", _configurationTickle.CallBackUrl}
};
byte[] responsebytes = client.UploadValues(url, "POST", reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
RestSharp:
var client1 = new RestClient(_configurationTickle.BaseUrl);
var request = new RestRequest(_configurationTickle.TokenRequestEndPoint,Method.POST);
request.AddParameter("client_id", _configurationTickle.ClientId);
request.AddParameter("client_secret", _configurationTickle.ClientSecret);
request.AddParameter("grant_type", "authorization_code");
request.AddParameter("code", authCode);
request.AddParameter("redirect_uri", _configurationTickle.CallBackUrl);
IRestResponse response = client1.Execute(request);
Can you please help whats wrong with the request in RestSharp, I am getting Invalid request error. Thanks in advance.
Upvotes: 0
Views: 669
Reputation: 2334
urlencoded
type request should be given as below and please check with the below modified code
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "Key1=value1&Key2=value2&....", ParameterType.RequestBody);
Modified Code:
var client1 = new RestClient(_configurationTickle.BaseUrl);
var request = new RestRequest(_configurationTickle.TokenRequestEndPoint,Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
String body="client_id="+_configurationTickle.ClientId+"&client_secret="+_configurationTickle.ClientSecret+"&grant_type=authorization_code&code="+authCode+"&redirect_uri="_configurationTickle.CallBackUrl;
request.AddParameter("application/x-www-form-urlencoded", body, ParameterType.RequestBody);
Upvotes: 1