4

I am not using a JSON key. I use the default credential loading mechanism that is used when you create any new client.

But what means is there to reflect on the current creds?

import google.auth
creds, project = google.auth.default()

# I can get the project id
print(project)

# I can get the oauth stuff
print(creds.client_secret)
print(creds.client_id)

But how do I get the email name of the user or Google service account that is currently active?

I tried this

print(creds.service_account_email)

But it returns an error:

AttributeError: 'Credentials' object has no attribute 'service_account_email'

This is suppose to be a property of the class.

I want to get the email name of the user or google service account.

Edit

Related to this post: Programmatically get current Service Account on GCP

From an example in that post, is there a way to get the user's email name if it's not a GSA?

import google.auth

credentials, project_id = google.auth.default()

if hasattr(credentials, "service_account_email"):
  print(credentials.service_account_email)
else:
    print("WARNING: no service account credential. User account credential?")
halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392
  • Review the different credential classes. They have different properties and features. `google.auth.default` uses ADC to locate credentials. Some credential types only provide an access token and not the underlying credential properties. – John Hanley Oct 14 '22 at 20:46
  • I cannot find on Stack Overflow or GCP docs a definitive answer like "If you're authenticated using XYZ method (e.g., `Application Default Credential`, user account credential, etc.), then forget about retrieving the email -- it cannot be done (or show a sample code snippet of how it could be done.)" All the answers I am reading are narrowly scoped to `GOOGLE_APPLICATION_CREDENTIALS` but doesn't give definitive answers to other scenarios. – Vincent Yin Apr 12 '23 at 14:41

0 Answers0