c# - How to use Restsharp with a plain text body -


i trying fulfil ebay access token webrequest

the server expects

http method: post
url: https://api.sandbox.ebay.com/identity/v1/oauth2/token

http headers :

content-type = application/x-www-form-urlencoded

authorization = basic

request body (wrapped readability):

grant_type=client_credentials& redirect_uri=<runame-value>& scope=https://api.ebay.com/oauth/api_sc 

this working using webrequest shown

var credentials = "basic " + convert.tobase64string(encoding.utf8.getbytes(clientid + ":" + clientsecret));  var request = webrequest.create("https://api.sandbox.ebay.com/identity/v1/oauth2/token") httpwebrequest;  request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.headers.add(httprequestheader.authorization, credentials);  var formdata = encoding.utf8.getbytes("grant_type=client_credentials&redirect_uri=" + runame + "&scope=https://api.ebay.com/oauth/api_scope"); request.contentlength = formdata.length;   using (var post = request.getrequeststream()) {    post.write(formdata, 0, formdata.length); }  using (var response = request.getresponse() httpwebresponse) {    streamreader reader = new streamreader(response.getresponsestream());    string result = reader.readtoend(); } 

however following restsharp request returning error

invalid request

var resource = "identity/v1/oauth2/token"; var request = new restrequest(resource, method.post); var credentials = convert.tobase64string(encoding.utf8.getbytes(clientid + ":" + clientsecret)); request.addparameter("authorization", $"basic {credentials}", parametertype.httpheader); request.addheader("content-type", "application/x-www-form-urlencoded"); var body = $"grant_type=client_credentials&redirect_uri={runame}&scope=https://api.ebay.com/oauth/api_scope"; request.addparameter("text/xml", body, parametertype.requestbody); var client = new restclient("https://api.sandbox.ebay.com");  return client.execute<t>(request); 

the following worked me, if can better explain going on ill mark answer correct

var resource = "identity/v1/oauth2/token"; var request = new restrequest(resource, method.post); var credentials = convert.tobase64string(encoding.utf8.getbytes(clientid + ":" + clientsecret)); request.addparameter("authorization", $"basic {credentials}", parametertype.httpheader); request.addheader("content-type", "application/x-www-form-urlencoded"); var body = $"grant_type=client_credentials&redirect_uri={runame}&scope=https://api.ebay.com/oauth/api_scope"; var formdata = encoding.utf8.getbytes(body); request.addparameter("application/x-www-form-urlencoded", formdata, parametertype.requestbody); var client = new restclient("https://api.sandbox.ebay.com");  return getresult<t>(client, request); 

note lines

var body = $"grant_type=client_credentials&redirect_uri={runame}&scope=https://api.ebay.com/oauth/api_scope"; var formdata = encoding.utf8.getbytes(body); request.addparameter("application/x-www-form-urlencoded", formdata, parametertype.requestbody); 

Comments