Home > Back-end >  VB.NET 401 Unauthorized error in PayPal OAuth API
VB.NET 401 Unauthorized error in PayPal OAuth API

Time:11-26

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

CodePudding user response:

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

  • Related