# How to connect with an OAuth data source in Azure Databricks

**URL:** <https://community.cdata.com/t/how-to-connect-with-an-oauth-data-source-in-azure-databricks/382>\
**Category:** Data Sources\
**Tags:** article, oauth, jdbc, azure-databricks\
**Created:** [September 7, 2023, 2:10pm UTC](https://community.cdata.com/t/how-to-connect-with-an-oauth-data-source-in-azure-databricks/382 "2023-09-07T14:10:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hesjona\_H](https://avatars.discourse-cdn.com/v4/letter/h/0ea827/32.png) [@Hesjona\_H](https://community.cdata.com/u/Hesjona_H)\
**Post date:** [September 7, 2023, 2:10pm UTC](https://community.cdata.com/t/how-to-connect-with-an-oauth-data-source-in-azure-databricks/382/1 "2023-09-07T14:10:25Z")

</div>

This is a simple guide on connecting with an OAuth data source in Azure Databricks via the JDBC driver (CData JDBC Driver for Salesforce is used as an example). For a comprehensive guide on utilizing the CData JDBC driver in Azure Databricks, please refer to our Knowledge Base article linked here: [Process & Analyze Salesforce Data in Azure Databricks (cdata.com)](https://www.cdata.com/kb/tech/salesforce-jdbc-azure-databricks.rst)

To configure the driver to use OAuth from Databricks, you should first generate the _OAuthSettings.txt_ file on a device that supports an internet browser by using the headless machine method. This is touched on in the driver’s documentation, in “_Establishing a Connection_” section and “_Headless Machines_” subsection (it is recommended to use “_Option 2: Transfer OAuth Settings_”): [https://cdn.cdata.com/help/RFJ/jdbc/pg\_connectionj.htm#headless-machines:~:text=it%20has%20expired.-,Headless%20Machines,-To%20configure%20the](https://cdn.cdata.com/help/RFJ/jdbc/pg_connectionj.htm#headless-machines:~:text=it%20has%20expired.-,Headless%20Machines,-To%20configure%20the)

After generating the _OAuthSetings.txt_ file, you will need to upload it to a valid path in Databricks. You can use the _FileStore_ folder within DBFS. To obtain the file path of the uploaded file in Databricks (required for the OAuthSettingsLocation property), click on the triangle icon of the _OAuthSettings.txt_ file \> select Copy path \> copy _File API Format_ path:

 ![65eec4e1-053a-4aa3-8431-4551ac41810f.png](https://us1.discourse-cdn.com/cdata/original/1X/fe6c3f7b9b4c3606ce240f0c33a67b025f0c1f76.png)

_Please ensure to copy the File API Format path – it allows you to read and write in the file__._ _If the Spark API Format is used, the OAuthSettings.txt won’t be updated when the token is refreshed._

Afterward, in your Databricks notebook’s connection string, assign the OAuthSettingsLocation property to the copied File API Format path, and configure the remaining connection properties accordingly to establish a connection with your data source. Below is the Python code to connect to Salesforce through OAuth authentication from Databricks:

#1. Connection information

OAuthClientId = “\<Your\_ClientId\>”

OAuthClientSecret = “\<Your\_ClientSecret\>”

RTK = “\<Your\_RTK\>”

OAuthSettingsLocation\_fileAPIFormat = “/dbfs/FileStore/OAuthSettings.txt”;

driver = “cdata.jdbc.salesforce.SalesforceDriver”

connection\_string= f"jdbc:salesforce:AuthScheme=OAuth;InitiateOAuth=REFRESH;OAuthClientId={OAuthClientId};OAuthClientSecret={OAuthClientSecret};OAuthSettingsLocation={OAuthSettingsLocation\_fileAPIFormat};RTK={RTK};"

#2. Reading the data

remote\_table = (

spark.read.format(“jdbc”) \

.option(“driver”, driver)

.option(“url”, connection\_string)

.option(“dbtable”, “sys\_tables”)

.load()

)

#3. Querying the data

display(remote\_table.select(“tableName”))

Please reach out to [support@cdata.com](mailto:support@cdata.com) if you run into any issues.
