为什么我在使用测试凭据时收到来自 CardPointe API 的 401 Unauthorized

Why am I receiving 401 Unauthroized from CardPoint API when using test credentials

我正在尝试测试 CardConnect 的 API,每当我向 https://fts.cardconnect.com:6443/rest/auth 发出请求时,我都会收到未经授权的响应。我试过将 rest sharp 的版本回滚到旧版本,但这似乎没有帮助。

这是主要功能。

public static void Main(String[] args)
        {
            // Send an Auth Transaction request
            String retref = authTransaction();
            // Void transaction
            voidTransaction(retref);
        }

这是我构建 JObject 以发送请求的地方。

public static String authTransaction()
        {
            Console.WriteLine("\nAuthorization Request");

            // Create Authorization Transaction request
            JObject request = new JObject();
            // Merchant ID
            request.Add("merchid", "496160873888");
            // Card Type
            request.Add("accttype", "VI");
            // Card Number
            request.Add("account", "4444333322221111");
            // Card Expiry
            request.Add("expiry", "0914");
            // Card CCV2
            request.Add("cvv2", "776");
            // Transaction amount
            request.Add("amount", "100");
            // Transaction currency
            request.Add("currency", "USD");
            // Order ID
            request.Add("orderid", "12345");
            // Cardholder Name
            request.Add("name", "Test User");
            // Cardholder Address
            request.Add("Street", "123 Test St");
            // Cardholder City
            request.Add("city", "TestCity");
            // Cardholder State
            request.Add("region", "TestState");
            // Cardholder Country
            request.Add("country", "US");
            // Cardholder Zip-Code
            request.Add("postal", "11111");
            // Return a token for this card number
            request.Add("tokenize", "Y");

            // Create the REST client
            CardConnectRestClient client = new CardConnectRestClient(ENDPOINT, USERNAME, PASSWORD);

            // Send an AuthTransaction request
            JObject response = client.authorizeTransaction(request);

            foreach (var x in response)
            {
                String key = x.Key;
                JToken value = x.Value;
                Console.WriteLine(key + ": " + value.ToString());
            }

            return (String)response.GetValue("retref");
        }

从这里开始,这是 object 最终发送的地方。

public JObject authorizeTransaction(JObject request)
        {
            return (JObject)send(ENDPOINT_AUTH, OPERATIONS.PUT, request);
        }
 private Object send(String endpoint, OPERATIONS operation, JObject request)
        {
            // Create REST client
            RestClient client = new RestClient(url);

            // Set authentication credentials
            client.Authenticator = new HttpBasicAuthenticator(username, password);

            // Create REST request
            RestRequest rest = null;
            switch (operation)
            {
                case OPERATIONS.PUT: rest = new RestRequest(endpoint, Method.Put); break;
                case OPERATIONS.GET: rest = new RestRequest(endpoint, Method.Get); break;
                case OPERATIONS.POST: rest = new RestRequest(endpoint, Method.Post); break;
                case OPERATIONS.DELETE: rest = new RestRequest(endpoint, Method.Delete); break;
            }

            rest.RequestFormat = DataFormat.Json;
            rest.AddHeader("Content-Type", "application/json");

            String data = (request != null) ? request.ToString() : "";
            rest.AddParameter("application/json", data, ParameterType.RequestBody);
            RestResponse response = client.ExecuteAsync(rest).GetAwaiter().GetResult();
            JsonTextReader jsreader = new JsonTextReader(new StringReader(response.Content));

            try
            {
                return new JsonSerializer().Deserialize(jsreader);
            }
            catch (JsonReaderException jx)
            {
                return null;
            }
        }

在大多数情况下,这只是 Cardpoint 在其 Github 页面上提供的文件,所以我很困惑为什么这不起作用。

我也尝试过更改我的凭据并手动添加授权 header。该文档提到会发生这种情况,因为它收到了无效的凭据。但是,在邮递员中使用相同的凭据 returns 200 响应。

我想通了这个问题。通常 RestSharp 会处理设置 content-type,但 CardConnect 是必须手动设置的时间之一。所以我遇到的问题是我如何设置 body 参数。我不得不改变

 rest.RequestFormat = DataFormat.Json;
        rest.AddHeader("Content-Type", "application/json");

        String data = (request != null) ? request.ToString() : "";
        rest.AddParameter("application/json", data, ParameterType.RequestBody);
        RestResponse response = client.ExecuteAsync(rest).GetAwaiter().GetResult();

 String data = (request != null) ? request.ToString() : "";
        rest.AddBody(data,"application/json");
        rest.AddHeader("Content-Type", "application/json");
        var response =  await client.ExecutePutAsync(rest);

执行此操作后,我的请求开始顺利通过。