Gmail api sending email: TypeError: a bytes-like object is required, not 'str'
See original GitHub issueHi, 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
- 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Glad to hear you were able to get that working! And thank you for posting the solution here. 🙏
Hi, I tried this and it’s worked.