Search code examples
python-3.xemailgmailgmail-api

Gmail API Reading credentials 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte


I think there is an issue with Gmail API and Python3. The original code in the documentation is in Python2, but for several reasons, including that my application is already working with Python3, I was passing the code to python3.

So... after sorting several issues, including a 400 request error, (which was apparently that the auth I provided to google wasn't corretly done) I'm facing (I hope) the final issue that apparently I'm trying to read a file but

Even just doing token.read() generates the same issue.

The token.pickle file is autogenerated once you've authorized google to access your email account and that the app can send emails automatically.

I know the credentials.json file is correct, as that's the point to tell google who you are and it always is reading my credentials correctly, redirecting me to give authorization to my app.

Here's the app to send emails, I think it's pretty straightforward, I followed the documentation and looked at other issues like this one and finally got this far:

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
from googleapiclient.errors import HttpError

# from httplib2 import Http
# from oauth2client import client, tools, file

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
from apiclient import errors

SCOPES = 'https://mail.google.com/'


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(f'Message Id: {message["id"]}')
        return message
    except errors.HttpError as error:
        print(f'An error occurred: {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(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    message_bytes = message.as_string().encode('utf-8')

    # return { 'raw': base64.urlsafe_b64encode(message.as_string()) }
    # return { 'raw': base64.urlsafe_b64encode(message_bytes) }
    raw = base64.urlsafe_b64encode(message_bytes)
    return raw.decode()


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', 'r') 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)

    # Create
    message = CreateMessage('[email protected]', '[email protected]', 'Testing Gmail API', 'Hi GMAIL API')

    # Send
    SendMessage(service, "me", message)

if __name__ == '__main__':
    main()

I truly don't know what to do here, if someone has already solved this issue and how.

THANKS!


Solution

  • In your script, when token.pickle is not created, Gmail API can be used by creating token.pickle at 1st authorization. But an error of 'raw' RFC822 payload message string or uploading message via /upload/* URL required occurs at message = (service.users().messages().send(userId=user_id, body=message).execute()). So in this case, please modify as follows.

    From:

    raw = base64.urlsafe_b64encode(message_bytes)
    return raw.decode()
    

    To:

    raw = base64.urlsafe_b64encode(message_bytes).decode('utf-8')
    return {'raw': raw}
    

    After above modification, when your script is run as the 2nd run, when token.pickle, which has already been created, is read, an error occurs. I think that this error is in your title. In this case, please modify as follows.

    From:

    with open('token.pickle', 'r') as token:
    

    To:

    with open('token.pickle', 'rb') as token:
    

    By this, I think that the script works.

    By the way, if an error occurs when the code is retrieved, also please modify as follows.

    From:

    creds = flow.run_local_server(port=0)
    

    To:

    creds = flow.run_local_server()
    

    Note:

    If this was not the direction of your issue, I apologize.