VB.NET PayPal OAuth 中的 401 未经授权错误 API

VB.NET 401 Unauthorized error in PayPal OAuth API

我正在尝试从 PayPal OAuth API 取回访问令牌,我可以使用 curl 取回它,但需要通过 vb.NET 获取代码。我总是收到 401 未经授权的响应,但不明白为什么。我正在使用以下代码:

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

如有任何帮助,我们将不胜感激,我已经三次检查了客户端 ID 和密码,两者都绝对正确

您需要对 clientid:secret 字符串进行 Base64 编码以进行 HTTP 基本身份验证。 Curl 为您做这件事。