Skip to content

Commit d350434

Browse files
committed
Better examples
1 parent ff329e7 commit d350434

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

examples/external_browser_auth.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,45 @@ def run(host: str, client_id: Optional[str], client_secret: Optional[str], azure
1919
print(me)
2020

2121

22+
def register_custom_app() -> tuple[str, str]:
23+
"""Creates new Custom OAuth App in Databricks Account"""
24+
logging.info("No OAuth custom app client/secret provided, creating new app")
25+
26+
from databricks.sdk import AccountClient
27+
28+
account_client = AccountClient()
29+
30+
custom_app = account_client.custom_app_integration.create(
31+
name="external-browser-demo",
32+
redirect_urls=[
33+
f"http://localhost:8020",
34+
],
35+
confidential=True,
36+
scopes=["all-apis"],
37+
)
38+
logging.info(f"Created new custom app: "
39+
f"--client_id {custom_app.client_id} "
40+
f"--client_secret {custom_app.client_secret}")
41+
42+
return custom_app.client_id, custom_app.client_secret
43+
44+
2245
if __name__ == "__main__":
2346
parser = argparse.ArgumentParser()
2447
parser.add_argument("--host", help="Databricks host", required=True)
2548
parser.add_argument("--client_id", help="Databricks client_id", default=None)
2649
parser.add_argument("--azure_client_id", help="Databricks azure_client_id", default=None)
2750
parser.add_argument("--client_secret", help="Databricks client_secret", default=None)
2851
parser.add_argument("--azure_client_secret", help="Databricks azure_client_secret", default=None)
52+
parser.add_argument("--register-custom-app", action="store_true", help="Register a new custom app")
2953
namespace = parser.parse_args()
30-
run(namespace.host, namespace.client_id, namespace.client_secret, namespace.azure_client_id, namespace.azure_client_secret)
54+
if namespace.register_custom_app and (namespace.client_id is not None or namespace.azure_client_id is not None):
55+
raise ValueError("Cannot register custom app and provide --client_id/--azure_client_id at the same time")
56+
if not namespace.register_custom_app and namespace.client_id is None and namespace.azure_client_secret is None:
57+
raise ValueError("Must provide --client_id/--azure_client_id or register a custom app")
58+
if namespace.register_custom_app:
59+
client_id, client_secret = register_custom_app()
60+
else:
61+
client_id, client_secret = namespace.client_id, namespace.client_secret
62+
63+
run(namespace.host, client_id, client_secret, namespace.azure_client_id, namespace.azure_client_secret)

examples/flask_app_with_oauth.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import logging
3232
import sys
3333

34-
from databricks.sdk.oauth import OAuthClient, OidcEndpoints, get_workspace_endpoints
34+
from databricks.sdk.oauth import OAuthClient, get_workspace_endpoints
3535
from databricks.sdk.service.compute import ListClustersFilterBy, State
3636

3737
APP_NAME = "flask-demo"
@@ -114,7 +114,11 @@ def register_custom_app(args: argparse.Namespace) -> tuple[str, str]:
114114
account_client = AccountClient(profile=args.profile)
115115

116116
custom_app = account_client.custom_app_integration.create(
117-
name=APP_NAME, redirect_urls=[f"http://localhost:{args.port}/callback"], confidential=True,
117+
name=APP_NAME,
118+
redirect_urls=[
119+
f"http://localhost:{args.port}/callback",
120+
],
121+
confidential=True,
118122
scopes=["all-apis"],
119123
)
120124
logging.info(f"Created new custom app: "

0 commit comments

Comments
 (0)