加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:02:03 所属栏目:百科 来源:网络整理
导读:当我打电话给Paypal Rest API时,我的RestSharp组件出错了. 我有以下代码使用Xamarin for Android. public async TaskPayPalGetTokenResponse GetAccessToken() { var restRequest = new RestRequest("/oauth2/token",Method.POST); // Add headers restReque
当我打电话给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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读