0

I am trying to incorporate PayPal payments into our project, but I am failing at the moment hehe.

Basically, first step is to get the access token request and response, which I am trying to do with WebRequest, but it spits out 401 at me.

Following instructions from: https://developer.paypal.com/docs/integration/direct/make-your-first-call/

Here's the code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebRequest request = WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token");
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Credentials = new NetworkCredential("client_id", "secret");
request.PreAuthenticate = true;

string body = "grant_type=client_credentials";
byte[] buffer = Encoding.UTF8.GetBytes(body);
request.ContentLength = buffer.LongLength;

var reqStr = request.GetRequestStream();
reqStr.Write(buffer, 0, buffer.Length);
reqStr.Close();

WebResponse response = request.GetResponse();

Ofcourse, client_id and secret are replaced with real values in the code :)

Thank you for your help!

BartWD
  • 140
  • 1
  • 10

1 Answers1

1

Figured it out thanks to: C# HttpWebRequest using Basic authentication

Turns out I was not using Basic Auth as intended by PayPal. Oops :D

Hope someone finds this useful.

Community
  • 1
  • 1
BartWD
  • 140
  • 1
  • 10