REST Table API Authorization Key

Abhishek Pidwa
Kilo Guru

Hi, 

I am writing a code in C# to call the servicenow Table API to retrieve the change_request rows. I am able to do this by setting Authorization to Basic with some value. It is working as expected since I took the authorization code from some other .NET application which was developed 3 years back. 

 

My question is where should I navigate to see the authorization value in ServiceNow? Where do we set this ? 

Any pointers would be great so that I can be sure that if someone changes that, I will be able to update my codebase accordingly.

 

 

1 ACCEPTED SOLUTION

i am adding one sample code here.

can you try to set like the same way.

 

 Uri sNowURI = new Uri("https://myinstance.service-now.com/api/now/table/mytable");

                               rest.BaseAddress = sNowURI;

                               rest.DefaultRequestHeaders.Authorization =

                                       new AuthenticationHeaderValue(

               "Basic",

               Convert.ToBase64String(

                       System.Text.ASCIIEncoding.ASCII.GetBytes(

                               string.Format("{0}:{1}", "user", "password"))));

                               rest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                               HttpResponseMessage response = await rest.GetAsync(sNowURI);

                               if (response.IsSuccessStatusCode)

                               {

                                       var contents = await response.Content.ReadAsStringAsync();

                               }

 

https://community.servicenow.com/community?id=community_article&sys_id=ffcce265dbd0dbc01dcaf3231f961...

View solution in original post

7 REPLIES 7

Hi Harsh,

 

Where exactly do you set that user up for Basic Authentication ? Are you using hardcoded username/password in your code to call that ? Or you are using some sort of encryption so that the username and password are hidden ?

This is how I am using the authorization code in my .NET application :

find_real_file.png

 

find_real_file.png

 

 

I want to know how can I get the encrypted value of the username/password which I have setup in the above example ? Where can I navigate in SNOW to do it ?

 

 

i am adding one sample code here.

can you try to set like the same way.

 

 Uri sNowURI = new Uri("https://myinstance.service-now.com/api/now/table/mytable");

                               rest.BaseAddress = sNowURI;

                               rest.DefaultRequestHeaders.Authorization =

                                       new AuthenticationHeaderValue(

               "Basic",

               Convert.ToBase64String(

                       System.Text.ASCIIEncoding.ASCII.GetBytes(

                               string.Format("{0}:{1}", "user", "password"))));

                               rest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                               HttpResponseMessage response = await rest.GetAsync(sNowURI);

                               if (response.IsSuccessStatusCode)

                               {

                                       var contents = await response.Content.ReadAsStringAsync();

                               }

 

https://community.servicenow.com/community?id=community_article&sys_id=ffcce265dbd0dbc01dcaf3231f961...

Awesome I got the username and password . Didn't know that basic Authorization uses Base 64 encoding . Tried this code and got the decoded value of username/password. 

 

find_real_file.png

 

Thanks again for your help.