Ready to get started?

Connect to live data from Harvest with the API Driver

Connect to Harvest

Using the CData ODBC Driver for Harvest in PyCharm



Connect to Harvest as an ODBC data source in PyCharm using the CData ODBC Driver for Harvest.

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 Harvest 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 Harvest as well as PyCharm.

Add Pyodbc to the Project

Follow the steps below to add the pyodbc module to your project.

  1. Click File -> Settings to open the project settings window.
  2. Click Project Interpreter from the Project: YourProjectName menu.
  3. To add pyodbc, click the + button and enter pyodbc.
  4. Click Install Package to install pyodbc.

Connect to Harvest

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 Harvest Profile on disk (e.g. C:\profiles\Harvest.apip). Next, set the ProfileSettings connection property to the connection string for Harvest (see below).

Harvest API Profile Settings

To authenticate to Harvest, you can use either Token authentication or the OAuth standard. Use Basic authentication to connect to your own data. Use OAuth to allow other users to connect to their data.

Using Token Authentication

To use Token Authentication, set the APIKey to your Harvest Personal Access Token in the ProfileSettings connection property. In addition to APIKey, set your AccountId in ProfileSettings to connect.

Using OAuth Authentication

First, register an OAuth2 application with Harvest. The application can be created from the "Developers" section of Harvest ID.

After setting the following connection properties, you are ready to connect:

  • ProfileSettings: Set your AccountId in ProfileSettings.
  • AuthScheme: Set this to OAuth.
  • OAuthClientId: Set this to the client ID that you specified in your app settings.
  • OAuthClientSecret: Set this to the client secret that you specified in your app settings.
  • CallbackURL: Set this to the Redirect URI that you specified in your app settings.
  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage how the driver obtains and refreshes the OAuthAccessToken.

Below is the syntax for a DSN:

[CData API Source] Driver = CData ODBC Driver for Harvest Description = My Description Profile = C:\profiles\Harvest.apip ProfileSettings = 'APIKey = my_personal_key AccountId = _your_account_id'

Execute SQL to Harvest

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\Harvest.apip;ProfileSettings = 'APIKey = my_personal_key;AccountId = _your_account_id';') cursor = cnxn.cursor() cursor.execute("SELECT Id, ClientName FROM Invoices WHERE State = 'open'") rows = cursor.fetchall() for row in rows: print(row.Id, row.ClientName)

After connecting to Harvest in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to Harvest data as if it were a standard database. If you have any questions, comments, or feedback regarding this tutorial, please contact us at support@cdata.com.