How to use SQLAlchemy ORM to access Okta Data in Python



Create Python applications and scripts that use SQLAlchemy Object-Relational Mappings of Okta data.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems effectively. With the CData Python Connector for Okta and the SQLAlchemy toolkit, you can build Okta-connected Python applications and scripts. This article shows how to use SQLAlchemy to connect to Okta data to query Okta data.

With built-in optimized data processing, the CData Python Connector offers unmatched performance for interacting with live Okta data in Python. When you issue complex SQL queries from Okta, the CData Connector pushes supported SQL operations, like filters and aggregations, directly to Okta and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to Okta Data

Connecting to Okta data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

To connect to Okta, set the Domain connection string property to your Okta domain.

You will use OAuth to authenticate with Okta, so you need to create a custom OAuth application.

Creating a Custom OAuth Application

From your Okta account:

  1. Sign in to your Okta developer edition organization with your administrator account.
  2. In the Admin Console, go to Applications > Applications.
  3. Click Create App Integration.
  4. For the Sign-in method, select OIDC - OpenID Connect.
  5. For Application type, choose Web Application.
  6. Enter a name for your custom application.
  7. Set the Grant Type to Authorization Code. If you want the token to be automatically refreshed, also check Refresh Token.
  8. Set the callback URL:
    • For desktop applications and headless machines, use http://localhost:33333 or another port number of your choice. The URI you set here becomes the CallbackURL property.
    • For web applications, set the callback URL to a trusted redirect URL. This URL is the web location the user returns to with the token that verifies that your application has been granted access.
  9. In the Assignments section, either select Limit access to selected groups and add a group, or skip group assignment for now.
  10. Save the OAuth application.
  11. The application's Client Id and Client Secret are displayed on the application's General tab. Record these for future use. You will use the Client Id to set the OAuthClientId and the Client Secret to set the OAuthClientSecret.
  12. Check the Assignments tab to confirm that all users who must access the application are assigned to the application.
  13. On the Okta API Scopes tab, select the scopes you wish to grant to the OAuth application. These scopes determine the data that the app has permission to read, so a scope for a particular view must be granted for the driver to have permission to query that view. To confirm the scopes required for each view, see the view-specific pages in Data Model < Views in the Help documentation.

Follow the procedure below to install SQLAlchemy and start accessing Okta through Python objects.

Install Required Modules

Use the pip utility to install the SQLAlchemy toolkit and SQLAlchemy ORM package:

pip install sqlalchemy pip install sqlalchemy.orm

Be sure to import the appropriate modules:

from sqlalchemy import create_engine, String, Column from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker

Model Okta Data in Python

You can now connect with a connection string. Use the create_engine function to create an Engine for working with Okta data.

NOTE: Users should URL encode the any connection string properties that include special characters. For more information, refer to the SQL Alchemy documentation.

engine = create_engine("okta:///?Domain=dev-44876464.okta.com&InitiateOAuth=GETANDREFRESH&OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")

Declare a Mapping Class for Okta Data

After establishing the connection, declare a mapping class for the table you wish to model in the ORM (in this article, we will model the Users table). Use the sqlalchemy.ext.declarative.declarative_base function and create a new class with some or all of the fields (columns) defined.

base = declarative_base() class Users(base): __tablename__ = "Users" Id = Column(String,primary_key=True) ProfileFirstName = Column(String) ...

Query Okta Data

With the mapping class prepared, you can use a session object to query the data source. After binding the Engine to the session, provide the mapping class to the session query method.

Using the query Method

engine = create_engine("okta:///?Domain=dev-44876464.okta.com&InitiateOAuth=GETANDREFRESH&OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt") factory = sessionmaker(bind=engine) session = factory() for instance in session.query(Users).filter_by(Status="Active"): print("Id: ", instance.Id) print("ProfileFirstName: ", instance.ProfileFirstName) print("---------")

Alternatively, you can use the execute method with the appropriate table object. The code below works with an active session.

Using the execute Method

Users_table = Users.metadata.tables["Users"] for instance in session.execute(Users_table.select().where(Users_table.c.Status == "Active")): print("Id: ", instance.Id) print("ProfileFirstName: ", instance.ProfileFirstName) print("---------")

For examples of more complex querying, including JOINs, aggregations, limits, and more, refer to the Help documentation for the extension.

Free Trial & More Information

Download a free, 30-day trial of the CData Python Connector for Okta to start building Python apps and scripts with connectivity to Okta data. Reach out to our Support Team if you have any questions.

Ready to get started?

Download a free trial of the Okta Connector to get started:

 Download Now

Learn more:

Okta Icon Okta Python Connector

Python Connector Libraries for Okta Data Connectivity. Integrate Okta with popular Python tools like Pandas, SQLAlchemy, Dash & petl.