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

what kind of authentication been set up an SN end ? is it basic or oauth ? 

nataraju
Kilo Explorer

I dont have access to check ServiceNow authentication, That is maintaining from admin, can you help me how to know from from REST API call .

Ravi9
ServiceNow Employee
ServiceNow Employee

depends on the error you are getting 

typically we use 2 auth protocol while there are exceptions of course 

basic auth and oauth 

for basic auth sn admin has to provide u user name and pswd 

for oauth client id and secret 

basic auth is most common if the data you require is not critical !

Am Getting Error as below

"You are not logged in, or your session has expired. Redirecting to the login page...".

Does it requires OAuth Authentication