【问题标题】:Gmail API (Python) - Unable to add a reply to the threadGmail API (Python) - 无法向线程添加回复
【发布时间】:2019-04-12 14:10:07
【问题描述】:

我想通过 API 添加对 gmail 线程的回复。

Google says to:

只需将与线程 ID 配对的 threadId 键添加到消息的元数据,即消息对象。

这就是我正在尝试的:

def create_message(sender, to, cc, bcc, subject, message_text, file=None, thread=None):
    message = MIMEMultipart()
    print(thread)
    message['to'] = to
    if cc:
        message['cc'] = cc
    if bcc:
        message['bcc'] = bcc
    if thread:
        message['threadId'] = thread
    message['from'] = sender
    message['subject'] = subject
    msg = MIMEText(message_text, 'html')
    message.attach(msg)
    if file:
        message = attach_file(message, file)
    return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}

但它不起作用。我不知道该怎么办。

【问题讨论】:

    标签: gmail-api google-api-python-client


    【解决方案1】:

    我终于设法让它工作了。 threadId 需要从 message 字典移动到 output 本身。

    希望这可以为其他人解决问题:

    def create_message(sender, to, cc, bcc, subject, message_text, file=None, thread=None):
        message = MIMEMultipart()
        message['to'] = to
        if cc:
            message['cc'] = cc
        if bcc:
            message['bcc'] = bcc
        message['from'] = sender
        message['subject'] = subject
        msg = MIMEText(message_text, 'html')
        message.attach(msg)
        if file:
            message = attach_file(message, file)
        output =  {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
        if thread:
            output['threadId'] = thread
        return output
    

    【讨论】:

    • 在我的情况下不起作用,它仍然会遇到一个新线程。请确认我两件事... 1.您是如何设法实时获取 threadId 的? (例如通过主题行或类似内容搜索threadid)和 2. 您在返回输出后执行了哪个函数?
    • 这假定您在执行函数时具有 threadId。threadId 包含在您从 API 获得的消息中发送的信息中。我认为没有办法让 API 决定您要回复哪个线程,您必须告诉它。至于输出,下一步是创建草稿。请参阅文档中 Python 选项卡下的示例:developers.google.com/gmail/api/guides/drafts
    猜你喜欢
    • 2019-04-08
    • 2017-09-13
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2019-07-10
    • 2014-08-17
    • 2015-09-26
    相关资源
    最近更新 更多