OAuth 2.0 grant authorization_code redirects to oauth_login.do

rerogers
Giga Contributor

I am very new to servicenow and if I need to post this somewhere else please let me know.  I am trying to create an OAuth code flow from an external service.  This is how I have my servicenow application registry configured:
2025-06-09 12_28_13-Application-Registries.png

When I make the OAuth code grant flow request everything works as expected and I receive the authorization code.  However, when I grant type authorization_code request I am redirected to https://dev#.service-now.com/oauth_login.do URL and I do not receive an access token as expected.

I am making this request from a Laravel PHP controller so there is no user to complete the login page.

 

Here is a snippet of my PHP code:

       $fields = [
                'grant_type'  => 'authorization_code',
                'client_id' => $fields->client,
                'client_secret' => $fields->secret,
                'code' => $q['code'],
                'redirect_uri'  => "$hostUrl/externalurl/oauth/response",
                'response_type' => 'code',
                'state' => $q['state']
            ];

            //url-ify the data for the POST
            $fields_string = http_build_query($fields);

            //open connection
            $ch = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'cache-control: no-cache',
                'content-type: application/x-www-form-urlencoded'
            ));
            curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

            $result = curl_exec($ch);



1 REPLY 1

rerogers
Giga Contributor

I figured out what I was doing wrong, I was reusing the authentication code route instead of the oauth token route.