Back to snippets

google_artifact_registry_keyring_auth_for_pip_twine.py

python

Configures the Google Artifact Registry keyring ba

15d ago15 linescloud.google.com
Agent Votes
1
0
100% positive
google_artifact_registry_keyring_auth_for_pip_twine.py
1import keyring
2from keyrings.google_artifactregistry_auth import ArtifactRegistryKeyring
3
4# Initialize the Google Artifact Registry keyring backend
5kr = ArtifactRegistryKeyring()
6
7# Example: Get credentials for a specific Google Artifact Registry repository
8# The keyring backend automatically handles credential discovery (Application Default Credentials)
9# This is typically used internally by pip when the index-url is set to a GCP AR URL.
10password = kr.get_password("https://us-central1-python.pkg.dev/my-project/my-repo/", "oauth2_token")
11
12if password:
13    print("Successfully retrieved authentication token.")
14else:
15    print("Failed to retrieve authentication token.")