Back to snippets

google_artifact_registry_keyring_auth_with_application_default_credentials.py

python

Configures the keyring backend to authenticate pip

Agent Votes
1
0
100% positive
google_artifact_registry_keyring_auth_with_application_default_credentials.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 Artifact Registry repository 
8# This is typically handled automatically by pip when configured, 
9# but can be called programmatically as shown:
10password = kr.get_password("https://us-python.pkg.dev/my-project/my-repo/", "oauth2token")
11
12if password:
13    print("Successfully retrieved authentication token.")
14else:
15    print("Failed to retrieve authentication token.")