Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何获取电子邮件gmail python API的主题_Python 3.x_Google Apps Script_Gmail_Python 3.9 - Fatal编程技术网

Python 3.x 如何获取电子邮件gmail python API的主题

Python 3.x 如何获取电子邮件gmail python API的主题,python-3.x,google-apps-script,gmail,python-3.9,Python 3.x,Google Apps Script,Gmail,Python 3.9,这是到目前为止我的代码,其中包含获取凭据和所有导入的部分。它获取最新电子邮件的正文,但没有标签“READ-BY-SCRIPT”,也没有标签“收件箱”。如何获取电子邮件的主题而不是正文?请查看,然后 结构如下: def getbodyinbox(): service = build('gmail', 'v1', credentials=creds) label_name = "READ-BY-SCRIPT" label_id = 'Label_85075

这是到目前为止我的代码,其中包含获取凭据和所有导入的部分。它获取最新电子邮件的正文,但没有标签“READ-BY-SCRIPT”,也没有标签“收件箱”。如何获取电子邮件的主题而不是正文?

请查看,然后 结构如下:

def getbodyinbox():
    service = build('gmail', 'v1', credentials=creds)
    label_name = "READ-BY-SCRIPT"
    label_id = 'Label_8507504117657095973'
    results = service.users().messages().list(
        userId='me', q="-label:"+label_name, maxResults=1).execute()
    messages = results.get('messages', [])
    body = []
    if not messages:
        body = "no messages"
        return body
    else:
        for message in messages:
            msg = service.users().messages().get(
                userId='me', id=message['id']).execute()
            labels = msg['labelIds']
            if "INBOX" in labels:
                headers = msg['payload']['headers']
                headers = str(headers)
                print(headers)
                if "class_ix" in headers:
                    body.append(msg['payload']['parts'])
                    if 'data' in body[0][0]['body']:
                        body = base64.urlsafe_b64decode(
                            body[0][0]['body']['data'])
                    elif 'data' in body[0][1]['body']:
                        body = base64.urlsafe_b64decode(
                            body[0][1]['body']['data'])
                    body = str(body)

                        
                    return body


print(getbodyinbox())
以及:

换句话说,主题包含在标题中

您可以使用

headers=msg['payload']['headers']
如果i[“name”]=“subject”],则标题中的i的subject=[i['value']

它输出此错误:“文件”//Users/me/Coding/Gmail/getBodyInbox.py”,第77行,在打印(getBodyInbox())文件“/Users/mel/Coding/Gmail/getBodyInbox.py”中,第62行,在getBodyInbox subject中,如果i[“name”==“subject”]文件“/Users/mel/Coding/Gmail/getBodyInbox.py”,在subject=[i]中,则标题中的i=[i['value']表示i如果i[“name”]=“Subject”]TypeError:字符串索引必须是整数```您能说明如何将其集成到代码的其余部分吗?有关更完整的示例,请参阅。我再次运行了代码,它刚刚修复了自己?现在看起来不错。
  "payload": {
    "partId": string,
    "mimeType": string,
    "filename": string,
    "headers": [
      {
        "name": string,
        "value": string
      }
    ],