How to authenticate and access Servicenow workflow from .net c# REST API call. Servicenow account having OCTA authentication
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2022 10:21 PM
My organization having Service now account and created workflow and currently accessing through servicenow website. Having the authorization connected with microsoft account. i need to access workflow from .net c# with REST API call.i tried with basic authentication code, but am not able to authorize to access. Can you help me to how to authorize to servicenow account and access the workflow.
var client = new HttpClient();
client.BaseAddress = new Uri("https://XXXXX/nav_to.do?uri=%2Fu_accounts_XXXXX_list.do%3Fsysparm_first_row%3D1%26sysparm_query%3Dactive%253Dtrue%255Eu_assignment_group%2XXXXX484%26sysparm_view%3D");
var request = new HttpRequestMessage(HttpMethod.Post, "/path");
var byteArray = new UTF8Encoding().GetBytes("<clientid>:<clientsecret>");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("grant_type", "password"));
formData.Add(new KeyValuePair<string, string>("username", "XXXX@lkqcorp.com"));
formData.Add(new KeyValuePair<string, string>("password", "XXXXX"));
formData.Add(new KeyValuePair<string, string>("scope", "all"));
request.Content = new FormUrlEncodedContent(formData);
var response = await client.SendAsync(request);
var contents = await response.Content.ReadAsStringAsync();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 06:56 PM
u r getting this error after running ur code ? or via manual login ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2022 12:46 AM
UriBuilder builder = new UriBuilder("https://ccc.service-now.com/u_accounts_payable_queue_list.do");
var query = HttpUtility.ParseQueryString(builder.Query);
query["sysparm_limit"] = "2";
query["sysparm_offset"] = "2";
builder.Query = query.ToString();
string apiUrl = builder.ToString();
var credentials = new NetworkCredential(Environment.GetEnvironmentVariable("nxabhimanyu@lkqcorp.com"), "lkq@mar123");
using (var handler = new HttpClientHandler { Credentials = credentials })
using (HttpClient client = new HttpClient(handler))
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
var table = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Data.DataTable>(data);
}
}
By above code am getting the error

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2022 09:43 PM
looks like its a basic auth ! can u try to login with the credential u r passing in SN instance itself ? error seems to tell me there is something wrong with the profile setup
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2022 10:55 PM
Yes am using Basic authentication, By using browser i can able to login with same credentials, It is redirects through microsoft 365 account.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 03:31 AM
you will have to use backdoor , like https://INSTANCE NAME.service-now.com/login.do > use this way and see if u can login , also its ideal if you work this out with ur sn admin itself , much easier to solve !