Error: Invalid base64-encoded string: number of data characters (5569) cannot be 1 more than a multiple of 4 in Python and Beautifulsoup

The code which shows the e – mail messages:

    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
    import base64
    from apiclient import errors
    import payload
    from bs4 import BeautifulSoup
    messagecount=int(input(" Please write the email message count which wants to be show:"))
    # If modifying these scopes, delete the file token.pickle.
    SCOPES = ['']
    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', 'rb') 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:
           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)
    # Call the Gmail API
    results = service.users().labels().list(userId='me').execute()
    results = service.users().messages().list(userId='me', labelIds=['INBOX']).execute()
    messages = results.get('messages', [])
    message_count = messagecount
    for message in messages[:message_count]:
            msg = service.users().messages().get(userId='me',metadataHeaders=None, id=message['id']).execute()
            # Use try-except to avoid any Errors
               # Get value of 'payload' from dictionary 'txt'
               payload = msg['payload']
               headers = payload['headers']
               # Look for Subject and Sender Email in the headers
               for d in headers:
                   if d['name'] == 'Subject':
                       subject = d['value']
                   if d['name'] == 'From':
                       sender = d['value']
               # The Body of the message is in Encrypted format. So, we have to decode it.
               # Get the data and decode it with base 64 decoder.
               parts = payload.get('parts')[0]
               data = parts['body']['data']
               decoded_data = base64.b64decode(data) ##### there is a problem with this line. ####
               # Now, the data obtained is in lxml. So, we will parse 
               # it with BeautifulSoup library
               soup = BeautifulSoup(decoded_data , "lxml")
               body = soup.body()
               # Printing the subject, sender's email and message
               print("Subject: ", subject)
               print("From: ", sender)
               print("Message: ", body)
            except errors:

Hello friends, when I run the above code, it gives an error on pyconsole:

return binascii.a2b_base64(s)

Invalid base64-encoded string: number of data characters (5569) cannot be 1 more than a multiple of 4

As far as I predict this problem is related to non-English characters in-mails (ğ,ü,ç,ş, etc.) but I couldn’t solve the problem. Could you help me with this?

Source: Python-3x Questions