Abhinay Ketha
Abhinay Ketha

Reputation: 51

Service now oauth grant flow error with oauth_token.do

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

Initially I made request:

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

and the response after allowing access to:

http://callback-url?code=Z2YYGhgfh1tMoFPDO7Dr0nZuPnhQPs53qwkm_Sw99gpUf92gU3x_OOuoOqdYBvlPFF01pOfgZg9VoXpCruSRYQ

after that to get token:

dev234.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?

Upvotes: 2

Views: 2850

Answers (2)

Abhinay Ketha
Abhinay Ketha

Reputation: 51

At last worked, after getting code from earlier steps,i made a php request looks like::

$client = new Client();

   $post_data = [
                   'grant_type' => 'authorization_code',
                   'code' => 'your code',
                   'redirect_uri' => '<callback_url>',
                   'scope' => 'useraccount'
               ];

   $auth = base64_encode('<client_id>:<client_secret>');

   $res = $client->request('POST', 'https://myinstance.service-now.com/oauth_token.do', [
       'headers' => [
           'Authorization' => 'Basic ' . $auth,
           'Content-Type' => 'application/x-www-form-urlencoded'
       ],
       'form_params' => $post_data
   ]);

   dd(json_decode($res->getBody(), true)); 

The response contains accesstoken and refresh token.

Upvotes: 3

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

Check step two

https://myinstance.servicenow.com/oauth_auth.do?grant_type=code&code={the auth code}&redirect_uri={the_same_redirect_url}&client_id={the_same_client_identifier}

you are using

grant_type=authorization_code

Upvotes: 0

Related Questions