Using the CData ODBC Driver for Outlook 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 Outlook 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 Outlook 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 Outlook
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.
Using OAuth Authentication
Microsoft Graph API uses OAuth 2.0 for authentication. You must register an application in the Microsoft Azure Portal to obtain OAuth credentials (Client ID and Client Secret).
Obtaining OAuth Credentials
- Log in to the Azure Portal.
- Navigate to Azure Active Directory > App registrations.
- Click New registration to create a new application.
- Enter an application name and select the appropriate account types.
- Set the Redirect URI to your application's callback URL (e.g., http://localhost:33333 for desktop apps).
- Click Register to create the application.
- On the application overview page, copy the Application (client) ID - this is your OAuthClientId.
- Navigate to Certificates & secrets and create a new client secret.
- Copy the client secret value - this is your OAuthClientSecret.
- Navigate to API permissions and add the required Microsoft Graph API permissions:
- Mail.Read - For accessing email messages
- Contacts.Read - For accessing contacts
- Calendars.Read - For accessing calendar events
- Tasks.Read - For accessing To Do tasks
- offline_access - For obtaining refresh tokens
- Click Grant admin consent to grant these permissions.
Connecting with OAuth
After setting the following connection properties, you are ready to connect:
- AuthScheme: Set this to OAuth.
- InitiateOAuth: Set this to GETANDREFRESH. The CData API Profile for Outlook will automatically walk through the OAuth process in order to obtain the access token.
- OAuthClientId: Set this to the Application (client) ID from Azure Portal.
- OAuthClientSecret: Set this to the client secret value from Azure Portal.
- TenantId: Set this to your Azure AD tenant identifier (GUID or domain name like 'contoso.onmicrosoft.com').
- CallbackURL: Set this to the Redirect URI you specified in your app registration (e.g., http://localhost:33333 for desktop apps).
Example connection string
Profile=C:\profiles\Outlook.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;TenantId=your_tenant_id;CallbackUrl=http://localhost:33333;
Below is the syntax for a DSN:
[CData API Source] Driver = CData ODBC Driver for Outlook Description = My Description Profile = C:\profiles\Outlook.apip AuthScheme = OAuth InitiateOAuth = GETANDREFRESH OAuthClientId = your_client_id OAuthClientSecret = your_client_secret TenantId = your_tenant_id CallbackUrl = http://localhost:33333
Execute SQL to Outlook
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\Outlook.apip;AuthScheme = OAuth;InitiateOAuth = GETANDREFRESH;OAuthClientId = your_client_id;OAuthClientSecret = your_client_secret;TenantId = your_tenant_id;CallbackUrl = http://localhost:33333;')
cursor = cnxn.cursor()
cursor.execute("SELECT , FROM CalendarGroupCalendars WHERE CalendarGroupId = 'group_id'")
rows = cursor.fetchall()
for row in rows:
print(row., row.)
After connecting to Outlook in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to Outlook 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].