ワークスペース用のアクセス トークンを使用する
Access tokens for a workspace are a premium feature. To learn about the Bitbucket Cloud Premium plan, visit: Bitbucket Cloud Premium.
Access tokens for a workspace are single-purpose access tokens with reduced access (specified during creation) that can be useful for scripting, CI/CD tools, and testing Bitbucket-connected apps during development. Access tokens created within a workspace are tied to that workspace, not a user, and are managed by the workspace’s admins.
次の例は、Bitbucket Cloud REST API と Git コマンド ライン インターフェイス (Git CLI) を使用して、ワークスペースで Bitbucket Cloud アクセス トークンを使用する方法を示しています。
Bitbucket API でワークスペースのアクセス トークンを使用する
We recommend Bitbucket Cloud integration or app developers use OAuth for user authentication. For information on creating a Bitbucket Cloud integration or app with OAuth support, visit Atlassian Developer - Bitbucket Cloud.
The following example shows how to use a Bitbucket Cloud access token for a workspace with the curl command as a guide for how to authenticate with Bitbucket Cloud APIs. This example is querying the commits on a Bitbucket repository using the Get Repository API.
To connect to Bitbucket Cloud, send the access token as a bearer token in an HTTP Authorization header. For example:
curl --request GET \
--url 'https://api.bitbucket.org/2.0/repositories/{workspace}/{repository}' \
--header 'Authorization: Bearer <workspace_access_token>' \
--header 'Accept: application/json'Git コマンドライン インターフェイスでワークスペースのアクセス トークンを使用する
Access tokens for a workspace should be used with the Git command-line interface in programs and scripts that are non-interactive, such as build tools, automation scripts, and CI/CD applications. When using Git daily, we recommend connecting to Bitbucket Cloud using SSH keys or Git Credential Manager to avoid manually entering an access token for a workspace every time you issue a command that interacts with Bitbucket. For details on setting up SSH keys for Bitbucket, see: Set up SSH keys for Bitbucket Cloud.
Git CLI でワークスペースの Bitbucket Cloud アクセス トークンを使用するには、次の権限を持つアクセス トークンを作成します。
リポジトリ読み取り (
repository)リポジトリ書き込み (
repository:write)
Git CLI でワークスペースのアクセス トークンを使用するには、インタラクティブ プロンプトからアクセス トークンを提供するか、URL にリポジトリのアクセス トークンを含めるかの 2 つのオプションがあります。
インタラクティブ パスワード プロンプトによるワークスペースのアクセス トークン
この方法では、ワークスペースのアクセス トークンが保護されずに URL に保存されるのを防ぎます。Git が Bitbucket Cloud とやり取り (git pull、git push、git fetch など) するたびに、ワークスペースレベルのアクセス トークンの入力を要求します。また、リポジトリの Git ユーザー構成も必要です。
インタラクティブ プロンプトを介してワークスペースのアクセス トークンを提供するには、次の手順に従います。
次のコマンドでリポジトリを複製します。
git clone https://x-token-auth@bitbucket.org/{workspace}/{repository}.gitローカルデバイスにすでに複製されているリポジトリの場合は、次のコマンドでリモート URL をアップデートします。
git remote set-url origin https://x-token-auth@bitbucket.org/{workspace}/{repository}.git新たに複製されたリポジトリに移動します。
cd {repository}/次のように、リポジトリの Git ユーザーを設定します。
git config user.email "{bot_email}"{bot_email}とは、ワークスペースレベルのアクセス トークンの作成時に生成されたワークスペース メールのアクセス トークンを指します (例:52c16467c5f19101ff2061cc@bots.bitbucket.org)。
ワークスペース メールのアクセス トークン:
アクセス トークンの名前ではありません。
リポジトリのワークスペースの「アクセス トークン」ページで、アクセス トークンの名前 を選択することで取得できます。
メールの送受信はできません。Git 操作をアクセス トークンに一致させるためだけに使用されます。
URL にワークスペースのアクセス トークンを含める
アクセス トークンをプレーン テキストとして保護しないで保存したり、Git リモート URL の一部として永続的に保存したりしないことをお勧めします。この方法は、アクセス トークンがビルド ツールの「秘密」変数として安全に保存されている場合に役立ちます。
インタラクティブ パスワード プロンプトなしでワークスペースのアクセス トークンを使用するために、アクセス トークンを URL に含めることができます。次に例を示します。
次のコマンドでリポジトリを複製します。
git clone https://x-token-auth:{workspace_access_token}@bitbucket.org/{workspace}/{repository}.gitローカルデバイスにすでに複製されているリポジトリの場合は、次のコマンドでリモート URL をアップデートします。
git remote set-url origin https://x-token-auth:{workspace_access_token}@bitbucket.org/{workspace}/{repository}.git新たに複製されたリポジトリに移動します。
cd {repository}/次のように、リポジトリの Git ユーザーを設定します。
git config user.email "{botid}@bots.bitbucket.org"{botid}とはワークスペースのアクセス トークンを作成したときに生成されたアクセス トークン ID を指します。ワークスペースレベルのアクセス トークンの ID は、リポジトリレベルのアクセス トークンの名前ではありません。
この内容はお役に立ちましたか?