{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":40433,"path":"platform-security/authentication/c_OAuthAuthorizationCodeFlow.md","title":"OAuth authorization code grant flow","bundle":null,"doc_type":null,"release":"australia","body":"\n# OAuth authorization code grant flow\n\nAuthorization code grant flow allows a user to access a resource by authenticating directly with an OAuth server that trusts the resource, in contrast with authenticating with username/password credentials.\n\nThis implementation of OAuth authorization code flow allows access to a resource via REST. The authorization code framework gets the access token through the authorized URL that the user configures rather than requiring the user to enter a username/password. The username/password are never exposed to the client that is requesting access to the resource.\n\n## A ServiceNow instance as the authorization server\n\nThe OAuth server is typically a third-party authorization server. You can also specify a ServiceNow instance as the authorization server that issues the tokens for authorization code flow.\n\nThe user who owns the restricted resource must authorize access. The user can also revoke the issued access token at any time to terminate access.\n\n## Authorization code grant flow process\n\nThe Authorization code grant flow process consists of these three steps:\n\n\\[Omitted image \"AuthCodeGrantFlow1.png\"\\] Alt text: Authorization code grant flow process\n\nIn step one, the client application or website initiates a REST API call in the form of a GET request to the instance via the user agent. Typically, the REST call is initiated when the end user clicks a button or a link on the client application or website to request an access token. In the client application, the end user also has to specify the authorization URL, token URL, client ID, and client secret. For an explanation of these items, see the field descriptions in this topic: [Use a third-party OAuth provider](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/web-services/t_UseAThirdPartyOAuthProvider.md). If the client asks for a grant type, the end user must select **Authorization Code**.\n\nExample GET request from the client application to the instance:\n\n```\nhttps://myinstance.service-now.com/oauth_auth.do?response_type=code&redirect_uri={the_redirect_url}&client_id={the_client_identifier}\n```\n\n**Note:** The **response\\_type** must be **code** to use the standard OAuth code grant flow.\n\nThe end user must manually allow access to the restricted resource on the instance. In the ServiceNow implementation, the end user must be logged into the instance. The instance prompts the end user with a UI page that has **Allow** and **Deny** buttons.\n\nThe item that the client application is actually requesting the token from is the OAuth provider application registry record that you created, also known as the authorization endpoint \\(see [Use a third-party OAuth provider](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/web-services/t_UseAThirdPartyOAuthProvider.md)\\). The auth code is sent from the authorization endpoint to the client. It does not go to the client directly but to the **Redirect URL** that you specify on the authorization endpoint form. This URL is also known as a callback URL. You can obtain this URL from the client application or website.\n\nExample response from the instance to the client application, providing an authorization code:\n\n```\nhttps/http://{callbackURL}?code={the actual auth code}\n```\n\n\\[Omitted image \"AuthCodeGrantFlow2.png\"\\] Alt text: Client application sends auth code to ServiceNow instance, which in turn sends a token\n\nNow that the client application has the authorization code, the client uses the code to request the access token. The authorization code proves that the user has consented in step 1.\n\nExample POST request from the client application to the ServiceNow instance that provides the auth code and requests the access token:\n\n```\nhttps://myinstance.service-now.com/oauth_token.do?grant_type=authorization_code&code={the auth code}&redirect_uri={the_same_redirect_url}&client_id={the_same_client_identifier}&client_secret={client_secret_value}\n```\n\nThe endpoint on the instance returns an access token and a refresh token. The refresh token can be used to request additional access tokens.\n\nYou can manage the tokens, including revoking the token, in the instance. See [Manage OAuth tokens](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-security/authentication/t_ManageTokens.md).\n\n\\[Omitted image \"AuthCodeGrantFlow3.png\"\\] Alt text: Client application sends a token to ServiceNow instance, which then sends a JSON payload of data\n\nThe client application uses the access token to authenticate to the REST API. After authenticating the client application, the REST API returns the requested data in a JSON payload.\n\nExample GET request for the JSON payload of data for the Incident \\[incident\\] table:\n\n```\nhttps://myinstance.service-now.com/api/now/table/incident?access_token={the_token}\n```\n\n**Note:** The system also supports [OAuth implicit grants](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-security/authentication/c_OAuthImplicitGrants.md), also known as implicit grant code flow.\n\n## Integration support\n\nAuthorization code flow supports the following integrations on the instance:\n\n-   Multi-SSO\n-   SAML 2.0 Update 1\n-   Multifactor authentication\n\nThe mobile interface is also supported.\n\n"}}