'How do I using multiple gmail accounts with gmail API on the python project?
I trying create a project using few gmail accounts. I added first gmail account and its working clearly but I am stuck when I trying add other accounts. I check on Google doc. and i dont find. A credentials.json file was generated when I added the first account and only one credentials file was generated for a project as far as I know. So, How do I using other gmail accounts's data in my project?
Here is my code:
SCOPES = ['https://mail.google.com/']
def gmail_authenticate():
creds = None
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
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)
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
return build('gmail', 'v1', credentials=creds)
service = gmail_authenticate()
get_email = service.users().messages().list(userId="[email protected]",labelIds= ["UNREAD"]).execute()
here is my project directory:
-credentials.json
-gmailAPI.py
-token.json
-token.pickle
Solution 1:[1]
Service account
You can get the unread headers of your Gmail accounts by using a service account and impersonate any user within your domain. But I'm assuming you're using free Gmail accounts, and that won't be feasible.
OAuth token
You need to authorize all the Gmail accounts you have in your script by getting an OAuth token. So your app will have the permissions to access your accounts' data
import google.oauth2.credentials
import google_auth_oauthlib.flow
# Use the client_secret.json file to identify the application requesting
# authorization. The client ID (from that file) and access scopes are required.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
'client_secret.json',
scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'])
# Indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required. The value must exactly
# match one of the authorized redirect URIs for the OAuth 2.0 client, which you
# configured in the API Console. If this value doesn't match an authorized URI,
# you will get a 'redirect_uri_mismatch' error.
flow.redirect_uri = 'https://www.example.com/oauth2callback'
# Generate URL for request to Google's OAuth 2.0 server.
# Use kwargs to set optional request parameters.
authorization_url, state = flow.authorization_url(
# Enable offline access so that you can refresh an access token without
# re-prompting the user for permission. Recommended for web server apps.
access_type='offline',
# Enable incremental authorization. Recommended as a best practice.
include_granted_scopes='true')
After the function runs, you will be prompted to login with your Gmail account and accept all the permissions you request.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Salomon |
