【发布时间】:2026-01-28 10:05:02
【问题描述】:
我正在尝试访问 PayPal 的 API 以通过 C#.NET Winforms 应用程序向客户提交发票,但我非常困惑。另一位用户发布此代码作为连接解决方案:
public class PayPalClient
{
public async Task RequestPayPalToken()
{
// Discussion about SSL secure channel
// http://*.com/questions/32994464/could-not-create-ssl-tls-secure-channel-despite-setting-servercertificatevalida
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
try
{
// ClientId of your Paypal app API
string APIClientId = "**_[your_API_Client_Id]_**";
// secret key of you Paypal app API
string APISecret = "**_[your_API_secret]_**";
using (var client = new System.Net.Http.HttpClient())
{
var byteArray = Encoding.UTF8.GetBytes(APIClientId + ":" + APISecret);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var url = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token", UriKind.Absolute);
client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
var requestParams = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials")
};
var content = new FormUrlEncodedContent(requestParams);
var webresponse = await client.PostAsync(url, content);
var jsonString = await webresponse.Content.ReadAsStringAsync();
// response will deserialized using Jsonconver
var payPalTokenModel = JsonConvert.DeserializeObject<PayPalTokenModel>(jsonString);
}
}
catch (System.Exception ex)
{
//TODO: Log connection error
}
}
}
public class PayPalTokenModel
{
public string scope { get; set; }
public string nonce { get; set; }
public string access_token { get; set; }
public string token_type { get; set; }
public string app_id { get; set; }
public int expires_in { get; set; }
}
恐怕这至少比我领先一步,因为我不知道在我的项目中哪里适合粘贴代码。假设您创建了一个全新的 C# Winforms 应用程序。在不涉及创建发票等细节的情况下。我需要哪些代码来支持 PayPal API 以及它在项目中的位置?我知道我需要从 PayPal 获得应用程序的授权,但我无法找到适用于 C# 和 PayPal 的良好“入门”指南。我在 PayPal 上创建了一个 REST API 应用程序,所以我确实有一个客户端 ID 和“秘密”来传递 Oauth 授权 - 我只是找不到这样做的地方。
提前致谢。我有一些 C#.net 编程经验,但老实说,我的编程经验主要可以追溯到 VB6,所以我需要大图的解释。感谢您的耐心等待!
【问题讨论】: