Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal API, HttpWebRequest throws SSL WebException

I'm trying to get an PayPal access token as shown here: https://developer.paypal.com/docs/integration/direct/make-your-first-call/#get-an-access-token

My C# Code looks like this:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token");
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret));
request.Accept = "application/json";
request.Headers.Add("Accept-Language", "en_US");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;

byte[] postBytes = Encoding.ASCII.GetBytes("grant_type=client_credentials");
request.ContentLength = postBytes.Length;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

Stream postStream = request.GetRequestStream(); // ###### Here I get the Exception! ######
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

When calling request.GetRequestStream(), I get a WebException with the german text "Die Anfrage wurde abgebrochen: Es konnte kein geschützter SSL/TLS-Kanal erstellt werden." which means that it was unable to create a secure SSL/TLS channel.

What do I do wrong? Can anybody help me?

Thanks, Volker

like image 317
Volker Avatar asked Jan 19 '17 18:01

Volker


1 Answers

I found the solution:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //SecurityProtocolType.Tls12;

Maybe it helps somebody else, too...

like image 110
Volker Avatar answered Sep 28 '22 11:09

Volker