1. <i id='lyWdk'><tr id='lyWdk'><dt id='lyWdk'><q id='lyWdk'><span id='lyWdk'><b id='lyWdk'><form id='lyWdk'><ins id='lyWdk'></ins><ul id='lyWdk'></ul><sub id='lyWdk'></sub></form><legend id='lyWdk'></legend><bdo id='lyWdk'><pre id='lyWdk'><center id='lyWdk'></center></pre></bdo></b><th id='lyWdk'></th></span></q></dt></tr></i><div id='lyWdk'><tfoot id='lyWdk'></tfoot><dl id='lyWdk'><fieldset id='lyWdk'></fieldset></dl></div>
        • <bdo id='lyWdk'></bdo><ul id='lyWdk'></ul>

        <tfoot id='lyWdk'></tfoot>

        <legend id='lyWdk'><style id='lyWdk'><dir id='lyWdk'><q id='lyWdk'></q></dir></style></legend>

        <small id='lyWdk'></small><noframes id='lyWdk'>

        来自代码示例的 Gmail API 错误 - 需要类似字节的对象,而不是“str"

        Gmail API Error from Code Sample - a bytes-like object is required, not #39;str#39;(来自代码示例的 Gmail API 错误 - 需要类似字节的对象,而不是“str)

        <small id='uX0mt'></small><noframes id='uX0mt'>

        1. <legend id='uX0mt'><style id='uX0mt'><dir id='uX0mt'><q id='uX0mt'></q></dir></style></legend>

            <tbody id='uX0mt'></tbody>
        2. <tfoot id='uX0mt'></tfoot>

          <i id='uX0mt'><tr id='uX0mt'><dt id='uX0mt'><q id='uX0mt'><span id='uX0mt'><b id='uX0mt'><form id='uX0mt'><ins id='uX0mt'></ins><ul id='uX0mt'></ul><sub id='uX0mt'></sub></form><legend id='uX0mt'></legend><bdo id='uX0mt'><pre id='uX0mt'><center id='uX0mt'></center></pre></bdo></b><th id='uX0mt'></th></span></q></dt></tr></i><div id='uX0mt'><tfoot id='uX0mt'></tfoot><dl id='uX0mt'><fieldset id='uX0mt'></fieldset></dl></div>
                • <bdo id='uX0mt'></bdo><ul id='uX0mt'></ul>
                • 本文介绍了来自代码示例的 Gmail API 错误 - 需要类似字节的对象,而不是“str"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm incorporating the Gmail API into a program that I'm making, and I'm getting an error that I haven't been able to resolve/that I haven't been able to find an answer to online. The relevant code is below, as well as the error:

                  from apiclient import discovery
                  from httplib2 import Http
                  from oauth2client import file, client, tools
                  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
                  import os
                  
                  def create_message(sender, to, subject, message_text):
                  
                      message = MIMEText(message_text)
                      message['to'] = to
                      message['from'] = sender
                      message['subject'] = subject
                  
                      return {'raw': base64.urlsafe_b64encode(message.as_string())}
                  
                  def send_message(service, user_id, message):
                  
                      message = (service.users().messages().send(userId=user_id, body=message).execute())
                      print('Message Id: %s' % message['id'])
                      return message
                  
                  def send_email(orders):
                      SCOPES = 'https://mail.google.com/'
                      store = file.Storage('gmail.json')
                      creds = store.get()
                      if not creds or creds.invalid:
                          flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
                          creds = tools.run_flow(flow, store)
                      service = discovery.build('gmail','v1',http=creds.authorize(Http()))
                  
                      message_text = orders[0]
                  
                      created_message = create_message('from','to','subject', message_text)
                      send_message(service, 'from', created_message)
                  send_email(['TEST'])
                  
                  
                  Traceback (most recent call last):
                    File "test_email.py", line 50, in <module>
                      schoolPing(['TEST'])
                    File "test_email.py", line 47, in schoolPing
                      created_message = create_message('from','to','subject', message_text)
                    File "test_email.py", line 27, in create_message
                      return {'raw': base64.urlsafe_b64encode(message.as_string())}
                    File "/Users/Andre/anaconda/lib/python3.5/base64.py", line 119, in urlsafe_b64encode
                      return b64encode(s).translate(_urlsafe_encode_translation)
                    File "/Users/Andre/anaconda/lib/python3.5/base64.py", line 59, in b64encode
                      encoded = binascii.b2a_base64(s)[:-1]
                  TypeError: a bytes-like object is required, not 'str'
                  

                  解决方案

                  Found a solution, replace this line:

                  return {'raw': base64.urlsafe_b64encode(message.as_string())}
                  

                  with:

                  return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
                  

                  Notice added .encode() and .decode() method calls.

                  First, str object is encoded to bytes object - base64.urlsafe_b64encode requires it in Python 3 (compared to str object in Python 2).

                  Then, the base64 encoded bytes object must be decoded back to str. This is needed as googleapiclient library will attempt to json serialize it later in code and that is not possible for bytes objects.

                  这篇关于来自代码示例的 Gmail API 错误 - 需要类似字节的对象,而不是“str"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                    <tfoot id='5cLz6'></tfoot>
                      <tbody id='5cLz6'></tbody>
                  • <legend id='5cLz6'><style id='5cLz6'><dir id='5cLz6'><q id='5cLz6'></q></dir></style></legend>

                    • <small id='5cLz6'></small><noframes id='5cLz6'>

                        • <bdo id='5cLz6'></bdo><ul id='5cLz6'></ul>

                            <i id='5cLz6'><tr id='5cLz6'><dt id='5cLz6'><q id='5cLz6'><span id='5cLz6'><b id='5cLz6'><form id='5cLz6'><ins id='5cLz6'></ins><ul id='5cLz6'></ul><sub id='5cLz6'></sub></form><legend id='5cLz6'></legend><bdo id='5cLz6'><pre id='5cLz6'><center id='5cLz6'></center></pre></bdo></b><th id='5cLz6'></th></span></q></dt></tr></i><div id='5cLz6'><tfoot id='5cLz6'></tfoot><dl id='5cLz6'><fieldset id='5cLz6'></fieldset></dl></div>