ErocM
ErocM

Reputation: 4662

Cannot get authentication header set

I'm trying to set up an api call. I'm new to this so please bear with me.

I was told that I needed to get a access token before I can make a query. Then I pass the access token in to the next query.

Here is what I have so far:

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 = "[email protected]";
  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);
}

I'm having problems passing in the access token once I have completed the initial token request. I thought that passing it to the next query would be a matter of setting the header.

  client.DefaultRequestHeaders.Add("Authorization", accesstoken);

But I'm getting an invalid access token when I try.

Any suggestions would be greatly appreciated.

This is the response I am getting back from the call:

{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 }}

In response to Silvermind, I'm getting back the Bearer and passing it in:

enter image description here

Upvotes: 0

Views: 910

Answers (1)

Serge
Serge

Reputation: 43860

try this

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

Upvotes: 1

Related Questions