【发布时间】:2018-08-07 12:15:21
【问题描述】:
我在网上找到的用于在 C# 中实现 PayPal REST API 的文档非常少。
我已经完成了获取访问令牌的第一步,但我一直看到发送 API 调用的方法相互冲突,而且我尝试过的方法都没有。
这是我当前的代码:
private async void cmdRefund_Click(object sender, EventArgs e)
{
//var apiContext = Configuration.GetAPIContext();
string AccessToken;
string Nonce;
string AppID;
string TokenType;
try
{
// ClientId of your Paypal app API
//This is the live ID
string APIClientId = "AZxxxx-8";
//this is the live secret Key
string APISecret = "Exxxx39";
using (var client = new HttpClient())
{
var byteArray = Encoding.UTF8.GetBytes(APIClientId + ":" + APISecret);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
//this is the live url
var url = new Uri("https://api.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 resp = await webresponse.Content.ReadAsStringAsync();
MessageBox.Show(resp);
if (resp.IndexOf("access_token") == -1)
{
MessageBox.Show("PayPal Authorization Failed.");
return;
}
AccessToken = resp.Substring(resp.IndexOf("access_token") + 15);
AccessToken = AccessToken.Substring(0, AccessToken.IndexOf("\""));
Nonce = resp.Substring(resp.IndexOf("nonce") + 8);
Nonce = Nonce.Substring(0, Nonce.IndexOf("\""));
AppID = resp.Substring(resp.IndexOf("app_id") + 9);
AppID = AppID.Substring(0, AppID.IndexOf("\""));
TokenType = resp.Substring(resp.IndexOf("token_type") + 13);
TokenType = TokenType.Substring(0, TokenType.IndexOf("\""));
MessageBox.Show("Access Token: \r\n" + AccessToken + "\r\nNonce:\r\n" + Nonce + "\r\nAppID:\r\n" + AppID + "\r\nToken Type:\r\n" + TokenType);
// response will deserialized using Jsonconver
//return JsonConvert.DeserializeObject<PayPalGetTokenResponse>(resp);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
//Authorization has been achieved here - now I want to process a refund
var apiContext = new APIContext(AccessToken);
Amount refundAmount = new Amount();
refundAmount.total = "0.01";
refundAmount.currency = "USD";
Refund refund = new Refund();
refund.amount = refundAmount;
string saleId = "9XY489008U7836633";
//*************THIS NEXT LINE CAUSES AN ERROR
Refund refundforreal = Sale.Refund(apiContext, saleId, refund);
//*************
string refundId = refund.id;
}
最后一行导致错误:“PayPal.IdentityException: '远程服务器返回错误:(401) Unauthorized.'”
据我所知,我的访问令牌是完全有效的,但这不起作用。我应该注意,我试图获取信息和退款的交易不是通过 REST API 进行的,而是通过我们网站上集成的常规 PayPal 界面进行的。我不知道这是否会导致问题,但这就是我需要做的。
我在 Visual Studios 2017 中使用用 C# 编写的 Windows 窗体应用程序,因为我正在替换一个旧的 VB6 程序,该程序需要用户在程序的浏览器中登录到 PayPal 会话,并且需要用某些东西替换该程序这对我们的员工来说既可用又熟悉,并且通过使用 REST API 而不是在 WebBrowser 控件中填写字段的旧方法更具前瞻性。
****************编辑************ - 我添加了这个作为后续:
我接受了@shamanthGowdraShankaramurthy 的建议并使用 Postman 并设法做我想做的事,所以谢谢你 - 这确实帮助我知道至少我想做的事情是可能的。
我仍然不知道如何在 C# 中进行 POST。我想也许我会远离 SDK 中内置的“退款”对象,而是尝试以其他方式发布。
我在 Postman 中使用的网址是:https://api.paypal.com/v1/payments/sale/9XY489008U7836633/refund
我将此作为正文发送以对我的测试交易进行 0.01 美元的退款: { “数量”: { “总计”:“0.01”, “货币:美元” }, "invoice_number": "INV-1234567" }'
我使用工作代码中的访问令牌向 POST 添加了不记名令牌授权。
最后,在 Postman 中,我将正文从“Text”更改为“JSON (application/json)。
如何在 C# winforms 应用程序中将所有这些元素(URL、我的不记名令牌、正文和正文为 json 的信息)合并到 POST 中?
【问题讨论】:
-
您是否尝试过以与程序无关的方式发出请求 - 例如使用 Postman 或高级休息客户端。这将有助于排除与实际请求或 api 令牌或类似事物有关的任何问题。首先尝试一个简单的请求。一旦成功,您就可以根据实际要求工作。我没有方便的令牌来尝试专门帮助您。
-
谢谢@shamanthGowdraShankaramurthy,采纳了你的建议并设法让它在 Postman 中工作 - 你能看看我在上面所做的编辑并引导我朝着正确的方向去做 POST ?我会使用 HttpClient 吗?
标签: c# winforms paypal paypal-rest-sdk