What you will build
A Python script, app_auth.py, that authenticates as an application, without a user, with OAuth 2.0 credentials and the client credentials flow:
- It exchanges a client ID and client secret for an app token using requests-oauthlib.
- It calls the portals/self endpoint with that token and prints the privileges the token carries.
You will create the OAuth credentials in the portal first, then write the script that uses them.
Check prerequisites
You will need an ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account that can create developer credentials. In ArcGIS Online and ArcGIS Enterprise, that means a user type of Creator or higher.
To run the script: Python 3.12 or later and requests-oauthlib (pip install requests-oauthlib), which installs oauthlib too.
Adapted from an original tutorial by Dave Olsthoorn.
Create the OAuth credentials
Create the credentials for your script in your portal:
- Go to Content > My content > New item.
- Click Developer credentials.
- Select OAuth 2.0 credentials (For app authentication).
- Select Private application with selected privileges, so the token gets only the privileges you grant.
- Select No item access. This script reads no items; for your own app, grant access only to the items it needs.
- Leave the privileges off. This script needs none; for your own app, enable only the ones it calls.
- Leave the referrer URLs empty. A backend script doesn’t need them: they help only when the token ends up in a web browser.
- Fill in the item details.
- Review the summary and create the item.
- Copy the Client ID and the Client Secret.










Import the library
Import two classes:
BackendApplicationClient, from oauthlib, tells the session to use the client credentials flow, for a backend client with no user. It also stores the client ID.OAuth2Session, from requests-oauthlib, is arequestssession that gets the token and adds it to every request you send with it.
Configure the script
Paste the client ID and the client secret from the credentials item:
The client secret is as sensitive as a password. Keep it in server-side code only, and out of your repository: in production, read it from the environment or a secret manager.
Authenticating against ArcGIS Enterprise?
If you created the credentials in ArcGIS Enterprise, set the Portal URL:
Build the token endpoint
The script requests its token from the portal’s OAuth 2.0 token endpoint: the portal URL followed by /sharing/rest/oauth2/token.
Set up the OAuth session
Create the client with your client ID, and the session from the client. The session uses it to request the token and to send your requests with it.
auto_refresh_url and auto_refresh_kwargs let the OAuth2Session request new tokens with the refresh token instead of the client secret.
Request a token
Call fetch_token with the client secret. The ArcGIS REST API expects the client ID and the client secret as parameters of the POST request, so set include_client_id=True.
The session stores the token in its token attribute.
Make a request
Send requests to the ArcGIS REST API or to ArcGIS services through the session: it adds the token to each one in the Authorization header. The script uses get(), and the other requests methods, such as post(), work the same way.
The script calls portals/self, which returns the application’s title and the privileges of its token, among other details.
Run the script
Run the script. It prints the title of the credentials item and the privileges of the token:
> python app_auth.py
Title: "Tutorial: App Authentication with python requests-oauthlib"
Has the following privileges:
- premium:user:basemapsYour script authenticates as an application. To act on behalf of your users instead, sign them in with OAuth 2.0 in a browser app.