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