Consuming Paypal REST API from C# -


im trying simple test rest api based on curl example: https://developer.paypal.com/docs/integration/direct/make-your-first-call/

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \ -h "accept: application/json" \ -h "accept-language: en_us" \ -u "eoj2s-z6oon_le_ks1d75wsz6y0sfdvsy9183ivxfyzp:eclusmeuk8e9ihi7zdvlf5cz6y0sfdvsy9183ivxfyzp" \ -d "grant_type=client_credentials" 

my c#:

var h = new httpclienthandler(); h.credentials = new networkcredential("client_id", "secret"); var client = new httpclient();  client.baseaddress = new uri("https://api.sandbox.paypal.com/v1/oauth2/token"); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); client.defaultrequestheaders.add("accept-language", "en_us");  var requestcontent = new formurlencodedcontent(new[] {      new keyvaluepair<string, string>("grant_type", "client_credentials"), });  httprequestmessage req = new httprequestmessage(system.net.http.httpmethod.post, "https://api.sandbox.paypal.com/v1/oauth2/token");  req.content = new stringcontent("grant_type=client_credentials");  var r = client.sendasync(req).result; 

error returned:

statuscode: 415, reasonphrase: 'unsupported media type', version: 1.1, content: system.net.http.streamcontent, headers: { proxy_server_info: host=slcsbjava2.slc.paypal.com;threadid=13873 paypal-debug-id: 6bc39da1e021d server_info: identitysecuretokenserv:v1.oauth2.token&calthreadid=351&topleveltxnstarttime=14649fa185e&host=slcsbidensectoken501.slc.paypal.com&pid=25122 correlation-id: 6bc39da1e021d transfer-encoding: chunked date: thu, 29 may 2014 21:54:25 gmt server: apache-coyote/1.1 } 

what need change fix code?

i know question on year old, figured post found around topic since there isn't out there. after messing longer admit, got work. big difference between posted in question , in answer around authentication posting body (notice body, instead of using stringcontent, i'm using formurlencodedcontent - created in question, never used)

system.net.servicepointmanager.securityprotocol = system.net.securityprotocoltype.tls12;  using (httpclient client = new httpclient()) {     client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));     client.defaultrequestheaders.acceptlanguage.add(new stringwithqualityheadervalue("en_us"));      var clientid = "<client_id>";     var clientsecret = "<client_secret>";     var bytes = encoding.utf8.getbytes($"{clientid}:{clientsecret}");      client.defaultrequestheaders.authorization = new authenticationheadervalue("basic", convert.tobase64string(bytes));      var keyvalues = new list<keyvaluepair<string, string>>();     keyvalues.add(new keyvaluepair<string, string>("grant_type", "client_credentials"));     var responsemessage = await client.postasync("https://api.sandbox.paypal.com/v1/oauth2/token", new formurlencodedcontent(keyvalues));     viewmodel.responsemessage = await responsemessage.content.readasstringasync(); } 

after doing this, able json response in responsemessage , bearer token code.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -