Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VB.NET 401 Unauthorized error in PayPal OAuth API

I’m trying to get an access token back from PayPals OAuth API, I can get it back with curl but need to get the code through vb.NET. I always get a 401 unauthorized response but can’t see why. I’m using the following code:

Dim uri as new Uri("https://api-m.sandbox.paypal.com/v1/oauth2/token")
    Dim wClientID As String = "client ID"
    Dim wClientSecret As String = "Client Secret"
    Dim data as string = "grant_type=client_credentials"
    Dim dataByte as Byte()
    
    ServicePointManager.Expect100Continue = true
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    request = WebRequest.Create(uri)
    request.Headers.Add("Username", wClientID)
    request.Headers.Add("Password", wClientSecret)
    request.Headers.Add(HttpRequestHeader.Authorization, "Basic " & wClientID & ":" & wClientSecret)
    request.ContentType = "application/x-www-form-urlencoded"
    request.Method = "POST"
    
    dataByte = Encoding.UTF8.GetBytes(data)
    
    Using requestStream = request.GetRequestStream
        requestStream.Write(dataByte, 0, dataByte.Length)
        requestStream.Close()
    End Using

Any help would be appreciated, I’ve triple checked the client id and secret, both are definitely correct

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to Base64 encode the clientid:secret string for HTTP basic authentication. Curl does this for you.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading