OAuth を使用して、ユーザーアカウントで Databricks リソースへの対話型アクセスを承認します
このトピックでは、Databricks DatabricksCLIコマンドを対話形式で実行したり、 を呼び出したりするときに、 リソースへのアクセスを承認する手順と詳細について説明します。DatabricksRESTAPIs
Databricks は、UI の外部で Databricks リソースと対話する際のユーザー認証と認証の優先プロトコルとして OAuth を使用します。 Databricks は、OAuth の認証方法の一部として生成されるアクセス トークンの更新を自動化するための統合クライアント認証ツールも提供しています。
詳細については、「 Databricks リソースへのアクセスの承認」を参照してください。
Databricks リソースにアクセスする際の承認と認証にはどのようなオプションがありますか?
このトピックでは、 承認 とは、委任を通じて特定の Databricks リソースへのアクセスをネゴシエートするために使用されるプロトコル (OAuth) を指します。 認証 とは、資格情報が表現、送信、および検証されるメカニズムを指し、この場合は アクセストークン です。
Databricks は、 OAuth 2.0 ベースの認証を使用して、これらのリソースにアクセスする権限を持つユーザーに代わって、コマンド ラインまたはコードから Databricks アカウントとワークスペース リソースへのアクセスを有効にします。 ユーザーが最初にサインインして OAuth 認証要求に同意すると、参加しているツールまたは SDK に OAuth トークンが提供され、その時点からユーザーに代わってトークンベースの認証が実行されます。 OAuthアクセストークンの有効期間は1時間で、その後、関係するツールまたはSDKは、1時間有効な新しいトークンを取得するための自動バックグラウンド試行を行います。
Databricks では、OAuth を使用してユーザー アカウントのアクセスを承認する 2 つの方法がサポートされています。
- Databricks 統合クライアント認証サポートを使用して、ほとんどの場合自動的に行われます。 特定の Databricks SDK (Databricks Terraform SDK など) とツールを使用している場合は、この簡略化されたアプローチを使用します。 サポートされているツールと SDK については、「 Databricks 統合クライアント認証」を参照してください。
- 手動で、OAuth コード検証ツール/チャレンジのペアと認証コードを直接生成し、それらを使用して、構成で提供する初期 OAuth トークンを作成します。 Databricks 統合クライアント認証でサポートされている API を使用していない場合は、この方法を使用します。 詳細については、「 OAuth ユーザー間 (U2M) 認証用のアクセス トークンを手動で生成して使用する」を参照してください。
Databricks 統合クライアント認証による対話型ユーザー認証
認証の設定を開始する前に、ワークスペースオブジェクトに対して実行する特定のカテゴリの操作の ACL アクセス許可を確認し、アカウントに必要なアクセスレベルがあるかどうかを判断します。 詳細については、「 アクセス制御リスト」を参照してください。
Databricks SDK と、統合クライアント認証をサポートするツールを使用して OAuth 承認を実行するには、コード内に次のものを統合します。
- Environment
- Profile
- CLI
- Connect
- VS Code
- Terraform
- Python
- Java
- Go
To use environment variables for a specific Databricks authentication type with a tool or SDK, see Authorizing access to Databricks resources or the tool’s or SDK’s documentation. See also Environment variables and fields for client unified authentication and the Default methods for client unified authentication.
For account-level operations, set the following environment variables:
DATABRICKS_HOST
, set to the value of your Databricks account console URL,https://accounts.cloud.databricks.com
.DATABRICKS_ACCOUNT_ID
For workspace-level operations, set the following environment variables:
DATABRICKS_HOST
, set to the value of your Databricks workspace URL, for examplehttps://dbc-a1b2345c-d6e7.cloud.databricks.com
.
Create or identify a Databricks configuration profile with the following fields in your .databrickscfg
file. If you create the profile, replace the placeholders with the appropriate values. To use the profile with a tool or SDK, see Authorizing access to Databricks resources or the tool’s or SDK’s documentation. See also Environment variables and fields for client unified authentication and the Default methods for client unified authentication.
For account-level operations, set the following values in your .databrickscfg
file. In this case, the Databricks account console URL is https://accounts.cloud.databricks.com
:
[<some-unique-configuration-profile-name>]
host = <account-console-url>
account_id = <account-id>
For workspace-level operations, set the following values in your .databrickscfg
file. In this case, the host is the Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
:
[<some-unique-configuration-profile-name>]
host = <workspace-url>
For the Databricks CLI, run the databricks auth login
command with the following options:
- For Databricks account-level operations,
--host <account-console-url> --account-id <account-id>
. - For Databricks workspace-level operations,
--host <workspace-url>
.
After you run this command, follow the instructions in your web browser to log in to your Databricks account or workspace.
For more details, see OAuth authorization with the Databricks CLI.
OAuth U2M authentication is supported in the following Databricks Connect versions:
-
For Python, Databricks Connect for Databricks Runtime 13.1 and above.
-
For Scala, Databricks Connect for Databricks Runtime 13.3 LTS and above.
For Databricks Connect, you can do one of the following:
- Set the values in your
.databrickscfg
file for Databricks workspace-level operations as specified in this article’s “Profile” section. Also set thecluster_id
environment variable in your profile to your workspace instance URL, for examplehttps://dbc-a1b2345c-d6e7.cloud.databricks.com
. - Set the environment variables for Databricks workspace-level operations as specified in this article’s “Environment” section. Also set the
DATABRICKS_CLUSTER_ID
environment variable to your workspace instance URL, for examplehttps://dbc-a1b2345c-d6e7.cloud.databricks.com
.
Values in your .databrickscfg
file always take precedence over environment variables.
To initialize the Databricks Connect client with these environment variables or values in your .databrickscfg
file, see Compute configuration for Databricks Connect.
For the Databricks extension for Visual Studio Code, do the following:
- In the Configuration pane, click Configure Databricks.
- In the Command Palette, for Databricks Host, enter your workspace URL, for example
https://dbc-a1b2345c-d6e7.cloud.databricks.com
, and then pressEnter
. - Select OAuth (user to machine).
- Complete the on-screen instructions within your web browser to finish authenticating with your Databricks account and allowing all-apis access.
For more details, see OAuth U2M authorization with the Databricks CLI.
For both account-level and workspace-level operations, you must use the Databricks CLI to run the following command before you apply your Terraform configuration. This command instructs the Databricks CLI to generate and cache the necessary OAuth token in the path .databricks/token-cache.json
within your user’s home folder on your machine:
Configuring for Databricks account-level operations
databricks auth login --host <account-console-url> --account-id <account-id>
Replace the following placeholders:
-
Replace
<account-console-url>
with the valuehttps://accounts.cloud.databricks.com
. (Do not set this to the value of your Databricks workspace URL.) -
Replace
<account-id>
with the value of your Databricks account. See Locate your account ID.
If you have an existing Databricks configuration profile with the host
and account_id
fields already set, you can substitute --host <account-console-url> --account-id <account-id>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the account login URL and account ID as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Terraform code similar to one of the following snippets:
provider "databricks" {
alias = "account"
}
For direct configuration, set the provider
using the following configuration. Replace the retrieve-
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as HashiCorp Vault. See also Vault Provider). In this example, you can set account_id
to your Databricks account console URL.
provider "databricks" {
alias = "account"
host = <retrieve-account-console-url>
account_id = <retrieve-account-id>
}
Configuring for Databricks workspace-level operations
databricks auth login --host <workspace-url>
Replace the placeholder <workspace-url>
with the target Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
.
If you have an existing Databricks configuration profile with the host
field already set, you can substitute --host <workspace-url>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the workspace URL as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Terraform code similar to one of the following snippets:
provider "databricks" {
alias = "workspace"
}
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as HashiCorp Vault. See also Vault Provider). In this case, the host is the Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
:
provider "databricks" {
alias = "workspace"
host = <retrieve-workspace-url>
}
For more information about authenticating with the Databricks Terraform provider, see Authentication.
For both account-level and workspace-level operations, you must use the Databricks CLI to run the following command before you run your Python code. This command instructs the Databricks CLI to generate and cache the necessary OAuth token in the path .databricks/token-cache.json
within your user’s home folder on your machine:
Configuring for Databricks account-level operations
databricks auth login --host <account-console-url> --account-id <account-id>
Replace the following placeholders:
-
Replace
<account-console-url>
with the valuehttps://accounts.cloud.databricks.com
. (Do not set this to the value of your Databricks workspace URL.) -
Replace
<account-id>
with the value of your Databricks account. See Locate your account ID.
If you have an existing Databricks configuration profile with the host
and account_id
fields already set, you can substitute --host <account-console-url> --account-id <account-id>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the account login URL and account ID as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Python code similar to one of the following snippets:
For default authorization using Databricks unified client authentication:
from databricks.sdk import AccountClient
a = AccountClient()
# ...
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as AWS Systems Manager Parameter Store). In this case, the Databricks account console URL is https://accounts.cloud.databricks.com
:
from databricks.sdk import AccountClient
a = AccountClient(
host = retrieveAccountConsoleUrl(),
account_id = retrieveAccountId()
)
# ...
Configuring for Databricks workspace-level operations
databricks auth login --host <worskpace-url>
Replace the placeholder <workspace-url>
with the target Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
.
If you have an existing Databricks configuration profile with the host
field already set, you can substitute --host <workspace-url>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the workspace URL as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Python code similar to one of the following snippets:
For default authorization using Databricks unified client authentication:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
# ...
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as AWS Systems Manager Parameter Store). In this case, the host is the Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient(host = retrieveWorkspaceUrl())
# ...
For more information about authenticating with Databricks tools and SDKs that use Python and that implement Databricks client unified authentication, see:
For both account-level and workspace-level operations, you must use the Databricks CLI to run the following command before you run your Java code. This command instructs the Databricks CLI to generate and cache the necessary OAuth token in the path .databricks/token-cache.json
in your user’s home folder on your machine:
Configuring for Databricks account-level operations
databricks auth login --host <account-console-url> --account-id <account-id>
Replace the following placeholders:
-
Replace
<account-console-url>
with the valuehttps://accounts.cloud.databricks.com
. (Do not set this to the value of your Databricks workspace URL.) -
Replace
<account-id>
with the value of your Databricks account. See Locate your account ID.
If you have an existing Databricks configuration profile with the host
and account_id
fields already set, you can substitute --host <account-console-url> --account-id <account-id>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the account login URL and account ID as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Java code similar to one of the following snippets:
For default authorization using Databricks unified client authentication:
import com.databricks.sdk.AccountClient;
// ...
AccountClient a = new AccountClient();
// ...
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as AWS Systems Manager Parameter Store). In this case, the Databricks account console URL is https://accounts.cloud.databricks.com
:
import com.databricks.sdk.AccountClient;
import com.databricks.sdk.core.DatabricksConfig;
// ...
DatabricksConfig cfg = new DatabricksConfig()
.setHost(retrieveAccountConsoleUrl())
.setAccountId(retrieveAccountId());
AccountClient a = new AccountClient(cfg);
// ...
Configuring for Databricks workspace-level operations
For workspace-level operations, you should first use the Databricks CLI to run the following command before you run your Java code. This command instructs the Databricks CLI to generate and cache the necessary OAuth token in the path .databricks/token-cache.json
within your user’s home folder on your machine:
databricks auth login --host <worskpace-url>
Replace the placeholder <workspace-url>
with the target Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
.
If you have an existing Databricks configuration profile with the host
field already set, you can substitute --host <workspace-url>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the workspace URL as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Java code similar to one of the following snippets:
For default authorization using Databricks unified client authentication:
import com.databricks.sdk.WorkspaceClient;
// ...
WorkspaceClient w = new WorkspaceClient();
// ...
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as AWS Systems Manager Parameter Store). In this case, the host is the Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
:
import com.databricks.sdk.WorkspaceClient;
import com.databricks.sdk.core.DatabricksConfig;
// ...
DatabricksConfig cfg = new DatabricksConfig()
.setHost(retrieveWorkspaceUrl())
WorkspaceClient w = new WorkspaceClient(cfg);
// ...
For more information about authorizing and authenticating with Databricks tools and SDKs that use Java and that implement Databricks client unified authentication, see:
- Set up the Databricks Connect client for Scala (the Databricks Connect client for Scala uses the included Databricks SDK for Java for authentication)
- Authenticate the Databricks SDK for Java with your Databricks account or workspace
For both account-level and workspace-level operations, you must use the Databricks CLI to run the following command before you run your Go code. This command instructs the Databricks CLI to generate and cache the necessary OAuth token in the path .databricks/token-cache.json
within your user’s home folder on your machine:
Configuring for Databricks account-level operations
databricks auth login --host <account-login-url> --account-id <account-id>
Replace the following placeholders:
-
Replace
<account-console-url>
with the valuehttps://accounts.cloud.databricks.com
. (Do not set this to the value of your Databricks workspace URL.) -
Replace
<account-id>
with the value of your Databricks account. See Locate your account ID.
If you have an existing Databricks configuration profile with the host
and account_id
fields already set, you can substitute --host <account-console-url> --account-id <account-id>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the account login URL and account ID as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Go code similar to one of the following snippets:
For default authorization using Databricks unified client authentication:
import (
"github.com/databricks/databricks-sdk-go"
)
// ...
a := databricks.Must(databricks.NewAccountClient())
// ...
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as AWS Systems Manager Parameter Store). In this case, the Databricks account console URL is https://accounts.cloud.databricks.com
:
import (
"github.com/databricks/databricks-sdk-go"
)
// ...
a := databricks.Must(databricks.NewAccountClient(&databricks.Config{
Host: retrieveAccountConsoleUrl(),
AccountId: retrieveAccountId(),
}))
// ...
Configuring for Databricks workspace-level operations
For workspace-level operations, you should first use the Databricks CLI to run the following command, before you run your Go code. This command instructs the Databricks CLI to generate and cache the necessary OAuth token in the path .databricks/token-cache.json
within your user’s home folder on your machine:
databricks auth login --host <worskpace-url>
Replace the placeholder <workspace-url>
with the target Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
.
If you have an existing Databricks configuration profile with the host
field already set, you can substitute --host <workspace-url>
with --profile <profile-name>
.
After you run the auth login
command, you are prompted to save the workspace URL as a Databricks configuration profile. When prompted, enter the name of a new or existing profile in your .databrickscfg
file. Any existing profile with the same name in your .databrickscfg
file is overwritten.
If prompted, complete your web browser’s on-screen instructions to complete the login. Then use Go code similar to one of the following snippets:
import (
"github.com/databricks/databricks-sdk-go"
)
// ...
w := databricks.Must(databricks.NewWorkspaceClient())
// ...
For direct configuration (replace the retrieve
placeholders with your own implementation to retrieve the values from the console or some other configuration store, such as AWS Systems Manager Parameter Store). In this case, the host is the Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
:
import (
"github.com/databricks/databricks-sdk-go"
)
// ...
w := databricks.Must(databricks.NewWorkspaceClient(&databricks.Config{
Host: retrieveWorkspaceUrl(),
}))
// ...
For more information about authenticating with Databricks tools and SDKs that use Go and that implement Databricks client unified authentication, see Authenticate the Databricks SDK for Go with your Databricks account or workspace.
OAuth ユーザーツーマシン (U2M) 認証用のアクセストークンを手動で生成して使用する
このセクションは、 Databricks クライアントの統合認証 標準で動作しないサードパーティのツールまたはサービスを使用しているユーザーを対象としています。
何らかの理由で OAuth U2M 認証のために Databricks OAuth アクセス トークンを手動で生成、更新、または使用する必要がある場合は、このセクションの指示に従ってください。
ステップ 1: OAuth コード検証ツールとコード チャレンジのペアを生成する
OAuth U2M 認証用のアクセス・トークンを手動で生成して使用するには、まず OAuth コード検証ツールと 、そのコード検証ツールから派生した OAuth コード・チャレンジ が必要です。 ステップ 2 のコード チャレンジを使用して、OAuth 認証コードを生成します。 ステップ 3 のコード検証ツールと認証コードを使用して、OAuth アクセス トークンを生成します。
技術的には、コード検証ツールとコード チャレンジにエンコードされていないプレーンテキスト文字列を使用することは可能ですが、Databricks では、 代わりに OAuth 標準に従ってコード検証ツールとコード チャレンジを生成する ことを強くお勧めします。
具体的には、コードベリファイアは、セットA-Z
、 a-z
、 0-9
および句読点文字-._~
(ハイフン、ピリオド、アンダースコア、チルダ) の文字を使用した、43 ~ 128 の暗号学的にランダムな文字列である必要があります。文字が長い。 コード チャレンジは、コード ベリファイアの SHA256 ハッシュを Base64 URL でエンコードした文字列である必要があります。 詳細については、 「認可リクエスト」を参照してください。
次の Python スクリプトを実行すると、一意のコードベリファイアとコード チャレンジのペアをすばやく生成できます。 この生成されたコード ベリファイアとコード チャレンジのペアは複数回再利用できますが、Databricks では、OAuth U2M 認証のアクセス許可を手動で生成するたびに、新しいコード ベリファイアとコード チャレンジのペアを生成することをお勧めします。
import uuid, hashlib, base64
# Generate a UUID.
uuid1 = uuid.uuid4()
# Convert the UUID to a string.
uuid_str1 = str(uuid1).upper()
# Create the code verifier.
code_verifier = uuid_str1 + "-" + uuid_str1
# Create the code challenge based on the code verifier.
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode('utf-8')
# Remove all padding from the code challenge.
code_challenge = code_challenge.replace('=', '')
# Print the code verifier and the code challenge.
# Use these in your calls to manually generate
# access tokens for OAuth U2M authentication.
print(f"code_verifier: {code_verifier}")
print(f"code_challenge: {code_challenge}")
ステップ 2: 認証コードを生成する
OAuth 認可コードを使用して、Databricks OAuth アクセストークンを生成します。 認可コードは、Databricks OAuth アクセストークンの生成に使用した直後に期限切れになります。 認可コードのスコープは、生成元のレベルによって異なります。 次のように、Databricks アカウント レベルまたはワークスペース レベルで認可コードを生成できます。
- ユーザー アカウントがアクセスできるアカウントとワークスペース内のアカウントレベル と ワークスペースレベルのを呼び出すには、 アカウントレベルで認証コードを生成しますRESTAPIs Databricks。
- ユーザー アカウントがアクセスできる 1 つのワークスペース内でのみ REST APIs を呼び出すには、そのワークスペースのみの ワークスペース レベルで認証コードを生成できます 。
アカウントレベルの認証コードを生成する
-
アカウント管理者として、アカウントコンソールにログインします。
-
右上のユーザー名の横にある下向きの矢印をクリックします。
-
アカウントIDを コピーしてください。
-
Web ブラウザーのアドレス バーで、次の URL を参照します。 読みやすくするために改行が追加されました。 URL にこれらの改行を含めることはできません。
次のURLで、次のように置き換えます。
<account-id>
をコピーした アカウントID に置き換えてください。<redirect-url>
は、ローカルマシンへのリダイレクト URL、たとえばhttp://localhost:8020
に置き換えてください。<state>
認可コードの整合性を検証するために使用できるプレーンテキスト文字列に置き換えます。<code-challenge>
をステップ 1 で生成したコードチャレンジに置き換えます。
https://accounts.cloud.databricks.com/oidc/accounts/<account-id>/v1/authorize
?client_id=databricks-cli
&redirect_uri=<redirect-url>
&response_type=code
&state=<state>
&code_challenge=<code-challenge>
&code_challenge_method=S256
&scope=all-apis+offline_access -
プロンプトが表示されたら、画面の指示に従って Databricks アカウントにログインします。
-
Web ブラウザーのアドレスバーで、認可コードをコピーします。 認可コードは、URL 内の
code=
と&
文字の間の完全な文字列です。 たとえば、次の URL の認可コードはdcod...7fe6
です。http://localhost:8020/?code=dcod...7fe6&state=<state>
この応答 URL の
<state>
値が、要求 URL で指定したstate
値と一致することを視覚的に確認して、この承認コードの整合性を確認する必要があります。 値が異なる場合は、侵害される可能性があるため、この認可コードを使用しないでください。 -
「アカウントレベルのアクセストークンを生成する」に進んでください。
ワークスペース レベルの認証コードを生成する
-
Web ブラウザーのアドレス バーで、次の URL を参照します。 読みやすくするために改行が追加されました。 URL にこれらの改行を含めることはできません。
次のURLで、次のように置き換えます。
<databricks-instance>
を Databricks ワークスペース インスタンス名 (例:dbc-a1b2345c-d6e7.cloud.databricks.com
) に置き換えます。<redirect-url>
は、ローカルマシンへのリダイレクト URL、たとえばhttp://localhost:8020
に置き換えてください。<state>
認可コードの整合性を検証するために使用できるプレーンテキスト文字列に置き換えます。<code-challenge>
をステップ 1 で生成したコードチャレンジに置き換えます。
https://<databricks-instance>/oidc/v1/authorize
?client_id=databricks-cli
&redirect_uri=<redirect-url>
&response_type=code
&state=<state>
&code_challenge=<code-challenge>
&code_challenge_method=S256
&scope=all-apis+offline_access -
プロンプトが表示されたら、画面の指示に従って Databricks ワークスペースにログインします。
-
Web ブラウザーのアドレスバーで、認可コードをコピーします。 認可コードは、URL 内の
code=
と&
文字の間の完全な文字列です。 たとえば、次の URL の認可コードはdcod...7fe6
です。http://localhost:8020/?code=dcod...7fe6&state=<state>
この応答 URL の
<state>
値が、要求 URL で指定したstate
値と一致することを視覚的に確認して、この承認コードの整合性を確認する必要があります。 値が異なる場合は、侵害される可能性があるため、この認可コードを使用しないでください。
ステップ 3: 認証コードを使用して OAuth アクセストークンを生成する
次のように、前のステップの OAuth 認可コードを使用して、Databricks OAuth アクセストークンを生成します。
- user アカウントがアクセスできるアカウントとワークスペース内の アカウントレベル とワークスペースRESTAPIs レベルのDatabricksを呼び出すには、アカウントレベルの認証コードを使用して、 アカウントレベルでアクセストークンを生成します 。
- ユーザーアカウントがアクセスできる 1 つのワークスペース内でのみ REST APIs を呼び出すには、ワークスペースレベルの認証コードを使用して、そのワークスペースのみの ワークスペースレベルでアクセストークンを生成できます 。
アカウントレベルのアクセストークンを生成する
-
curl
などのクライアントをアカウント レベルの認可コードとともに使用して、アカウント レベルの OAuth アクセスを生成します。 次のcurl
呼び出しで、次のプレースホルダーを置き換えます。<account-id>
をステップ 2 の アカウント ID に置き換えます。<redirect-url>
をステップ2のリダイレクトURLに置き換えてください。<code-verifier>
ステップ 1 で生成したコードベリファイアに置き換えます。<authorization-code>
を、ステップ 2 で生成したアカウントレベルの認可コードに置き換えます。
curl --request POST \
https://accounts.cloud.databricks.com/oidc/accounts/<account-id>/v1/token \
--data "client_id=databricks-cli" \
--data "grant_type=authorization_code" \
--data "scope=all-apis offline_access" \
--data "redirect_uri=<redirect-url>" \
--data "code_verifier=<code-verifier>" \
--data "code=<authorization-code>" -
レスポンス内の、アカウント レベルの OAuth アクセストークンをコピーします。 アクセストークンは、
access_token
オブジェクト内の完全な文字列です。 たとえば、次のレスポンスのアクセストークンはeyJr...Dkag
です。JSON{
"access_token": "eyJr...Dkag",
"refresh_token": "doau...f26e",
"scope": "all-apis offline_access",
"token_type": "Bearer",
"expires_in": 3600
}このアクセストークンは 1 時間で有効期限が切れます。 新しいアクセストークンを生成するには、この手順をステップ 1 から繰り返します。
-
「手順 4: Databricks REST API を呼び出す」に進んでください。
ワークスペース レベルのアクセス トークンを生成する
-
curl
などのクライアントをワークスペース レベルの認可コードとともに使用して、ワークスペース レベルの OAuth アクセストークンを生成します。 次のcurl
呼び出しで、次のプレースホルダーを置き換えます。<databricks-instance>
を Databricks ワークスペース インスタンス名 (例:dbc-a1b2345c-d6e7.cloud.databricks.com
) に置き換えます。<redirect-url>
をステップ2のリダイレクトURLに置き換えてください。<code-verifier>
ステップ 1 で生成したコードベリファイアに置き換えます。<authorization-code>
を、ステップ 2 で生成したワークスペース レベルの認可コードに置き換えます。
Bashcurl --request POST \
https://<databricks-instance>/oidc/v1/token \
--data "client_id=databricks-cli" \
--data "grant_type=authorization_code" \
--data "scope=all-apis offline_access" \
--data "redirect_uri=<redirect-url>" \
--data "code_verifier=<code-verifier>" \
--data "code=<authorization-code>" -
レスポンス内の、ワークスペース レベルの OAuth アクセストークンをコピーします。 アクセストークンは、
access_token
オブジェクト内の完全な文字列です。 たとえば、次のレスポンスのアクセストークンはeyJr...Dkag
です。JSON{
"access_token": "eyJr...Dkag",
"refresh_token": "doau...f26e",
"scope": "all-apis offline_access",
"token_type": "Bearer",
"expires_in": 3600
}このアクセストークンは 1 時間で有効期限が切れます。 新しいアクセストークンを生成するには、この手順をステップ 1 から繰り返します。
手順 4: Databricks REST API を呼び出す
アカウントレベルまたはワークスペースレベルの OAuth アクセストークンを使用して Databricks アクセストークンのスコープに応じて、アカウントレベルの REST APIs およびワークスペースレベルの REST APIsに対して認証します。 Databricks ユーザー アカウントは、アカウント レベルの REST APIsを呼び出すには、アカウント管理者である必要があります。
アカウントレベルの REST API リクエストの例
この例では、 curl
と Bearer
認証を使用して、アカウントに関連付けられているすべてのワークスペースの一覧を取得します。
<oauth-access-token>
をアカウントレベルの OAuth アクセストークンに置き換えます。<account-id>
をアカウントIDに置き換えます。
export OAUTH_TOKEN=<oauth-access-token>
curl --request GET --header "Authorization: Bearer $OAUTH_TOKEN" \
"https://accounts.cloud.databricks.com/api/2.0/accounts/<account-id>/workspaces"
ワークスペース レベルの REST API 要求の例
この例では、 curl
と Bearer
認証を使用して、指定したワークスペースで使用可能なすべてのクラスターを一覧表示します。
<oauth-access-token>
をアカウントレベルまたはワークスペースレベルの OAuth アクセストークンに置き換えます。<databricks-instance>
を Databricks ワークスペース インスタンス名 (例:dbc-a1b2345c-d6e7.cloud.databricks.com
) に置き換えます。
export OAUTH_TOKEN=<oauth-access-token>
curl --request GET --header "Authorization: Bearer $OAUTH_TOKEN" \
"https://<databricks-instance>/api/2.0/clusters/list"