0

Im trying to do a simple test with the REST API based on their 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 do I need to change to fix my code?

2
  • 1
    StackOverflow is an English speaking community, please translate your question. Multi-language variants are currently not available. Commented May 29, 2014 at 22:02
  • This question appears to be off-topic because it is not written in English Commented May 29, 2014 at 22:03

2 Answers 2

9

I know that this question is now over a year old, but figured I would post what I found around this topic since there isn't much out there. After messing with this for much longer than I would like to admit, I finally got this to work. The big difference between what was posted in the question and what is in my answer is around the authentication as well as the posting to the body (notice for the body, instead of using StringContent, I'm using FormUrlEncodedContent - which was created in the question, but then 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, I was able to get back the json response in the responseMessage and then get the bearer token back for my code.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this from NuGet, you get classes for use all the functions in the rest api, official by paypal

https://www.nuget.org/packages/PayPal

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.