How do I download every Gmail email with Python? - python

I want to download them with as few API calls as possible. https://github.com/googleapis/google-api-python-client/tree/master/samples doesn't have Gmail.

Start by following Python quickstart it will give you an idea of how to get the Auth part working
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
if __name__ == '__main__':
main()
Once you understand what that is doing you should be able to consult user message list to get an idea of how to alter it for listing a messages.

Related

An API that reads mails that are unread with certain description, but when read remove the label UNREAD

I have a Python script that interacts with the Gmail API and searches mails that are unread, with certain description and from specific email addresses. But I want it so when it reads the mail it removes the UNREAD label so it doesn't detect it when I run the script again.
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
email_address1 = "mensajeria#bancoestado.cl"
email_address2 = "noreply#correo.bancoestado.cl"
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
#Get Messages
results = service.users().messages().list(userId='me', labelIds=['INBOX'], q=f'from:{email_address1} OR from:{email_address2} is:unread').execute()
messages = results.get('messages', [])
#Messages Filter
message_count = 100
for message in messages[:message_count]:
msg = service.users().messages().get(userId='me', id=message['id']).execute()
email = (msg['snippet'])
if "que recibiste Monto $" in email:
print(f'{email}\n')
service.users().messages().modify(userId='me', id=message['id'], body={'removeLabelIds': ['UNREAD']}).execute()
if __name__ == '__main__':
main()
The script works just fine until it reaches my attempt of removing the UNREAD label so it doesn't detect it again. Here
service.users().messages().modify(userId='me', id=message['id'], body={'removeLabelIds': ['UNREAD']}).execute()
I know it can be done, because in the labels section of the Gmail API documentation it says that the UNREAD label can be manually changed.
So if someone could give me a hand I would appreciate it.
I've already found a solution. The problem was that the scope that I was using didn't had the permissions needed to remove a label from a message, so I changed the scope to this instead and it worked just fine.
SCOPES = ['https://mail.google.com/']

In python, is there any way I can store a 'Resource' object so I can use it later?

I am writing a program about the YouTube API
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_console()
youtube_analytics = googleapiclient.discovery.build("youtubeAnalytics", "v2", credentials=credentials)
Finally it will return a 'Resource' object, can I store this object?
So that I can get this object to use in the future just by referring to the file
You can store the json creds returned by the authorization flow. The library will then be able to load those stored creds the next time it needs access.
The following example is adapted from the official Google drive quickstart
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']
def main():
"""Shows basic usage of the YouTube Analytics v2 API.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('youtubeAnalytics', 'v2', credentials=creds)
# Call the YouTube analytics
...
except HttpError as error:
# TODO(developer) - Handle errors from API.
print(f'An error occurred: {error}')
if __name__ == '__main__':
main()
If you have any issues with this please let me know.
save it as a pkl file then call the file and update it
import pickle
dump into pkl file for first time and updates
with open('mypickle.pickle', 'wb') as f:
pickle.dump(resource, f)
resource = whatever you want to store
open file at later time
with open('mypickle.pickle', 'rb') as f:
loaded_obj = pickle.load(f)

Download Attachment from gmail api using Python

I am trying to download the attachment from gmail using the python and I am not able to fetch the attachment id from my mail. Please find my code below
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def get_gmail_service():
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
# Call the Gmail API
service = build('gmail', 'v1', credentials=creds)
return service
except HttpError as error:
# TODO(developer) - Handle errors from gmail API.
print(f'An error occurred: {error}')
def get_email_list():
service = get_gmail_service()
results = service.users().messages().list(userId='me',q='from:abc#gmail.com is:read').execute()
# print(results.get('messages',[])[0].get('id',[]))
return results.get('messages', [])[0].get('id', [])
# return results.get('messages',[])
def get_email_content(message_id):
service = get_gmail_service()
attach = service.users().messages().get(userId='me',id =message_id).execute()
attach_id = attach.get('payloads',[]).get('parts',[]).get('body',[])
data = service.users().messages().get(userId='me',id = message_id).execute()
return attach_id
if __name__ == '__main__':
# get_email_list()
print(get_email_content(get_email_list()))
Please correct my code so that I can download the attachment using the gmail api.
There are two main issues with this code.
results.get() method either returns a Message or MessagePart Object. So you only need to use the get() method once to get the complete object and then you can target the specific part of the object you want.
For Example. results.get('messages', [])[0]['id']
A payload for an email can be multipart (which means that "parts" will be an array of MessagePart objects). So we need to iterate over to get a "message part" that has a file. In this case, we can check if the MessagePart object has a filename.
parts = attach.get('payload',[])['parts']
for i in parts:
if( i['filename'] ):
return i['body']['attachmentId']
So After taking care of these two issues, this is the new code:
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def get_gmail_service():
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
# Call the Gmail API
service = build('gmail', 'v1', credentials=creds)
return service
except HttpError as error:
# TODO(developer) - Handle errors from gmail API.
print(f'An error occurred: {error}')
def get_email_list():
service = get_gmail_service()
results = service.users().messages().list(userId='me',q='from:abc#gmail.com is:read').execute()
# print(results.get('messages',[])[0]['id'] )
return results.get('messages', [])[0]['id']
# return results.get('messages',[])
def get_email_content(message_id):
print(message_id)
service = get_gmail_service()
data = service.users().messages().get(userId='me',id = message_id).execute()
attach = service.users().messages().get(userId='me',id =message_id).execute()
parts = attach.get('payload',[])['parts']
for i in parts:
if( i['filename'] ):
return i['body']['attachmentId']
if __name__ == '__main__':
# get_email_list()
print(get_email_content(get_email_list()))
I hope this answers your question!

I am looking for Google drive storage size of the users associated with university email/directory api

I'm looking for a Google API to get the size of the drive of the users associated with university email/directory api, but I can't find anything. The code to delete a user's email using the Google API is provided below. Similarly, I need to know the size of the user's drive. Could someone please assist me? Is there a way to get the drive's size via an API? Thanks.
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.user']
def main():
"""Shows basic usage of the Admin SDK Directory API.
Prints the emails and names of the first 10 users in the domain.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('admin', 'directory_v1', credentials=creds)
return service
def test():
# user = service.users().get(userKey="user1#matador.csun.edu").execute()
# members = service.groups().list(domain='my.csun.edu', userKey=user['primaryEmail'], pageToken=None, maxResults=500).execute()
# print(user)
# Call the Admin SDK Directory API
print('Getting the first 10 users in the domain')
results = service.users().list(customer='my_customer', maxResults=10,
orderBy='email').execute()
print(results)
users = results.get('users', [])
if not users:
print('No users in the domain.')
else:
print('Users:')
for user in users:
print(user)
# print(dir(user))
# print(u'{0} ({1})'.format(user['primaryEmail'],
# user['name']['fullName']))
def del_user(user):
try:
service.users().delete(userKey=user).execute()
print("Deleted!")
except:
print("User doesn't exist!")
if __name__ == '__main__':
service = main()
nameExt='23'
# with open('NewGmailInProd/gmailUser'+nameExt+'.txt') as fileToRead:
# with open('NewGmailInProd/test.txt') as fileToRead:
# emails = fileToRead.readlines()
emails = ['user1#matador.csun.edu']
for email in emails:
del_user(email.strip())

How to fix 'No OUs found'

I am using the Google Admin SDK - Directory API, I have a successful connection to the API but no OUs are being stored in my array.
Result:
Getting the OUs in the domain
No OUs found in the domain.
I tried modifying the code to list the first 10 users instead and it works.
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.orgunit']
def main():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('admin', 'directory_v1', credentials=creds)
# Call the Admin SDK Directory API
print('Getting the OUs in the domain')
results = service.orgunits().list(customerId='my_customer').execute()
ou_list = results.get('orgunits', [])
if not ou_list:
print('No OUs found in the domain.')
else:
print('OU List:')
# for ou in ou_list:
# print(u'{0} ({1})'.format(ou_list['name'], ou_list['orgUnitPath']))
if __name__ == '__main__':
main()
No error messages are displayed but, OUs are not being stored.
The key is not orgunits, it is orgazinationalUnits:
ou_list = results.get('orgunits', [])

Categories

Resources