![](/img/trans.png)
[英]Where does authorization code go in C# PayPal REST API Winforms app?
[英]How do you send a PayPal REST API Refund in a C# Winforms app?
我在网上找到很少用于在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)未经授权。'”
据我所知,我的访问令牌完全有效,但这不起作用。 我应该注意,我正在尝试获取信息和退款的交易不是通过REST API进行的,而是通过我们网站上集成的常规PayPal界面进行的。 我不知道这是否会导致问题,但这就是我需要做的事情。
我正在使用在Visual Studio 2017中用C#编写的Windows窗体应用程序,因为我正在替换一个旧的VB6程序,该程序要求用户在程序中的浏览器中登录到PayPal会话,并且需要用两者同时替换该程序我们的员工可以使用和熟悉,并且通过使用REST API而不是填充WebBrowser控件中的字段的旧方法更具前瞻性。
***********编辑************ - 我添加了这个作为后续跟进:
我接受了@ shamanthGowdraShankaramurthy的建议并使用了Postman并设法做了我想做的事,所以谢谢 - 这确实帮助我知道至少我想做的事情是可能的。
我仍然不知道如何在C#中进行POST。 我想也许我会远离SDK中内置的“Refund”对象,而是尝试以其他方式POST。
我在Postman使用的网址是: https : //api.paypal.com/v1/payments/sale/9XY489008U7836633/refund
我发送此身作为我的测试交易的0.01美元退款:{“金额”:{“总计”:“0.01”,“货币”:“美元”},“invoice_number”:“INV-1234567”}'
我使用我的工作代码中的访问令牌向POST添加了一个Bearer Token授权。
最后,在Postman中,我将正文从“Text”改为“JSON(application / json)”。
如何将所有这些元素(URL,我的持票人令牌,正文和身体json的信息)合并到C#winforms应用程序的POST中?
如果其他人正在寻找这个,答案结果比我想象的要简单得多。 我认为我没有使用PayPal SDK轻松获得的AccessTokens,从而搞砸了自己。
首先,确保正确设置了App.Config文件:
<!-- PayPal SDK settings -->
<paypal>
<settings>
//specify sandbox or live
<add name="mode" value="sandbox" />
<add name="clientId" value="insert_clientid_key_here" />
<add name="clientSecret" value="Insert_client_secret_key_here" />
</settings>
</paypal>
那么这就是我退款所需的所有代码:
private void cmdRefund_Click(object sender, EventArgs e)
{
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
var apiContext = new APIContext(accessToken);
Amount refundAmount = new Amount();
refundAmount.total = "0.01";
refundAmount.currency = "USD";
RefundRequest refund = new RefundRefundRequest();
refund.amount = refundAmount;
string saleId = "9XY489008U7836633";
Refund refundforreal = Sale.Refund(apiContext, saleId, refund);
MessageBox.Show("Refund status:" + refundforreal.state + "\r\n" + "Txn #:" + refundforreal.id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.