C# 带有RestSharp的Paypal Rest Api在xamarin android中不起作用

C# 带有RestSharp的Paypal Rest Api在xamarin android中不起作用,c#,paypal,xamarin.forms,httpclient,restsharp,C#,Paypal,Xamarin.forms,Httpclient,Restsharp,我在调用Paypal Rest API时遇到RestSharp组件错误 我有以下代码使用Xamarin for Android public async Task<PayPalGetTokenResponse> GetAccessToken() { var restRequest = new RestRequest("/oauth2/token", Method.POST); // Add headers restReq

我在调用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;
    }
公共异步任务GetAccessToken() { var restRequest=new restRequest(“/oauth2/token”,Method.POST); //添加标题 AddHeader(“接受”、“应用程序/json”); restRequest.AddHeader(“接受语言”、“en_US”); //生成授权标头 restClient.Authenticator=新的HttpBasicAuthenticator(Config.ApiClientId,Config.ApiSecret); //添加要发送的数据 restRequest.AddParameter(“授权类型”、“客户端凭据”); var response=restClient.Execute(restRequest); response.Data.DisplayError=CheckResponseStatus(响应,HttpStatusCode.OK); 返回响应数据; } 但出现错误:“错误: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;
    }
公共异步任务GetAccessToken() { 字符串clientId=Config.ApiClientId; string secret=Config.ApiSecret; string oAuthCredentials=Convert.ToBase64String(Encoding.Default.GetBytes(clientId+“:”+secret)); 字符串uriString=Config.apirl+“/oauth2/token”; PayPalGetTokenResponse结果; HttpClient=newHttpClient(new NativeMessageHandler()); var h_request=newhttprequestmessage(HttpMethod.Post,uriString); h_request.Headers.Authorization=新的AuthenticationHeaderValue(“基本”,oAuthCredentials); h_request.Headers.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”); h_request.Headers.AcceptLanguage.Add(新StringWithQualityHeaderValue(“en_-US”); h_request.Content=新的StringContent(“授权类型=客户端凭据”,UTF8Encoding.UTF8); 尝试 { HttpResponseMessage response=等待客户端.SendAsync(h_请求); //如果调用失败,则创建ErrorResponse…具有响应属性的简单类 如果(!response.issucessStatusCode) { var error=await response.Content.ReadAsStringAsync(); var errResp=JsonConvert.DeserializeObject(错误); //抛出新的PayPaleException{error_name=errResp.name,details=errResp.details,message=errResp.message}; } var success=wait response.Content.ReadAsStringAsync(); 结果=JsonConvert.DeserializeObject(成功); } 捕获(例外) { 抛出新的HttpRequestException(“对贝宝服务的请求失败”); } 返回结果; }
您是否尝试强制使用现代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使用的HttpClient以使用ModernHttpClient?是的,但出现了相同的错误您是否尝试了此处的示例:但是这会抑制潜在风险的SSL问题。感谢您的回复,我还输入了:ServicePointManager.ServerCertificateValidationCallback+=(发送方、证书、链、sslPolicyErrors)=>true;在主活动中,但得到相同的错误