How to authenticate and access Servicenow workflow from .net c# REST API call. Servicenow account having OCTA authentication

nataraju
Kilo Explorer

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();

 

11 REPLIES 11

Ravi9
ServiceNow Employee
ServiceNow Employee

u r getting this error after running ur code ? or via manual login ? 

 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

 

 

Ravi9
ServiceNow Employee
ServiceNow Employee

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

Yes am using Basic authentication, By using browser i can able to login with same credentials, It is redirects through microsoft 365 account.

Ravi9
ServiceNow Employee
ServiceNow Employee

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 !