question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gmail api sending email: TypeError: a bytes-like object is required, not 'str'

See original GitHub issue

Hi, I got an error when i try to use code example for sending email with gmail api. Error: TypeError: a bytes-like object is required, not ‘str’

Environment details

  • OS type and version:
  • Python version: 3.9
  • pip version:20.3.3
  • google-api-python-client version: 2.13.0

Steps to reproduce

  1. Excute example code

Code example

from email.mime.text import MIMEText
import base64
creds = Credentials.from_authorized_user_file('token.json')
service = build('gmail', 'v1', credentials=creds)
msg = MIMEText("test")
msg['Subject'] = "test"
msg['From'] = "my_address"
msg['To'] = "my_address"
e = (service.users().messages().send(userId="me", body={'raw': base64.urlsafe_b64encode(msg.as_string())}).execute())

Stack trace

File "/usr/lib/python3.9/base64.py", line 118, in urlsafe_b64encode
return b64encode(s).translate(_urlsafe_encode_translation)
File "/usr/lib/python3.9/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
busunkim96commented, Aug 10, 2021

Glad to hear you were able to get that working! And thank you for posting the solution here. 🙏

1reaction
tyki6commented, Aug 10, 2021

Hi, I tried this and it’s worked.

{'raw': base64.urlsafe_b64encode(msg.as_bytes()).decode()}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Gmail API Error from Code Sample - a bytes-like object is ...
First, str object is encoded to bytes object - base64.urlsafe_b64encode requires it in Python 3 (compared to str object in Python 2). Then,...
Read more >
TypeError: a bytes-like object is required, not 'str' #857 - GitHub
Bytes objects contain raw data — a sequence of octets — whereas strings are Unicode sequences . Conversion between these two types is...
Read more >
Python – Gmail API Error from Code Sample – a bytes-like object is ...
I'm incorporating the Gmail API into a program that I'm making, and I'm getting an error that I ... TypeError: a bytes-like object...
Read more >
Sending Email | Gmail - Google Developers
There are two ways to send email using the Gmail API: You can send it directly using the messages.send method. You can send...
Read more >
subscriptions create failed due to Object of type 'bytes' is not ...
Failed to send email because a bytes-like object is required, not 'str'.: TypeError: a bytes-like object is required, not 'str'
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found