【发布时间】: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_id 和 secret 在代码中被替换为真实值 :)
感谢您的帮助!
【问题讨论】:
标签: paypal httpwebrequest webrequest