BartWD
BartWD

Reputation: 140

PayPal API - (401) Unauthorized when requesting Access Token

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!

Upvotes: 0

Views: 1319

Answers (1)

BartWD
BartWD

Reputation: 140

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.

Upvotes: 1

Related Questions