How to switch to a different authorized account in Gmail API? - python

I am writing a small python script to automate a workflow in my company. I have been testing the code on by inbox so far, as I did not want to mess up anything in the inbox of the actual account this script will be working on. The rest of the code was written, and after testing, I wanted to switch to the other account. So I logged into the new account, made a new project in the console, generated that projects unique client_secret file, and I then swapped out the old client_secret file with the new one, and also updated the project name to the one linked with the new account. However, upon running the script, I realized that the script was still running with my account linked. Im really not sure what else there is I need to replace, or if I am simply missing something?
Here are the relevant parts of my code for this issue:
import...
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
lags = None
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'gmail-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def GetMessage(service, user_id, msg_id, fmt, flds ,mdh):
"""Get a Message with given ID.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
msg_id: The ID of the Message required.
Returns:
A Message.
"""
try:
message = service.users().messages().get(userId=user_id, id=msg_id, format=fmt,fields=flds ,metadataHeaders=mdh).execute()
#print ('Message snippet: %s' % message['snippet'])
return message
except errors.HttpError, error:
print ('An error occurred: %s' % error)
def GetMimeMessage(service, user_id, msg_id):
"""Get a Message and use it to create a MIME Message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
msg_id: The ID of the Message required.
Returns:
A MIME Message, consisting of data from Message.
"""
try:
message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute()
#print ('Message snippet: %s' % message['snippet'])
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
mime_msg = email.message_from_string(msg_str)
return mime_msg
except errors.HttpError, error:
print ('An error occurred: %s' % error)
def ListMessagesMatchingQuery(service, user_id, query=''):
"""List all Messages of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'from:user#some_domain.com' for Messages from a particular sender.
Returns:
List of Messages that match the criteria of the query. Note that the
returned list contains Message IDs, you must use get with the
appropriate ID to get the details of a Message.
"""
try:
response = service.users().messages().list(userId=user_id,
q=query).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id, q=query,
pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
except errors.HttpError, error:
print ('An error occurred: %s' % error)
def SendMessage(service, user_id, message):
"""Send an email message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message: Message to be sent.
Returns:
Sent Message.
"""
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print ('Message Id: %s' % message['id'])
return message
except errors.HttpError, error:
print ('An error occurred: %s' % error)
def CreateMessage(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText.MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
def GmailInput():
'''
:return:
'''
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
defaultList= ListMessagesMatchingQuery(service, 'me', 'subject: claimed infringement label:unread ')
currentMSG=[]
ips=[]
DraftedMessages=[]
for msg in defaultList:
currentMSG=GetMimeMessage(service, 'me', msg['id'])
strCurrentMessage=str(currentMSG)
#Extract the IP Address Field using the XML included in DMCA emails
#get a string value of the current message
if '<IP_Address>' not in strCurrentMessage:
pass
#print("XML TAG DOES NOT EXIST")
else:
loc1 = strCurrentMessage.find('<IP_Address>')
if '</IP_Address>' not in strCurrentMessage:
pass
#print("XML TAG DOES NOT EXIST")
else:
loc2 = strCurrentMessage.find('</IP_Address>')
#extract the IP address:
#get rid of the XML identifier in the beginning of the string:
LOC1=loc1+12
#determine legth of ip field
length=loc2-LOC1
ipaddress = strCurrentMessage[LOC1 : LOC1+length]
ips.append(ipaddress)
print(ips)
return ips
def main():
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://mail.google.com/'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'name'
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
GmailInstance=GmailInput()
InfobloxComments=InfobloxLookup(GmailInstance)
SEPERATED_COLS=[]
for eachComment in InfobloxComments:
DividedComments=SplitComments(eachComment)
SEPERATED_COLS.append(DividedComments)
columns=defaultdict(list)
with open('entities_table.csv', 'rb') as ENTITIES:
reader=csv.DictReader(ENTITIES)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
CEN_ENTITIES=columns['Common Name']
SEPERATED_ENT=[]
for eachEntity in CEN_ENTITIES:
DividedEntities=SplitComments(eachEntity)
SEPERATED_ENT.append(DividedEntities)
CallGlobalNocScript()
MakeJSON(InfobloxComments)
file='email_list.json'
input_data=json.loads(open(file).read())
print(input_data)
for each in input_data:
pass
# GmailOutput(each)
if __name__ == '__main__':
sys.exit(main())

Related

Gmail API - set sender name when sending mail from a g-suite service account

I want to dynamically change the name that shows up in the recipients mailbox (appears on the left of the subject + content of the email).
No matter what I try, the name always ends up as 'info', and the 'from' address is always 'info#g-suite-domain'
Following is the code I'm using.
import argparse
import base64
import os
import sys
from email.mime.text import MIMEText
from apiclient import errors
from google.oauth2 import service_account
from googleapiclient.discovery import build
from httplib2 import Http
def create_message(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text, 'html')
message['to'] = to
message['from'] = sender
message['subject'] = subject
raw = base64.urlsafe_b64encode(message.as_bytes())
raw = raw.decode()
body = {'raw': raw}
return body
def send_message(service, user_id, message):
"""Send an email message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message: Message to be sent.
Returns:
Sent Message.
"""
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print('Message Id: %s' % message['id'])
return message
except errors.HttpError as error:
print('An error occurred: %s' % error)
def service_account_login():
SCOPES = [
'https://mail.google.com',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.readonly',
]
dirname = os.path.dirname(os.path.realpath(__file__))
SERVICE_ACCOUNT_FILE = dirname + '/service-key.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = credentials.with_subject(
'info#g-suite-domain')
service = build('gmail', 'v1', credentials=delegated_credentials)
return service
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--to', required=True)
parser.add_argument('--subject', required=True)
args = parser.parse_args()
content = sys.stdin.read()
EMAIL_FROM = "service-acount-id#project-id.iam.gserviceaccount.com"
service = service_account_login()
# Call the Gmail API
message = create_message(EMAIL_FROM, args.to, args.subject, content)
sent = send_message(service, 'me', message)
I have tried...
relevant solutions I could find online but none helped.
setting both the EMAIL_FROM and with_subject address to the form of "Name <sender-address#domain>" which has no effect.
changing the 'Send mail as' option in gmail for 'info#g-suite-domain'
specifying the name for both email accounts in gmail, google admin, and google console.
creating a new project + service account
reading the googleapiclient.discovery library for problematic code but could not find anything.
I previously transferred the domain providers from Wix to Namecheap, and I didn't realize the MX records were not transferred. It seems, due to the lack of MX record, the from address would always rewrite to 'info#g-suite-domain' (ignoring the message['from'] value).
I have obtained the desired result with:
message["from"] = "info#g-suite-domain <info#g-suite-domain>"

Send mail in Gmail with python

I'm trying to send a simple test email using the Gmail API but I keep getting the same error for every code sample I find.
My code at the moment is this:
def validationService():
SCOPES = ['https://mail.google.com/']
SERVICE_ACCOUNT_FILE = 'creds.json'
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('gmail', 'v1', credentials=creds)
return service
def SendMessage(service, user_id, message):
try:
message = (service.users().messages().send(userId=user_id, body=message).execute())
print('Message Id:',message['id'])
return message
except errors.HttpError as error:
print('An error occurred:', error)
def CreateMessage(sender, to, subject, message_text):
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
service = validationService()
email = CreateMessage("sendermail#gmail.com", "receiverrmail#gmail.com", "Test", "This is a test")
sent = SendMessage(service, "sendermail#gmail.com", email)
Returns
>>> An error occurred: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Bad Request">
I also don't understand the difference between the "sender" parameter at CreateMessage and userId at SendMessage.
If serves a purpose, I'm using a Service Account credential
-
Thanks!
When using a service account with the Gmail API. You need to set domain-wide delegation, which allows the service account to impersonate any user in your G Suite domain. Therefore, you must have a G Suite Account to be able to use domain wide-delegation as the docs say:
If you have a G Suite domain—if you use G Suite, for example—an
administrator of the G Suite domain can authorize an application to
access user data on behalf of users in the G Suite domain.
So now, why do you need to impersonate a user(a real person)? it's due to the fact a service account is a bot(not a real person) that is used to server-to-server interactions making possible your app calls Google APIs and although the service account has a parameter called client_email, which has a structure like name#project-randomnumber.iam.gserviceaccount.com it's not a real email that belongs to a real person (kind of confusing I know).
Having said that, I made some changes to your code. First, I modified your validationService function in order to build the service using domain-wide delegation.
def validationService():
# Set the crendentials
credentials = service_account.Credentials.\
from_service_account_file(SERVICE_ACCOUNT_FILE, scopes= SCOPES)
# Delegate the credentials to the user you want to impersonate
delegated_credentials = credentials.with_subject(USER_EMAIL)
service = discovery.build('gmail', 'v1', credentials=delegated_credentials)
return service
In your SendMessage function is not necessarily to pass the user who is going to send the email. Using me is enough as the Users.messages: send Parameters state:
userId string The user's email address. The special value me can be
used to indicate the authenticated user.
def SendMessage(service, message):
message = service.users().messages().send(userId="me", body=message).execute()
return message
Your whole code at the end could look like this one:
from googleapiclient import discovery, errors
from oauth2client import file, client, tools
from google.oauth2 import service_account
from email.mime.text import MIMEText
import base64
SERVICE_ACCOUNT_FILE = 'service_account.json'
SCOPES = [' https://mail.google.com/']
# The user we want to "impersonate"
USER_EMAIL = "user#domain"
def validationService():
# Set the crendentials
credentials = service_account.Credentials.\
from_service_account_file(SERVICE_ACCOUNT_FILE, scopes= SCOPES)
# Delegate the credentials to the user you want to impersonate
delegated_credentials = credentials.with_subject(USER_EMAIL)
service = discovery.build('gmail', 'v1', credentials=delegated_credentials)
return service
def SendMessage(service, message):
message = service.users().messages().send(userId="me", body=message).execute()
return message
def CreateMessage(sender, to, subject, message_text):
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
def main():
try:
service = validationService()
email = CreateMessage(USER_EMAIL, "receiverrmail#domain", "Test", "This is a test")
email_sent = SendMessage(service, email)
print('Message Id:', email_sent['id'])
except errors.HttpError as err:
print('\n---------------You have the following error-------------')
print(err)
print('---------------You have the following error-------------\n')
if __name__ == '__main__':
main()
Notice
You also need to allow your service account to access Google's API when using domain-wide delegation by setting the Managing API client access on your G Suite account.
You don't break your head... Just use email and smtplib libraries. Very simple and fun sending email through Python, if you ask me. I have given my coding below and you should develop your own code based on this. If it works, just let me know - I will be happy. here it is....
import email, smtplib, os, time, fnmatch
from datetime import date, timedelta
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def sendMailTo(recepient_id, today):
login_id = 'myemailID#gmail.com'
login_pwd = "mypassword"
fro_ = login_id
s_name = 'smtp.gmail.com'
s_port = 587
fileList = []
fi_type = '*.pdf'
file_path = "C:\\Myfolder\\Subfolder"
srvr = smtplib.SMTP(s_name, s_port)
srvr.ehlo()
srvr.starttls()
srvr.ehlo()
srvr.login(login_id, login_pwd)
sub = "Your subject here"
# loading MIMEMultipart obj onto outer var
outer = MIMEMultipart('alternative')
outer["From"] = 'Your company/Personal Name'
outer["To"] = recepient_id
outer['Subject'] = sub
# storing only pdf files
fileList = fnmatch.filter(os.listdir(file_path), fi_type)
for fi in fileList:
fi_name = os.path.join(file_path, fi)
fp = open(fi_name, 'rb')
img = MIMEImage(fp.read(), _subtype='jpg')
fp.close()
img.add_header('Content-disposition', "attachment", filename = fi)
outer.attach(img)
#start sending email with attachment with original file name
srvr.sendmail(fro_, recepient_id, outer.as_string())
part = None
outer = None
srvr.quit()

How correctly set "In-Reply-To" and "Reference" headers in Gmail API

I'm trying to reply a mail I received using Gmail API. I tried following code it appends the sending message to thread in my mailbox but for the receiver it send as a new message. What is the proper way to declare In-Reply-To and Reference headers?
def create_message(origin=None, destination=to, subject=None, msg_txt=None, thr_id=None):
"""Create a message for an email.
Args:
origin: Email address of the sender.
destination: Email address of the receiver.
subject: The subject of the email message.
msg_txt: The text of the email message.
thr_id: the threadId of the message to attach
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(msg_txt)
message['to'] = destination
message['from'] = origin
message['subject'] = subject
raw_msg={'raw': (base64.urlsafe_b64encode(message.as_bytes()).decode())}
raw_msg['threadId'] =thr_id
raw_msg['Reference'] = '<CANyAw3CWm33sKL80GMKp-b=8JgXz3MVPkvCVbJ_oK4NuGJcb3w#mail.gmail.com>'
raw_msg['In-Reply-To'] = '<CANyAw3CWm33sKL80GMKp-b=8JgXz3MVPkvCVbJ_oK4NuGJcb3w#mail.gmail.com>'
raw_msg['Message-ID'] = '<CANyAw3CWm33sKL80GMKp-b=8JgXz3MVPkvCVbJ_oK4NuGJcb3w#mail.gmail.com>'
return raw_msg
My main method is as follow,
def main():
"""Canned reply responder using the Gmail API.
Creates a Gmail API service object and responds to a query with a standard response
whilst giving it a label to ensure only 1 response per thread is sent
"""
# get credentials first and create gmail service object
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('gmailApiCredentials.json', SCOPES)
creds = tools.run_flow(flow, store)
gmail_service = build('gmail', 'v1', http=creds.authorize(Http()))
# receive email messages
q = 'subject:this is a test message'
messages = list_messages_matching_query(gmail_service, user_id,
query=q,
maxResults=1)
if not messages:
print("No messages to show")
else:
pprint.pprint('Messages to show: {}'.format(messages))
# get thread of first document - so you can label the thread itself if need be
thread_id = messages[0]['threadId']
thread = get_thread(gmail_service, user_id, thread_id)
msg_id = messages[0]['id']
message = get_message(gmail_service, user_id, msg_id)
subject ='Re:this is a test message'
msg = create_message(destination=to, origin=to,
subject=subject,
msg_txt='Hai', thr_id=thread_id)
send_message(gmail_service,"me", msg)
print("Message Sent")
The create_message method should be changed as follow to set 'Reference' and 'In-Reply-To' headers.
def create_message(origin=None, destination=TO, subject=None, msg_txt=None, thr_id=None, msgID=None):
"""Create a message for an email.
Args:
origin: Email address of the sender.
destination: Email address of the receiver.
subject: The subject of the email message.
msg_txt: The text of the email message.
thr_id: the threadId of the message to attach
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(msg_txt)
message['to'] = destination
message['from'] = origin
message['subject'] = subject
message.add_header('Reference', msgID)
message.add_header('In-Reply-To', msgID)
raw_msg = {'raw': (base64.urlsafe_b64encode(message.as_bytes()).decode())}
raw_msg['threadId'] = thr_id
return raw_msg
The correct msgID can be obtained from following method,
def get_mime_message(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id,
format='raw').execute()
msg_str = base64.urlsafe_b64decode(message['raw']).decode()
mime_msg = email.message_from_string(msg_str)
return mime_msg
except errors.HttpError as error:
print('An error occurred: %s' % error
The msgID can be obtained as follow,
ms = get_mime_message(gmail_service, USER_ID, msg_id)
msgID = format(ms['Message-ID'])
Do not confuse msg_id with msgID. msg_id is gmail specific and msgID is global. This msgID should be used in 'Reference' and 'In-Reply-To' headers.
I ran into this issue a while back as well, and threadId to my "top level" headers, like this:
return {'raw': base64.urlsafe_b64encode(message.as_string()), 'threadId': thread_id}
seemed to do the trick!

oauth2client.clientsecrets.InvalidClientSecretsError: Missing property "redirect_uris" in a client type of "web"

I am trying to send Gmail from a Python script to myself, and have the following code that used to be working:
#!/usr/bin/env python
import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os
import httplib2
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
from apiclient import errors
from config import MY_EMAIL, CLIENT_SECRET_FILE
SCOPES = 'https://www.googleapis.com/auth/gmail.compose'
APPLICATION_NAME = 'Gmail API Python Quickstart'
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
def send_message(service, user_id, message):
"""Send an email message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message: Message to be sent.
Returns:
Sent Message.
"""
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print('Message Id: %s' % message['id'])
return message
except errors.HttpError as error:
print('An error occurred: %s' % error)
def create_message(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'sendEmail.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def send_email(sender=MY_EMAIL, receiver=MY_EMAIL, subject=None, body=None):
print("sending email to: %s, from: %s" % (receiver, sender))
if not body:
raise Exception("You need a body to send an email")
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
send_message(service, "me", create_message(sender, receiver, subject, body))
if __name__ == "__main__":
send_email(subject="Test gmail automatic emails", body="Hello world")
My client_secret.json looks like
{"web":{"client_id":"23423423423-234234234.apps.googleusercontent.com","project_id":"prime-imagery-4444","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"asdfasdfasdf-trWCLxFdb", "redirect_uris": ["http://localhost:8000/fakeurl"]}}
I added in redirect_uris as that was the only good advice I found online, but I don't want to redirect. Last time I ran this script, having the client_secret was enough to send Gmails from my Gmail account using Python.
Just go to your Credentials in your Google Dev Console and add what it's asking. http://localhost:8080/oauth2callback or https://www.samplesite.com/oauth2callback. Adding the 'oauth2callback' is the redirect URI. Check the Create authorization credentials.
I faced the same issue.
So after you add the redirect_uri also don't forget to redownload the clien_secret.json file.
You need to perform 3 steps. (step 2 wasn't specified in other answers):
1 ) You add a uri to Authorized redirect URIs.
2 ) The Authorized redirect URIs uri is equal to the one you entered in Authorized JavaScript origins just with a closing slash /.
3 ) Download client_secret.json again and add it to your working direcory.

Python / Flask google api integration

I'm new to Python as well as in Flask... I have developed an python file which sets up the Oauth2 authentication with googele and fetching the list of messages from GMAIL API. Here is my code
import json
import flask
import httplib2
import base64
import email
from apiclient import discovery, errors
from oauth2client import client
app = flask.Flask(__name__)
#app.route('/')
def index():
if 'credentials' not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
http_auth = credentials.authorize(httplib2.Http())
gmail_service = discovery.build('gmail', 'v1', http_auth)
threads = gmail_service.users().threads().list(userId='me').execute()
return json.dumps(threads)
#app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://mail.google.com/',
redirect_uri=flask.url_for('oauth2callback', _external=True)
)
if 'code' not in flask.request.args:
auth_uri = flow.step1_get_authorize_url()
return flask.redirect(auth_uri)
else:
auth_code = flask.request.args.get('code')
credentials = flow.step2_exchange(auth_code)
flask.session['credentials'] = credentials.to_json()
return flask.redirect(flask.url_for('index'))
#app.route('/getmail')
def getmail():
if 'credentials' not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
http_auth = credentials.authorize(httplib2.Http())
gmail_service = discovery.build('gmail', 'v1', http_auth)
query = 'is:inbox'
"""List all Messages of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'from:user#some_domain.com' for Messages from a particular sender.
Returns:
List of Messages that match the criteria of the query. Note that the
returned list contains Message IDs, you must use get with the
appropriate ID to get the details of a Message.
"""
try:
response = gmail_service.users().messages().list(userId='me', q=query).execute()
messages = []
if 'messages' in response:
print 'test %s' % response
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = gmail_service.users().messages().list(userId='me', q=query, pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
except errors.HttpError, error:
print 'An error occurred: %s' % error
if __name__ == '__main__':
import uuid
app.secret_key = str(uuid.uuid4())
app.debug = True
app.run()
Authentication works fine and when I go to /getmail URL I am getting this error TypeError: 'list' object is not callable
Any thoughts what I am doing wrong?
I changed return object in Flask from return messages to this piece of code.
First I imported to from flask.json import jsonify
try:
response = gmail_service.users().messages().list(userId='me', q=query).execute()
messages = []
if 'messages' in response:
print 'test %s' % response
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = gmail_service.users().messages().list(userId='me', q=query, pageToken=page_token).execute()
messages.extend(response['messages'])
return jsonify({'data': messages}) # changed here
except errors.HttpError, error:
print 'An error occurred: %s' % error
all credit goes to #doru

Categories