0

I've created a project in GCP in which I'm trying to monitor inboxes of two accounts. I've created a service account with domain-wide delegation enabled via Google Admin Console. But I'm not able to access the inbox of these two accounts. I tried getting profile info:

def authenticate_gmail_api(): """Authenticates and returns the Gmail API service.""" creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES, subject=USER_EMAIL) return build('gmail', 'v1', credentials=creds) service = authenticate_gmail_api() profile = service.users().getProfile(userId='me').execute() print(profile) 

and it is printing this: {'emailAddress': '[email protected]', 'messagesTotal': 1375, 'threadsTotal': 405, 'historyId': '252086'}

But when I try to access the history details by calling history list:

history_response = service.users().history().list( userId='me', startHistoryId=start_history_id ).execute() print("History Response:", history_response) 

I'm using an old history ID and it gives only historyId like this: History Response: {'historyId': '252155'}

I've tested by accessing other accounts in the workspace and I'm able to get the history details. But not working for those two accounts I need to monitor. So I asked my Google admin to check account type. It seems it is a primary account (regular account listed under Users) but it has routing enabled to forward the messages to other email addresses. It's like support email addresses that businesses have and it goes to multiple people but also maintains a

How can I access this type of account via service account?

5
  • As you may know the service accounts don't belong to an individual end user, but to an application.You can use a service account to access data or perform actions by the account itself, or to access data on behalf of Google Workspace. Refer to this link: Control API access with domain-wide delegation and let me know if it is helpful. Commented Feb 25 at 8:56
  • And most importantly make sure that the Gmail API is enabled in your GCP for the service account to interact with Gmail and to enable google workspace API refer to this official documentation and ensure that service account is impersonating the correct user account. Commented Feb 25 at 9:02
  • Also to access user data on a Google Workspace domain, the service account that you created needs to be granted access by a super administrator for the domain. Commented Feb 25 at 9:04
  • Did you have time to check my comment? It helped you to solve your issue? If not,I am happy to assist further Commented Feb 26 at 4:14
  • @ImranPremnawaz, actually the admin had some routing settings for this particular account, hence it was not saving the history. Once I asked him to keep the history, it started returning via the history list API. So this issue is resolved. The service account already had domain wide delegation etc. Thanks for your help though. Really appreciate it. Commented Mar 8 at 7:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.