【问题标题】:PayPal API - (401) Unauthorized when requesting Access TokenPayPal API - (401) 请求访问令牌时未经授权
【发布时间】:2017-07-06 08:36:49
【问题描述】:

我正在尝试将 PayPal 付款合并到我们的项目中,但目前我失败了,呵呵。

基本上,第一步是获取访问令牌请求和响应,我正在尝试使用 WebRequest 执行此操作,但它向我吐出 401。

遵循来自:https://developer.paypal.com/docs/integration/direct/make-your-first-call/的说明

代码如下:

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();

当然,client_idsecret 在代码中被替换为真实值 :)

感谢您的帮助!

【问题讨论】:

    标签: paypal httpwebrequest webrequest


    【解决方案1】:

    感谢:C# HttpWebRequest using Basic authentication

    原来我没有按照 PayPal 的意图使用基本身份验证。 哎呀:D

    希望有人觉得这很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-16
      • 2017-06-11
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多