Re: How to connect Visa API with two ways verification C#

Solved! Go to solution
Rossman
Regular Visitor

How to connect Visa API with two ways verification C#

Im adding CertificateFromFileProvider to add certificate and key, adding Base autorization token, but still got 400 error in C# when trying to get Visa Promotions. Works fine on Postman. Did someone have a worked code sample to connect with Visa API by C# with certificates?
1 REPLY 1
API_Products
Visa Developer Support Specialist

Re: How to connect Visa API with two ways verification C#

Hey @Rossman,

 

I'm happy to help resolve the issue. My apologies for the delayed response. Can you please let me know which API you are trying to call? Can you share your request header and snapshots of the error? 
 
Please try to resolve the issue by creating new .p12 using this command line
 
command line:
openssl pkcs12 -export -out p12certfile.p12 -inkey example-key.pem -in cert.pem

 

Create the certificate file ".p12" file using the openssl command I suggested.

 

Please try to test again and start by calling helloworld API from sandbox environment. "vdp/helloworld".
 
To resolve the issue, instead of using certificate file (.p12) in the code, install the certificate on the machine and let the Framework code load the certificate from certificate store and use it for API call.
 
With the above approach you'll be able to call the APIs successfully. Following is the sample to the final code that is working.

 

 

public string RequestHelloWorld()
        {
            string requestURL = "vdp/helloworld";

string userId = ConfigurationManager.AppSettings["userId"];
            string password = ConfigurationManager.AppSettings["password"];
            string apiResponse = "";

try
            {
                return PerformDualAuthCall(requestURL, HttpMethod.Get, null, null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return apiResponse;
        }

public string PerformDualAuthCall(string requestPath, HttpMethod method, object requestBody, Dictionary headers = null)
        {
            string requestURL = ConfigurationManager.AppSettings["visaUrl"] + requestPath;
            string userId = ConfigurationManager.AppSettings["userId"];
            string password = ConfigurationManager.AppSettings["password"];
            string apiResponse = "";

try
            {
                if (headers == null)
                {
                    headers = new Dictionary();
                }

headers.Add(HttpRequestHeader.Authorization.ToString(), GetBasicAuthHeader(userId, password));

var httpClientHandler = new HttpClientHandler();
                httpClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
                using (HttpClient httpClient = new HttpClient(httpClientHandler))
                {

var request = new HttpRequestMessage
                    {
                        Method = method,
                        RequestUri = new Uri(requestURL)
                    };

if (headers != null)
                    {
                        foreach (var item in headers)
                        {
                            request.Headers.Add(item.Key, item.Value);
                        }
                    }

if (requestBody != null)
                    {
                        request.Content = new StringContent(requestBody is string ? requestBody as string : JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
                    };

// Make the call
                    var response = httpClient.SendAsync(request).Result;

if (response.StatusCode == HttpStatusCode.OK)
                    {
                        apiResponse = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine(e.Message);
            }
            return apiResponse;
        }

 

 

 

To learn more about common Visa Developer error codes and how to resolve them please go to our Visa Developer Error Codes page here - https://developer.visa.com/pages/visa-developer-error-codes 

 

If there's an error code that you're looking for that's not listed here, please refer to the API-specific error codes in its respective documentation pages.

Payload and URI Error Codes

HTTP STATUS HTTP CODE CAUSE/RESOLUTION

BAD REQUEST

400

This error could be due to a variety of reasons.

Check for the following:

  • The url has a space after the ?.
  • Whitespace issues, in general, anywhere in the url.
  • Ideally the following fields need to be checked for correctness:
    •  URL
    •  Query params

Or

  • The API endpoint you are trying to use only supports Two-Way SSL authentication. Please ensure you are not using any other token type.

Or

  • Invalid input found in the request payload.

 

If the issue persists, please provide the following information: 

1. End Point
2. Request Header
3. Request Body
4. Response Header (include the x-correlation-id)
5. Response Body

Using SoapUI, you can find the x-correlation-id in the Raw Tab of the response header.




Thanks,

Diana



Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.