我正在尝试获取paypal的访问令牌。
我有下一个参数:EndPoint
,Client Id
,secret
,api username
,api signature
,api password
,application Id
我需要PayPal客户才能这样做吗?
我点了这个链接:https://developer.paypal.com/docs/integration/direct/make-your-first-call/
并尝试:
private string getAccessToken() {
var ppClient; // = new paypalClient(); // create a paypal client
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("Accept", "application/json");
parameters.Add("Accept-Language", "en_US");
parameters.Add("grant_type", "client_credentials");
var result = ppClient.Get("https://api.sandbox.paypal.com/v1/oauth2/token", parameters);
string accessToken = result["access_token"];
return accessToken;
}
谢谢大家!
答案 0 :(得分:1)
我建议使用RestSharp(只需抓住NuGet包): -
if (ServicePointManager.SecurityProtocol != SecurityProtocolType.Tls12) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // forced to modern day SSL protocols
var client = new RestClient(payPalUrl) { Encoding = Encoding.UTF8 };
var authRequest = new RestRequest("oauth2/token", Method.POST) {RequestFormat = DataFormat.Json};
client.Authenticator = new HttpBasicAuthenticator(clientId, secret);
authRequest.AddParameter("grant_type","client_credentials");
var authResponse = client.Execute(authRequest);
答案 1 :(得分:0)
有点迟到,但是遇到了类似的问题。最后,我不得不使用“基本”身份验证实现来从PayPal中获取详细信息。这是我的令牌收集器类。唯一真正需要做的就是用我的Paypal设置中生成的2个paypal client_Id和Secret字符串替换我的PayPal选项注入。
忽略我的AccessToken返回类型,它只是我编写的PoCo,而ReadToObject只是使用newtonsoft的简单反序列化器。 真正的好东西返回到“结果”字符串。
希望这会有所帮助!
public sealed class PaymentTokenServer : IPaymentTokenServer
{
private readonly List<KeyValuePair<string, string>> tokenServerPairs = new List<KeyValuePair<string, string>>();
private PayPalOptions payPalOptions;
public PaymentTokenServer(IOptions<PayPalOptions> paypalOptions)
{
this.payPalOptions = paypalOptions.Value;
this.tokenServerPairs.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
}
public AccessToken GetToken()
{
var content = new FormUrlEncodedContent(this.tokenServerPairs);
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(this.payPalOptions.TokenServerUrl);
client.DefaultRequestHeaders.AcceptLanguage.Add( new StringWithQualityHeaderValue("en_US"));
var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{this.payPalOptions.ClientId}:{this.payPalOptions.Secret}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64String);
var response = client.PostAsync("", content).Result;
var result = response.Content.ReadAsStringAsync().Result;
return result.ReadToObject<AccessToken>();
}
}
}