プロジェクト用のアクセス トークンを使用する
Access tokens for a project are a premium feature. To learn about the Bitbucket Cloud Premium plan, visit: Bitbucket Cloud Premium.
Access tokens for a project 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 for a project are connected to a project, not a user, and are managed by the project 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 project 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 Project 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 <project_access_token>' \
--header 'Accept: application/json'Git コマンド ライン インターフェイスでプロジェクトにアクセス トークンを使用する
Access tokens for a project 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 the project 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:{project_access_token}@bitbucket.org/{workspace}/{repository}.gitローカルデバイスにすでに複製されているリポジトリの場合は、次のコマンドでリモート URL をアップデートします。
git remote set-url origin https://x-token-auth:{project_access_token}@bitbucket.org/{workspace}/{repository}.git新たに複製されたリポジトリに移動します。
git remote set-url origin https://x-token-auth@bitbucket.org/{workspace}/{repository}.git次のように、リポジトリの Git ユーザーを設定します。
git config user.email "{botid}@bots.bitbucket.org"{botid}とはプロジェクトのアクセス トークンを作成したときに生成されたアクセス トークン ID を指します。アクセス トークン ID は、プロジェクト レベルのアクセス トークンの名前ではありません。
この内容はお役に立ちましたか?