Serverless native and mobile app workflow
This OAuth2.0 authentication method is ideal for applications built using ArcGIS Maps SDKs for Native Apps or using a custom URI handler, and for all serverless native and mobile applications using an OAuth 2.0 authorization_
grant type. Your users can be registered on ArcGIS Online or ArcGIS Enterprise.
There are 2 available methods for developers implementing this workflow in a native application:
Recommended: Use the ArcGIS Maps SDK for Native Apps
Authentication
,Manager OAuth
, andConfiguration Credential
objects to obtain and manage credentials. This requires minimal knowledge of OAuth 2.0 and these components handle the bulk of the process.Cache If you choose this method, use the sample for
Authentication
from your Native SDK:Manager Register a custom URI handler for your application, such as
myapp:
, to which your users are redirected upon completion of the OAuth 2.0 validation process.//auth
This workflow demonstrates how to implement a custom URI handler.
Overview
Direct your users to the authorization endpoint in a browser on the device with your
client_
,id response_
, atype=code redirect_
, and an optionaluri expiration
indicating the length of the authorization code's validity. The redirect URI must have been previously registered with the app or be a known URI. The user is presented a login form.Use dark colors for code blocks Copy When the user successfully provides their username + password, they are redirected to the configured redirect URI (either your application with a custom redirect URI such as
myapp:
, or a known, hosted redirect page//oauth.callback urn:
). The redirect URI string is appended with anietf: w g: oauth: 2.0:oob authorization code
. Use this code to request anaccess token
.Use dark colors for code blocks Copy Your application exchanges the
authorization code
for anaccess token
from the token endpoint. This requires the following:- The same application
client_
from step 1id - A
response_
=type AUTH_
CODE - The same
redirect_
from step 1uri - The
authorization code
from the response - Notice that
client_
is not used in this flowsecret
Use dark colors for code blocks Copy - The same application
The token endpoint replies with a JSON object containing either the authorization attributes or an error. Once you receive the
access token
, include it in all subsequent requests that require atoken
.
Responses
Error codes are generally standard HTTP status codes ranging 400-499, and the message indicates the failure source / details. The most common failure cause is an expired or invalid authorization code. In this event, generate a new authorization code and attempt the request again.
Process details
Configure the redirect URI
Sign into your dashboard and click the OAuth 2.0 tab.
Follow the steps in the Add a redirect URI tutorial.
You can also configure a redirect URI in ArcGIS Online or ArcGIS Enterprise.
Ensure that your redirect URI matches the handler you configured for your application in step 2. For example
my-arcgis-app:
. This is where the authorization code is sent once the user has provided credentials. Your application will need to read this URI and parse the authorization code from it.//auth
With a custom redirect URI
In order to redirect the user back to your application after they authenticate, you will need to set up a custom protocol handler. This varies from platform to platform.
When the user authorizes your application, the browser is redirected to this URI (for example, my-arcgis-app:
) with their authorization code. Using this example, you must ensure that your platform understands that my-arcgis-app
is your application. There are resources available to assist you with the various platforms.
Get an authorization code
Open an authorization browser window for the user, using the correct endpoint and parameters.
A browser provides added security measure:
- It displays the identity of the
arcgis.com
authorization page in the URL status bar. - Users can verify the browser's SSL status to confirm that
arcgis.com
is a valid host for the authorization page. - It confirms that username + password is given only to
arcgis.com
and not to an unknown entity, an unverified application, or to a third party.
Authorization endpoints
https:
https:
Authorization parameters
Parameter | Required | Format | Description |
---|---|---|---|
client_ | ✓ | string | Your application's client_ . |
redirect_ | ✓ | string | The redirect_ configured above. The user is redirected to this endpoint with the authorization_ . |
response_ | ✓ | code | You must include this OAuth 2.0 grant type. |
expiration | number | Requested duration (in seconds) of the refresh_ . -1 = a refresh_ that will last forever. ArcGIS Online organizations can override this with a shorter duration. |
Authorization example
Exchange the code for a token
Once the user authorizes your application, the browser will redirect them to the configured redirect URL with the appended authorization code
, in this example:
You can now exchange this code
with the token endpoint for a user's access_
.
Token endpoints
https:
https:
Request parameters
All request parameters should be form encoded
.
Parameter | Required | Format | Description |
---|---|---|---|
client_ | ✓ | string | Your application's client_ . |
code | ✓ | string | The authorization code . |
redirect_ | ✓ | string | The redirect_ used with this authorization code in the previous step. |
grant_ | ✓ | authorization_ | You must include this OAuth 2.0 grant type. |
Response object
The response object will contain an access_
, expires_
(number of seconds until the access_
expires), and the universally unique username
.
Refresh the token
The authorization code
grant type recommends short-lived access tokens and long-lived refresh tokens.
Token error messages
When a token expires, you will receive the following response. This typically means that your token has expired or is invalid. If you have a refresh token from the previous step, you can get a new access_
and try your request again.
Refresh request parameters
All request parameters should be form encoded
.
Parameter | Required | Format | Description |
---|---|---|---|
client_ | ✓ | string | Your application's client_ . |
refresh_ | ✓ | string | Your user's refresh token. |
grant_ | ✓ | refresh_ | You must include this OAuth 2.0 grant type. |
Response object
In the response you will receive an access_
for the user; you will not receive a new refresh token. If their refresh token expires, the user must instead go through the full sign in process.