【问题标题】:Gmail api python encode/decode errorGmail api python编码/解码错误
【发布时间】:2018-07-25 15:16:15
【问题描述】:

我有问题。我正在尝试使用 gmail api 阅读来自 gmail 的电子邮件。我按照这里的说明进行操作https://developers.google.com/gmail/api/v1/reference/users/messages/get

我做了一些更改以在 python 3 上运行它,所以我最终得到了以下代码:

def GetMimeMessage(service, user_id, msg_id):
try:
  message = service.users().messages().get(userId=user_id, id=msg_id,
                                         format='raw').execute()

  print ('Message snippet: %s' % message['snippet'])
  msg_str = base64.urlsafe_b64decode(message['raw'].encode('utf8'))
  mime_msg = email.message_from_bytes(msg_str)
  print(mime_msg)

  return mime_msg
except errors.HttpError as error:
  print ('An error occurred: %s' % error)

现在输出非常接近我想要的,但是有一个问题是输出中的匈牙利重音字符很奇怪: G=C3=A1bor 而不是 Gábor

而且html标签也被破坏了:

Follow us:          =09=09=09=09=09=09=09=09=09<a href=3D"http=

我已经发现这与消息的编码方式有关,请参阅电子邮件的标题:

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

问题是,我似乎无法找到正确解码它的方法。感谢您的帮助。

【问题讨论】:

    标签: python api encoding gmail quoted-printable


    【解决方案1】:

    我自己在同一个问题上苦苦挣扎了几个小时后,终于找到了解决方案(至少对我来说是这样)。即使线程很旧,我也将其发布在这里,以便其他人以后可以找到它。就是这样:

    # Get your message
    message = service.users().messages().get(userId=user_id, id=msg_id, format='full').execute()
    
    # Get the right body data item (this part was my Heureka-moment as there are several 'data' keys)
    content = message['payload']['parts'][0]['parts'][1]['body']['data']
    
    # Encode
    msg_body = base64.urlsafe_b64decode(content).decode('utf-8')
    

    这个序列让我的解码/编码工作。有许多不同的解决方案建议,因为有关于 base64 和 Google Gmail API 的解码/编码问题的线程,但似乎每种情况都是独一无二的。希望这个解决方案也能帮到你!

    【讨论】:

    • 我花了一整天的时间来解决这个问题。非常感谢!这应该由 Google 在他们的文档中介绍。
    【解决方案2】:

    谢谢图皮塔皮。 Python 的 Gmail API 文档可能会更清晰一些。上述解决方案也适用于我。我可以通过以下方式检索内容:

     msgs = message['payload']['parts']
     msg = msgs[1]['body']['data']
    

    MessagePart 可能由不同的部分组成:

    "parts": [
          {
            "partId": "0",
            "mimeType": "text/plain",
            "filename": "",
            "headers": [
              { "name": "Content-type", "value": "text/plain;charset=utf-8" },
              { "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
            ],
            "body": {
              "size": 1887,
              "data": "ascii string to be decoded"
            }
          },
          {
            "partId": "1",
            "mimeType": "text/html",
            "filename": "",
            "headers": [
              { "name": "Content-type", "value": "text/html;charset=utf-8" },
              { "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
            ],
            "body": {
              "size": 66970,
              "data": "longer ascii string to be decoded"
            }
          }
        ]
    

    Doc 中提取完整的有效负载:

    Resource representations An email message.
    
    {   "id": string,   "threadId": string,   "labelIds": [
        string   ],   "snippet": string,   "historyId": unsigned long,   "internalDate": long,   "payload": {
        "partId": string,
        "mimeType": string,
        "filename": string,
        "headers": [
          {
            "name": string,
            "value": string
          }
        ],
        "body": users.messages.attachments Resource,
        "parts": [
          (MessagePart)
        ]   },   "sizeEstimate": integer,   "raw": bytes }
    

    【讨论】:

      猜你喜欢
      • 2015-12-05
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2017-07-27
      • 2023-03-26
      • 2012-01-28
      • 2014-06-26
      • 2015-02-03
      相关资源
      最近更新 更多