I am working through this Google Gmail API tutorial (really I am skimming it to send an email). The body of my email must be encoded MIMEText & base64 but the contents of 'raw' property of a JSON payload must be a string. This leads me to a 'rock in a hard place' with 2 errors. Here is the code following the tutorial:
message = MIMEText(RecipData["EmailText"])
message['to'] = RecipData["Email"]
message['from'] = SENDER
message['subject'] = Subject
EncodedMessage = {'raw': base64.urlsafe_b64encode(message.as_string())}
service.users().messages().send(userId=SENDER, body=EncodedMessage).execute()
I get the following error:
TypeError: a bytes-like object is required, not 'str'
Now if I change this line:
EncodedMessage = {'raw': base64.urlsafe_b64encode(message.as_string())}
To:
EncodedMessage = {'raw': base64.urlsafe_b64encode(message.as_bytes())}
Then I get an error of:
TypeError: Object of type bytes is not JSON serializable
Not sure what I should do to get around this? Any suggestions? Thanks.