I'm currently working on an ASP.Net MVC 4 application that uses PayPal to process credit card transactions. I've gotten most of everything setup with the PayPal RESTFul API and for some reason PayPal's latest version of their SDK just doesn't want to work (through NUGET) so I decided to make the api calls myself using ASP.Net's latest and greatest HttpClient()
(like it this way better anyways)
What I need to do is the following (copied from PayPal's documentation)
curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H "Content-Type:application/json" \
-H "Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG" \
-d '{
"intent": "sale",
"rest_of_the_data": "I didn't include it here"
}'
What I'm stuck on right now is the third parameter -H "Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG" \
here is what I have that doesn't work:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1} {2}", "Authorization", accessToken.token_type, accessToken.access_token))));
var response = client.PostAsync<Payment>(endPoint + "/v1/payments/payment", payment, new JsonMediaTypeFormatter());
if (response.Result.IsSuccessStatusCode)
{
var responseContent = response.Result.Content;
var responseString = responseContent.ReadAsAsync<Payment>().Result;
}
}
With the above code I'm getting the following response:
Result = {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Thu, 13 Jun 2013 07:06:50 GMT
Server: Apache-Coyote/1.1
Content-Length: 0
Content-Type: application/json
}}
Now I'm sure I am passing the right credentials but I believe that its not correctly formatted according to the -H "Authorization:Bearer...
I believe that I should not be using client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(...
but I can't seem to find a replacement to make this work. What should I be using?
Any help in this regard would be greatly appreciated.