oauth grant_type=code

abhinay_ketha
Kilo Contributor

Hi,

i follow the article OAuth authorization code grant flow to get tokens

initially i made request

https://xxxx.service-now.com/oauth_auth.do?response_type=code&client_id=****534e4e81b7f

and the response after allowing access to

https://<callback-url>?code=Z2YYGhgfh1tMoFPDO7Dr0nZuPnhQPs53qwkm_Sw99gpUf92gU3x_OOuoOqdYBvlPFF01pOfgZg9VoXpCruSRYQ

after that to get token

https://xxxx.service-now.com/oauth_token.do?grant_type=authorization_code&code=<***>&client_id=<***>&client_secret=<***>

when i did request this,throwing error

{"error_description": "access_denied","error": "server_error"}

can't i get access_token and refresh_token in json format?

help me

1 ACCEPTED SOLUTION

yoav
Giga Contributor

Finally we did it. Here is working solution written in python.



1. Initial request (URL where to redirect user) -


  https://<your-instance>.service-now.com/oauth_auth.do?response_type=code&client_id=<your_client_id>&...



2. Request to get access/refresh tokens by code.


import base64
import requests
post_data = {
      'grant_type': 'authorization_code',
      'code': '<code>',
      'redirect_uri': '<your-redirect-url>',
      'scope': 'useraccount'
}
auth = base64.b64encode('<client_id>:<client_secret>')
headers = {
      'Authorization': 'Basic {}'.format(auth)
}
resp = requests.post(
      'https://<your_instance>.service-now.com/oauth_token.do',
      data=post_data,
      headers=headers)
# Now response contains access and refresh tokens.
print(resp.json())

View solution in original post

4 REPLIES 4

yoav
Giga Contributor

Hi, abhinay.ketha



We have the same issue here. Did you make any progress?



Thanks.


yoav
Giga Contributor

Finally we did it. Here is working solution written in python.



1. Initial request (URL where to redirect user) -


  https://<your-instance>.service-now.com/oauth_auth.do?response_type=code&client_id=<your_client_id>&...



2. Request to get access/refresh tokens by code.


import base64
import requests
post_data = {
      'grant_type': 'authorization_code',
      'code': '<code>',
      'redirect_uri': '<your-redirect-url>',
      'scope': 'useraccount'
}
auth = base64.b64encode('<client_id>:<client_secret>')
headers = {
      'Authorization': 'Basic {}'.format(auth)
}
resp = requests.post(
      'https://<your_instance>.service-now.com/oauth_token.do',
      data=post_data,
      headers=headers)
# Now response contains access and refresh tokens.
print(resp.json())

thnx


mahi4889Snow
Tera Contributor

Hello Team,

    how do we call the redirect_uri when no redirect url register under application registry?