Using the CData ODBC Driver for ApprovalMax in PyCharm
The CData ODBC Drivers can be used in any environment that supports loading an ODBC Driver. In this tutorial we will explore using the CData ODBC Driver for ApprovalMax from within PyCharm. Included are steps for adding the CData ODBC Driver as a data source, as well as basic PyCharm code to query the data source and display results.
To begin, this tutorial will assume that you have already installed the CData ODBC Driver for ApprovalMax as well as PyCharm.
Add Pyodbc to the Project
Follow the steps below to add the pyodbc module to your project.
- Click File -> Settings to open the project settings window.
- Click Project Interpreter from the Project: YourProjectName menu.
- To add pyodbc, click the + button and enter pyodbc.
- Click Install Package to install pyodbc.
Connect to ApprovalMax
You can now connect with an ODBC connection string or a DSN. See the Getting Started section in the CData driver documentation for a guide to creating a DSN on your OS.
Start by setting the Profile connection property to the location of the ApprovalMax Profile on disk (e.g. C:\profiles\ApprovalMax.apip). Next, set the ProfileSettings connection property to the connection string for ApprovalMax (see below).
ApprovalMax API Profile Settings
To authenticate to ApprovalMax and connect to your own data or to allow other users to connect to their data, the ApprovalMax Public API requires the OAuth 2.0 authorization code flow.
First, you will need to register an OAuth application with ApprovalMax. Sign in to the ApprovalMax Developer Portal (https://developer.approvalmax.com/applications) and create a new application. Your OAuth application will be assigned a Client ID and a Client Secret, and you must register at least one Redirect URI (Callback URL).
A Premium ApprovalMax subscription (or active trial) is required to use the Public API.
After setting the following connection properties, you are ready to connect:
- AuthScheme: Set this to OAuth.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage the process to obtain the OAuthAccessToken.
- OAuthClientId: Set this to the Client ID that is shown in your application settings on the ApprovalMax Developer Portal.
- OAuthClientSecret: Set this to the Client Secret that is shown in your application settings on the ApprovalMax Developer Portal.
- CallbackURL: Set this to the Redirect URI that is registered in your application settings.
- Scope: (Optional) Override the default OAuth scopes. The default value openid offline_access https://www.approvalmax.com/scopes/public_api/read grants read-only access to all tables in this profile and enables refresh tokens. Use the principle of least privilege when narrowing this scope.
The OAuth Authorization URL is https://identity.approvalmax.com/connect/authorize and the Token URL is https://identity.approvalmax.com/connect/token. Both authorization_code and refresh_token grant types are supported.
Below is the syntax for a DSN:
[CData API Source]
Driver = CData ODBC Driver for ApprovalMax
Description = My Description
Profile = C:\profiles\ApprovalMax.apip
AuthScheme = OAuth
InitiateOAuth = GETANDREFRESH
OAuthClientId = your_client_id
OAuthClientSecret = your_client_secret
CallbackURL = your_callback_url
Execute SQL to ApprovalMax
Instantiate a Cursor and use the execute method of the Cursor class to execute any SQL statement.
import pyodbc
cnxn = pyodbc.connect('DRIVER={CData ODBC Driver for API};Profile = C:\profiles\ApprovalMax.apip;AuthScheme = OAuth;InitiateOAuth = GETANDREFRESH;OAuthClientId = your_client_id;OAuthClientSecret = your_client_secret;CallbackURL = your_callback_url;')
cursor = cnxn.cursor()
cursor.execute("SELECT UserId, Email FROM UserProfiles WHERE CompanyId = '00000000-0000-0000-0000-000000000000'")
rows = cursor.fetchall()
for row in rows:
print(row.UserId, row.Email)
After connecting to ApprovalMax in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to ApprovalMax data as if it were a standard database. If you have any questions, comments, or feedback regarding this tutorial, please contact us at [email protected].