带有 RestSharp 的 Paypal Rest Api 在 xamarin android 中不起作用
Posted
技术标签:
【中文标题】带有 RestSharp 的 Paypal Rest Api 在 xamarin android 中不起作用【英文标题】:Paypal Rest Api With RestSharp not working in xamarin android 【发布时间】:2017-01-26 19:52:21 【问题描述】:当我调用 Paypal Rest API 时,RestSharp 组件出现错误。
我有以下代码使用 Xamarin for android。
public async Task<PayPalGetTokenResponse> GetAccessToken()
var restRequest = new RestRequest("/oauth2/token", Method.POST);
// Add headers
restRequest.AddHeader("Accept", "application/json");
restRequest.AddHeader("Accept-Language", "en_US");
// Make Authorization header
restClient.Authenticator = new HttpBasicAuthenticator(Config.ApiClientId, Config.ApiSecret);
// add data to send
restRequest.AddParameter("grant_type", "client_credentials");
var response = restClient.Execute<PayPalGetTokenResponse>(restRequest);
response.Data.DisplayError = CheckResponseStatus(response, HttpStatusCode.OK);
return response.Data;
但报错:“Error: SecureChannelFailure (验证或解密失败。)”
我也使用了 ModernHttpClient 但遇到了同样的错误
public async Task<PayPalGetTokenResponse> GetAccessToken()
string clientId = Config.ApiClientId;
string secret = Config.ApiSecret;
string oAuthCredentials = Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + secret));
string uriString = Config.ApiUrl+"/oauth2/token";
PayPalGetTokenResponse result;
HttpClient client = new HttpClient(new NativeMessageHandler());
var h_request = new HttpRequestMessage(HttpMethod.Post, uriString);
h_request.Headers.Authorization = new AuthenticationHeaderValue("Basic", oAuthCredentials);
h_request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
h_request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en_US"));
h_request.Content = new StringContent("grant_type=client_credentials", UTF8Encoding.UTF8);
try
HttpResponseMessage response = await client.SendAsync(h_request);
//if call failed ErrorResponse created...simple class with response properties
if (!response.IsSuccessStatusCode)
var error = await response.Content.ReadAsStringAsync();
var errResp = JsonConvert.DeserializeObject<string>(error);
//throw new PayPalException error_name = errResp.name, details = errResp.details, message = errResp.message ;
var success = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<PayPalGetTokenResponse>(success);
catch (Exception)
throw new HttpRequestException("Request to PayPal Service failed.");
return result;
【问题讨论】:
RestSharp 使用的 HttpClient 换成 ModernHttpClient 了吗? 是的,但遇到同样的错误 你试过这里的例子吗:***.com/a/17234955/1398425 但是,这会抑制存在潜在风险的 SSL 问题。 感谢您的回复,我也提出了:ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;在主要活动中但遇到相同的错误 【参考方案1】:您是否尝试强制使用现代 SSL 协议?
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
这对我有用:
if (ServicePointManager.SecurityProtocol != SecurityProtocolType.Tls12)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
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);
【讨论】:
以上是关于带有 RestSharp 的 Paypal Rest Api 在 xamarin android 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章