2016-09-19 18 views
4

PayPal Rest APIを呼び出すとRestSharpコンポーネントにエラーが発生しました。PayPal Rest API with RestSharp with xamarin android

私はAndroid用Xamarinを使用して次のコードを使用しています。

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; 
    } 
+0

あなたはModernHttpClientを使用するRestSharpによって使用されるのHttpClientを置き換えることがありますか? –

+0

はい、同じエラーが発生しました – Pratik

+0

ここで例を試したことがありますか?http://stackoverflow.com/a/17234955/1398425 –

答えて

0

同じエラーを持っているあなたは力を試してみました現代の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);