def get_gcloud_credential()

in keyrings/gauth.py [0:0]


def get_gcloud_credential():

  # fall back to fetching credentials from gcloud if Application Default Credentials fails
  try:
    logging.warning("Trying to retrieve credentials from gcloud...")
    command = subprocess.run(['gcloud','config','config-helper','--format=json(credential)'], check=True, stdout=subprocess.PIPE, universal_newlines=True)
  except Exception as e:
    raise Exception ("gcloud command exited with status: {0}".format(e))
  result = json.loads(command.stdout)
  credential = result.get("credential")
  if credential is None:
    raise Exception("No credential returned from gcloud")
  if "access_token" not in credential or "token_expiry" not in credential:
    raise Exception("Malformed response from gcloud")
  return credential.get("access_token")