Skip to content

Commit 06267ba

Browse files
jackyhu-dbsaishreeeee
authored andcommitted
Add OAuth M2M example (#266)
* Add OAuth M2M example Signed-off-by: Jacky Hu <jacky.hu@databricks.com> Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent 36850e7 commit 06267ba

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ To run all of these examples you can clone the entire repository to your disk. O
3333
- **`insert_data.py`** adds a tables called `squares` to your default catalog and inserts one hundred rows of example data. Then it fetches this data and prints it to the screen.
3434
- **`query_cancel.py`** shows how to cancel a query assuming that you can access the `Cursor` executing that query from a different thread. This is necessary because `databricks-sql-connector` does not yet implement an asynchronous API; calling `.execute()` blocks the current thread until execution completes. Therefore, the connector can't cancel queries from the same thread where they began.
3535
- **`interactive_oauth.py`** shows the simplest example of authenticating by OAuth (no need for a PAT generated in the DBSQL UI) while Bring Your Own IDP is in public preview. When you run the script it will open a browser window so you can authenticate. Afterward, the script fetches some sample data from Databricks and prints it to the screen. For this script, the OAuth token is not persisted which means you need to authenticate every time you run the script.
36+
- **`m2m_oauth.py`** shows the simplest example of authenticating by using OAuth M2M (machine-to-machine) for service principal.
3637
- **`persistent_oauth.py`** shows a more advanced example of authenticating by OAuth while Bring Your Own IDP is in public preview. In this case, it shows how to use a sublcass of `OAuthPersistence` to reuse an OAuth token across script executions.
3738
- **`set_user_agent.py`** shows how to customize the user agent header used for Thrift commands. In
3839
this example the string `ExamplePartnerTag` will be added to the the user agent on every request.

examples/m2m_oauth.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
3+
from databricks.sdk.core import oauth_service_principal, Config
4+
from databricks import sql
5+
6+
"""
7+
This example shows how to use OAuth M2M (machine-to-machine) for service principal
8+
9+
Pre-requisites:
10+
- Create service principal and OAuth secret in Account Console
11+
- Assign the service principal to the workspace
12+
13+
See more https://docs.databricks.com/en/dev-tools/authentication-oauth.html)
14+
"""
15+
16+
server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME")
17+
18+
19+
def credential_provider():
20+
config = Config(
21+
host=f"https://{server_hostname}",
22+
# Service Principal UUID
23+
client_id=os.getenv("DATABRICKS_CLIENT_ID"),
24+
# Service Principal Secret
25+
client_secret=os.getenv("DATABRICKS_CLIENT_SECRET"))
26+
return oauth_service_principal(config)
27+
28+
29+
with sql.connect(
30+
server_hostname=server_hostname,
31+
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
32+
credentials_provider=credential_provider) as connection:
33+
for x in range(1, 100):
34+
cursor = connection.cursor()
35+
cursor.execute('SELECT 1+1')
36+
result = cursor.fetchall()
37+
for row in result:
38+
print(row)
39+
cursor.close()
40+
41+
connection.close()

0 commit comments

Comments
 (0)