How to connect Python with OneDrive ODBC

Introduction

This guide will walk you through connecting Python with the OneDrive API driver using the OneDrive API. Integrating OneDrive with Python allows you to automate file storage tasks like uploading, downloading, sharing, and organizing files directly from your Python code. This connection provides seamless interaction with OneDrive, enabling you to manage cloud files efficiently and streamline workflows.

Prerequisites

  • ODBC PowerPack: Download and install the ZappySys ODBC PowerPack from the Customer Download Area or the trial version.
  • Microsoft account
  • pyodbc: An open-source Python module that makes accessing ODBC databases simple.

Steps

Install pyodbc in Python

To connect to ODBC-supported databases, install pyodbc in Python. This enables operations such as querying and managing data with ease.

  1. Ensure Python is installed on your system. Download it if needed.
  2. Open your terminal or command prompt.
  3. Install pyodbc using pip:
python -m pip install pyodbc

Reasons to Install:

  • If pyodbc is not installed, your Python script will generate the error:

ModuleNotFoundError: No module named 'pyodbc'

  • Database Connectivity: pyodbc enables Python to connect to ODBC databases such as Microsoft SQL Server, PostgreSQL, and MySQL.
  • Data Operations: Allows SQL queries, data retrieval, and other database interactions within Python scripts.
  • Cross-Platform Support: Works with operating systems like Windows, macOS, and Linux.
  • Simplicity and Efficiency: Provides an intuitive interface for managing database connections.

By installing pyodbc, you can integrate your Python applications with various ODBC-supported databases for efficient data management and analysis.

Create an Application on the OneDrive Side

  1. Go to the Azure Portal and log in.
  2. Navigate to Azure Active Directory.
  3. Click App registrations.
  4. Select New registration.
  5. Enter an application name.
  6. Choose Accounts in this organizational directory only for the supported account type.
  7. For the Redirect URI, select Web and enter https://login.microsoftonline.com/common/oauth2/nativeclient or any valid redirect URL, like https://zappysys.com/oauth.
  8. Save your Application (client) ID, TenantID, and Secret ID.

Create a New Driver

  1. Open ODBC Data Source by typing “ODBC” in the search box.
    Open ODBC Data Source

  2. For SQL Server Integration, go to User DSN or System DSN. Click Add.
    ZappySys ODBC Driver - Open UI

  3. From the driver list, select ZappySys API Driver, choose OneDrive, and click Continue.

OneDrive Connector Configuration

  1. Fill in Client ID and Client Secret and specify the necessary Scopes. Use the following URLs for Authorization and Token:
* https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize
* https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token
  1. Generate the token and test the connection.

  2. Go to the Preview Tab, select a table, and view the results.

Read Data in Python Using ODBC DSN

  1. Create a Python file, e.g., OneDriveExample.py:
import pyodbc
conn = pyodbc.connect('DSN=API Driver - OneDrive')

cursor = conn.cursor()

# Execute query to fetch data
cursor.execute("SELECT * FROM Drives")

row = cursor.fetchone()
print("------------OneDrive & Python----------------")
while row:
    print(row)
    row = cursor.fetchone()
  1. When you run the code, it will call the API and retrieve data.

Using an Entire ODBC Connection String

To avoid multiple DSNs for each platform, copy the connection string directly:

  1. In ODBC Data Source, click Copy Settings.

  2. A confirmation window will appear.
    Connection String Copied

  3. In Python, use the connection string with pyodbc.connect():

conn = pyodbc.connect('DRIVER={ZappySys API Driver};ServiceUrl=https://yourservice.provider.com/api/xxxx....;AuthName=Http;')

Conclusion

Following this guide, you’ve connected Python with OneDrive using the OneDrive API. This integration allows tasks like uploading, downloading, and organizing files from your Python scripts. With Python’s flexibility and OneDrive’s storage, you can efficiently manage workflows and file operations. For help, contact the ZappySys support team.

References