【问题标题】:python 3.x - Gmail api get title and senderpython 3.x - Gmail api 获取标题和发件人
【发布时间】:2017-06-05 12:46:45
【问题描述】:

我刚刚开始使用 Gmail API,并按照 Oauth2.0 教程获取授权代码。到目前为止我的代码是:

from oauth2client import client
import webbrowser
import httplib2

flow = client.flow_from_clientsecrets(
    'client_secrets.json',
    scope='https://www.googleapis.com/auth/gmail.readonly',
    redirect_uri='urn:ietf:wg:oauth:2.0:oob')

auth_uri = flow.step1_get_authorize_url()
webbrowser.open_new(auth_uri)

authcode = input("Enter auth code: ")

credentials = flow.step2_exchange(authcode)
http_auth = credentials.authorize(httplib2.Http())

我想通过发出请求来测试 API,该请求以这种格式获取最新未读消息的主题和发件人:

#pseudocode
get latestMessage
if latestMessage.read == True:
    print("you have no unread messages)
else:
    print("you have a new email: " + latestMessage.subject + " from" + latestmessage.sender)

【问题讨论】:

    标签: python python-3.x gmail-api


    【解决方案1】:

    我创建了一个模板脚本 (Python 3.5) 来访问 Gmail 并获取日期、主题、sn-p、发件人电子邮件和邮件正文等参数。

    您可以在我的 github 存储库中查看它,如果您愿意,可以分叉。链接是https://github.com/abhishekchhibber/Gmail-Api-through-Python

    【讨论】:

    • 感谢您,其他人在查看此线程时可能会发现这很有用。我也会检查一下,看看我所做的工作效率是否可以提高!
    【解决方案2】:

    here 所述,如果您想要像 To、From、Subject 这样的标题,那么您可以调用 messages.get(format=METADATA) 或者以您使用的语言显示。您可以查看文档中的example

    Returns:
        An object containing a base64url encoded email object.
      """
    
      message = MIMEText(message_text)
      message['to'] = to
      message['from'] = sender
      message['subject'] = subject
      return {'raw': base64.urlsafe_b64encode(message.as_string())}
    

    您也可以查看SO question 中关于如何获取电子邮件发件人的答案。

    【讨论】:

    • 谢谢,您的回答使我朝着正确的方向前进,尽管我认为您可能打算链接here :)
    猜你喜欢
    • 2011-11-11
    • 1970-01-01
    • 2015-07-22
    • 2016-01-08
    • 2021-04-27
    • 2016-12-23
    • 2014-09-07
    • 2020-01-13
    • 2021-04-19
    相关资源
    最近更新 更多