- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 08:58 AM
Hello everyone,
I have been trying to create an access_token with my refresh_token in C#, I want to use oauth2 authentication from ServiceNow. I succeeded with Postman but every time I try in C# I get the error "access denied".
Does someone know the correct way? Many thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 10:04 AM
If your OAuth2 authentication works in Postman but fails with "access denied" in C#, it's likely an issue with your request format, headers, or authentication details. Here’s how you can troubleshoot and fix it:
1. Verify Postman Configuration
Since it works in Postman, check the following:
Grant Type: Make sure you’re using refresh_token.
Headers: Look at Content-Type, Authorization, and other headers.
Body: Ensure you're sending the correct data format.
2. Correct C# Code for Refreshing Token
Here’s a working C# example using HttpClient:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
class Program
{
static async Task Main()
{
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string refreshToken = "your_refresh_token";
string tokenUrl = "https://your-instance.service-now.com/oauth_token.do";
using (HttpClient client = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
// Headers
var authValue = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authValue);
// Body parameters
var body = new Dictionary<string, string>
{
{ "grant_type", "refresh_token" },
{ "refresh_token", refreshToken }
};
request.Content = new FormUrlEncodedContent(body);
// Send request
HttpResponseMessage response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Token Response: " + responseString);
}
else
{
Console.WriteLine("Error: " + response.StatusCode);
Console.WriteLine("Response: " + responseString);
}
}
}
}
3. Common Issues & Fixes
(a) Invalid Client Credentials
Ensure client_id and client_secret match the ones in ServiceNow.
Try copy-pasting the values from Postman.
(b) Incorrect Authorization Header
The Authorization header must be in Basic Auth format:
Authorization: Basic Base64(client_id:client_secret)
The Convert.ToBase64String() method in the C# code handles this.
(c) Incorrect Token URL
The token URL should be:
https://your-instance.service-now.com/oauth_token.do
Replace your-instance with your actual ServiceNow instance.
(d) Missing Headers
Ensure Content-Type is application/x-www-form-urlencoded.
(e) Refresh Token Expired
Check the response from ServiceNow in Postman; it might say "invalid_grant".
Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.
With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 10:04 AM
If your OAuth2 authentication works in Postman but fails with "access denied" in C#, it's likely an issue with your request format, headers, or authentication details. Here’s how you can troubleshoot and fix it:
1. Verify Postman Configuration
Since it works in Postman, check the following:
Grant Type: Make sure you’re using refresh_token.
Headers: Look at Content-Type, Authorization, and other headers.
Body: Ensure you're sending the correct data format.
2. Correct C# Code for Refreshing Token
Here’s a working C# example using HttpClient:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
class Program
{
static async Task Main()
{
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string refreshToken = "your_refresh_token";
string tokenUrl = "https://your-instance.service-now.com/oauth_token.do";
using (HttpClient client = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
// Headers
var authValue = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authValue);
// Body parameters
var body = new Dictionary<string, string>
{
{ "grant_type", "refresh_token" },
{ "refresh_token", refreshToken }
};
request.Content = new FormUrlEncodedContent(body);
// Send request
HttpResponseMessage response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Token Response: " + responseString);
}
else
{
Console.WriteLine("Error: " + response.StatusCode);
Console.WriteLine("Response: " + responseString);
}
}
}
}
3. Common Issues & Fixes
(a) Invalid Client Credentials
Ensure client_id and client_secret match the ones in ServiceNow.
Try copy-pasting the values from Postman.
(b) Incorrect Authorization Header
The Authorization header must be in Basic Auth format:
Authorization: Basic Base64(client_id:client_secret)
The Convert.ToBase64String() method in the C# code handles this.
(c) Incorrect Token URL
The token URL should be:
https://your-instance.service-now.com/oauth_token.do
Replace your-instance with your actual ServiceNow instance.
(d) Missing Headers
Ensure Content-Type is application/x-www-form-urlencoded.
(e) Refresh Token Expired
Check the response from ServiceNow in Postman; it might say "invalid_grant".
Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.
With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2025 06:33 AM
Thank you, this helped me out a lot 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2025 06:39 AM
Hi Jens,
Thanks for accepting the solution provided and it worked at your end. That means a lot on supporting me.
Regards,
Krishna Kumar M