Python Gmail API 'not JSON serializable' -
i want send email through python using gmail api. everythingshould fine, still error "an error occurred: b'q29udgvudc1uexbloib0zxh0l3bsywluoybjagfyc2v0psj1cy1hc2npasiktulnrs..." here code:
import base64 import httplib2 email.mime.text import mimetext apiclient.discovery import build oauth2client.client import flow_from_clientsecrets oauth2client.file import storage oauth2client.tools import run_flow # path client_secret.json file downloaded developer console client_secret_file = 'client_secret.json' # check https://developers.google.com/gmail/api/auth/scopes available scopes oauth_scope = 'https://www.googleapis.com/auth/gmail.compose' # location of credentials storage file storage = storage('gmail.storage') # start oauth flow retrieve credentials flow = flow_from_clientsecrets(client_secret_file, scope=oauth_scope) http = httplib2.http() # try retrieve credentials storage or run flow generate them credentials = storage.get() if credentials none or credentials.invalid: credentials = run_flow(flow, storage, http=http) # authorize httplib2.http object our credentials http = credentials.authorize(http) # build gmail service discovery gmail_service = build('gmail', 'v1', http=http) # create message send message = mimetext("message") message['to'] = "myemail@gmail.com" message['from'] = "python.api123@gmail.com" message['subject'] = "subject" body = {'raw': base64.b64encode(message.as_bytes())} # send try: message = (gmail_service.users().messages().send(userid="me", body=body).execute()) print('message id: %s' % message['id']) print(message) except exception error: print('an error occurred: %s' % error)
i had same issue, assume using python3 found on post , suggestion following:
raw = base64.urlsafe_b64encode(message.as_bytes()) raw = raw.decode() body = {'raw': raw}
check out: https://github.com/google/google-api-python-client/issues/93
Comments
Post a Comment