无法获取身份验证 header 集

Cannot get authentication header set

我正在尝试建立一个 api 电话。我是新手,所以请多多包涵。

我被告知我需要先获得访问令牌才能进行查询。然后我将访问令牌传递给下一个查询。

这是我目前的情况:

public class Body
{
  public string username { set; get; }
  public string password { set; get; }
}

public class Check
{
  public string transaction_type { set; get; }
  public string check_number { set; get; }
  public string transit_number { set; get; }
  public string account_number { set; get; }
  public string amount { set; get; }
  public string magnetic_ink_check_reader { set; get; }
  public string name_on_check { set; get; }
  public string driver_license { set; get; }
  public string social_security_number { set; get; }
  public string date_of_birth { set; get; }
  public string state_code { set; get; }
  public string check_type { set; get; }
  public string account_type { set; get; }
  public string alliance_number { set; get; }
  public string authorization_option_form { set; get; }
  public string authorization_option_voice { set; get; }
  public string bill_to_street { set; get; }
  public string bill_to_city { set; get; }
  public string bill_to_state { set; get; }
  public string bill_to_postal_code { set; get; }
  public string bill_to_country { set; get; }
  public string city_of_account { set; get; }
  public string customer_id { set; get; }
  public string email { set; get; }
  public string external_ip { set; get; }
  public string invoice_number { set; get; }
  public string phone { set; get; }
  public string payment_reference_number { set; get; }
  public string raw_magnetic_ink_check_reader { set; get; }
  public string standard_entry_class_codes_type { set; get; }
}

private async Task GetResponse()
{
  var body = new Body();
  body.username = "someuser";
  body.password = "somepassword";

  var json = JsonConvert.SerializeObject(body);
  var content = new StringContent(json, Encoding.UTF8, "application/json");
  var response = await client.PostAsync("https://someurl.com/Identity", content);
  var responseString = await response.Content.ReadAsStringAsync();
  Console.WriteLine(responseString);

  var check = new Check();
  check.account_number = "1234949432";
  check.account_type = "Checking";
  check.alliance_number = string.Empty;
  check.amount = "4.00";
  check.authorization_option_form = "SinglePaymentSeries";
  check.authorization_option_voice = "ConsumerInitiatedCall";
  check.bill_to_city = "TAYLOR";
  check.bill_to_country = "US";
  check.bill_to_postal_code = "34567";
  check.bill_to_state = "AL";
  check.bill_to_street = "123 DARTMOUTH AVE";
  check.check_number = "1000";
  check.customer_id = "01-12345";
  check.check_type = "Personal";
  check.city_of_account = string.Empty;
  check.date_of_birth = string.Empty;
  check.driver_license = string.Empty;
  check.email = "some@where.com";
  check.transaction_type = "SALE";
  check.transit_number = "123123123";
  check.state_code = "LA";
  check.standard_entry_class_codes_type = "PPD";
  check.external_ip = string.Empty;
  check.invoice_number = "93939";
  check.raw_magnetic_ink_check_reader = string.Empty;
  check.phone = "4045551212";
  check.payment_reference_number = "292929";
  check.social_security_number = string.Empty;
  check.name_on_check = "Fred Durst";
  check.magnetic_ink_check_reader = string.Empty;

  var json2 = JsonConvert.SerializeObject(check);
  var content2 = new StringContent(json2, Encoding.UTF8, "application/json");

  var userObj = JObject.Parse(responseString);
  var accesstoken = Convert.ToString(userObj["access_token"]);
  client.DefaultRequestHeaders.Add("Authorization", accesstoken);
  
  var response2 = await client.PostAsync("https://someurl.com/transactions", content2);
  Console.WriteLine(response2);
}

我在完成初始令牌请求后传递访问令牌时遇到问题。我认为将它传递给下一个查询将是设置 header.

的问题
  client.DefaultRequestHeaders.Add("Authorization", accesstoken);

但是我在尝试时得到了一个无效的访问令牌。

如有任何建议,我们将不胜感激。

这是我从电话中得到的回复:

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Request-Context: appId=cid-v1:cc2757c0-2318-4d02-bb2a-0764c63ecca7
Strict-Transport-Security: max-age=31536000;includeSubDomains
X-Content-Type-Options: nosniff Date: Mon, 14 Feb 2022 19:00:39 GMT Set-Cookie: BNI_dccpersistence=MWGxSpqpbDbp6MmnAfvBPRLf4oDeJ6H58Sz_oldOmlcyATPp33UfYlKxU_RJNzeBGtiv71FkrjwNaeDCtkM0Jw==; Path=/;Secure; WWW-Authenticate: Bearer Content-Length: 0 }}

作为对 Silvermind 的回应,我要取回 Bearer 并将其传递进来:

试试这个

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);