- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I am trying to implement OAuth to authenticate external clients who access our custom scripted REST API .
I followed the instructions in the ServiceNow docs > OAuth with inbound REST article to set the OAuth endpoint for external clients, as shown in the screenshot below:
I am using this PowerShell script to make a grant_type=password request to get the access token.
$client_id = '<client id>'
$client_secret = '<client secret>'
$rest_user = '<user name>'
$rest_pw = '<user password>'
$token_url = 'https://<server url>/oauth_token.do'
$content_type = 'application/x-www-form-urlencoded'
$body = "grant_type=password&client_id=$($client_id)&client_secret=$($client_secret)&username=$($rest_user)&password=$($rest_pw)"
$response = Invoke-RestMethod -Method Post -Uri $token_url -ContentType $content_type -Body $body
The Invoke-RestMethod returns the following error:
{"error_description":"access_denied","error":"server_error"}
I have confirmed the username and password credentials by successfully using them with Basic Authentication.
We are currently on Xanadu. I remember previously using the same PowerShell code to successfully get an OAuth access token on an earlier release.
What am I doing wrong? Is there another way to get the OAuth access token?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Any specific reason to use Powershell script to get OAuth token ?
Did you try OAuth token request from Postman or REST API Explorer to check you are able to fetch access token and refresh token ?
From Washington release, you can also use Client Credentials based OAuth tokens and would be simpler than grant_type=password OR grant_type=refresh_token
Please refer below thread where I provided more information on this
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Colleen,
Both grant types (Password and Client Credentials) will work from external API with the context of "OAuth Application User". I tried and tested in my PDI for Scripted REST API and called it from PowerShell. See snip below. It works fine. Ensure that your "OAuth Application User" has right roles associated with it.
Looks like your body parameters in the PowerShell were getting converted into string. That could be the reason.
See working solutions below for both.
1) With Grant Type = Password
2) With Grant Type = Client Credentials
Hope it helps.
Let me know if it worked.
Regards,
Vikas K
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Colleen ,
Please refer to the following community post:
Solved: OAuth 2.0 error access_denied - ServiceNow Community
Try recreating a new application registry.
You can try this as well. It may resolve your issue as well.
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Any specific reason to use Powershell script to get OAuth token ?
Did you try OAuth token request from Postman or REST API Explorer to check you are able to fetch access token and refresh token ?
From Washington release, you can also use Client Credentials based OAuth tokens and would be simpler than grant_type=password OR grant_type=refresh_token
Please refer below thread where I provided more information on this
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Did you get a chance to review this as I believe the information provided should answer your question.
If my response helped to guide you or answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Bhuvan
I want to use OAuth to authenticate a scripted REST API to enable an external client to generate tickets in ServiceNow. I was trying to use the password grant_type because I want the tickets to be associated with the specified user. As far as I understand the client_credentials grant type is not associated with a user context.