微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Paypal Rest Api RestSharp在xamarin android中无效

当我打电话给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;
    }

但得到错误:“错误: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;
    }

解决方法

您是否试图强制使用现代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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐