如何解码原始格式的Gmail API获取邮件以提取链接

时间:2019-06-21 17:56:06

标签: python api base64 gmail

尝试获取给定消息ID的Gmail消息。我将消息检索为原始类型,并尝试在base64中对其进行解码,然后使用电子邮件中包含的链接。该消息似乎已被解码,但是似乎无用的字节,并且URL仍然都是无效的。

def GetMessageWithId(service, user_id, msg_id, format):
    try:
        message = service.users().messages().get(userId=user_id,
                                                 id=msg_id,
                                                 format=format).execute()
        msg_str = str(base64.urlsafe_b64decode(message["raw"].encode("ASCII")))
        return msg_str
    except errors.HttpError as error:
        print("An error occurred: %s" % error)

当我检查msg_str时,可以看到链接的位置,但是如果我尝试将它们复制并放入浏览器中,则它们无效。

然后,我尝试使用漂亮的汤在href中定位msg_str标签。但是,找到的链接看起来像这样:

  

3D“ https://post.pinterest= \ r \ n.com/f/a/WRi5L7G_wfTW1BovkyUGuw~~/AAAAAQA~/RgRe6WEYPwRXCXBpbnRlcmVzdEIKABwY= \ r \ n3AZdrwvFllIXdHVyZ2VbBn3%b2B3Db3D%By %% b%b%b%b%b%b%b%b%b%b%b%b%b%bbbbbbbbbbbbbb人.pinterest.com%2Fsecure%2Fautologin%2F%3Fod%3DFux7G1fLpQxdgu%252FAlq7%2 = \ r \ n52FO0wnXhG3mrIvODBVUav9ko5yjUdnc84zWzwWN%252BPJyxYElh86K0WCnm9Th%252F6kUWW%= \ r \ n252FfcKmC7yJz0qo50Ss4EaaUahZGfo19MQS%252BIeP4Dlvz0hgCjvxIS4R%252BPMAF%252FG = \ r \ nl9BpWrQ%253D 253D%%26user_id %3DNjEwMDk3MjE4MTc4OTE0MjA0%26next%3D%252Fpin%25 = \ r \ n2F806707351985179613%252F%253Futm_campaign%253Dpopular_pins%2526e_t%253De5a = \ r \ nb90da0abf493b944b3c27261acfe3%2526utm_content%253D806707351985179613%2526ut = \ r \ nm_source%253D31%2526utm_term%253D1%2526utm_medium% 253D2012

我希望可以将整个原始电子邮件解码为html,但似乎只有部分电子邮件可以。我将在Gmail的文档中链接此获取消息https://developers.google.com/gmail/api/v1/reference/users/messages/get

的链接

1 个答案:

答案 0 :(得分:0)

这看起来像是引用引用的可打印编码文本,已经过urlencoded。

import quopri
from urllib import parse

s = 'href%3D"https://post.pinterest=\r\n.com/f/a/WRi5L7G_wfTW1BovkyUGuw~~/AAAAAQA~/RgRe6WEYPwRXCXBpbnRlcmVzdEIKABwY=\r\n3AZdrwvFllIXdHVyZ2VvbmNocmlzM0BnbWFpbC5jb21YBAAAAAA~?target=3Dhttps%3A%2F%2=\r\nFwww.pinterest.com%2Fsecure%2Fautologin%2F%3Fod%3DFux7G1fLpQxdgu%252FAlq7%2=\r\n52FO0wnXhG3mrIvODBVUav9ko5yjUdnc84zWzwWN%252BPJyxYElh86K0WCnm9Th%252F6kUWW%=\r\n252FfcKmC7yJz0qo50Ss4EaaUahZGfo19MQS%252BIeP4Dlvz0hgCjvxIS4R%252BPMAF%252FG=\r\nl9BpWrQ%253D%253D%26user_id%3DNjEwMDk3MjE4MTc4OTE0MjA0%26next%3D%252Fpin%25=\r\n2F806707351985179613%252F%253Futm_campaign%253Dpopular_pins%2526e_t%253De5a=\r\nb90da0abf493b944b3c27261acfe3%2526utm_content%253D806707351985179613%2526ut=\r\nm_source%253D31%2526utm_term%253D1%2526utm_medium%253D2012'

parse.unquote_plus(quopri.decodestring(s).decode('utf-8'))
'href="https://post.pinterest.com/f/a/WRi5L7G_wfTW1BovkyUGuw~~/AAAAAQA~/RgRe6WEYPwRXCXBpbnRlcmVzdEIKABwY3AZdrwvFllIXdHVyZ2VvbmNocmlzM0BnbWFpbC5jb21YBAAAAAA~?target=https://www.pinterest.com/secure/autologin/?od=Fux7G1fLpQxdgu%2FAlq7%2FO0wnXhG3mrIvODBVUav9ko5yjUdnc84zWzwWN%2BPJyxYElh86K0WCnm9Th%2F6kUWW%2FfcKmC7yJz0qo50Ss4EaaUahZGfo19MQS%2BIeP4Dlvz0hgCjvxIS4R%2BPMAF%2FGl9BpWrQ%3D%3D&user_id=NjEwMDk3MjE4MTc4OTE0MjA0&next=%2Fpin%2F806707351985179613%2F%3Futm_campaign%3Dpopular_pins%26e_t%3De5ab90da0abf493b944b3c27261acfe3%26utm_content%3D806707351985179613%26utm_source%3D31%26utm_term%3D1%26utm_medium%3D2012'