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