【问题标题】:How to set recipients with Gmail's Node.js API如何使用 Gmail 的 Node.js API 设置收件人
【发布时间】:2017-03-21 01:25:00
【问题描述】:

尽管看起来很简单,但我似乎无法弄清楚如何使用 Google 的 Gmail API 设置草稿的收件人。 documentation 表示 users.messages Resource 对象包含一个 payload 对象,该对象包含一个 headers 对象,而 headers 对象包含名称-值对。

// example from google's gmail API documentation
"payload": {
  "partId": string,
  "mimeType": string,
  "filename": string,
  "headers": [
    {
      "name": string,
      "value": string
    }
  ],
  "body": users.messages.attachments Resource,
  "parts": [
    (MessagePart)
  ]
},

我认为您在这些标题中设置了草稿的“收件人”部分,因为文档说

此消息部分的标头列表。对于表示整个消息有效负载的顶级消息部分,它将包含标准 RFC 2822 电子邮件标头,例如 To、From 和 Subject。

但是,当我提出类似这样的请求时

"payload" : {
  "headers" : [
    {
      "name"  : "To",
      "value" : "me"
      // "me" should direct the draft to myself
    }
  ]
}

草稿的 To 部分仍为空。有什么解决方案或建议吗?

【问题讨论】:

    标签: javascript node.js gmail-api


    【解决方案1】:

    在您的请求中,您有这个:

    "headers" : [ "name" : "To", "value" : "me" ]

    "headers" 应该是一个对象数组,但您的数组不包含任何对象。

    相反,它应该是这样的:

    "headers": [ { "name": "To", "value": "me" } ]

    就像他们的例子:

    "payload": {
      "partId": string,
      "mimeType": string,
      "filename": string,
      "headers": [
        {
          "name": "To",
          "value": "me"
        }
      ],
      "body": users.messages.attachments Resource,
      "parts": [
        (MessagePart)
      ]
    },
    

    【讨论】:

    • 糟糕,这实际上是一个错字。我是这样用我的本地代码编写的,但是当我尝试创建草稿时结果相同。
    【解决方案2】:

    因此,我似乎误解了有关 Gmail API 的文档。当您向 drafts.c​​reate 发送请求时,您确实需要提供 users.messages Resource,但是,并非所有内容都是可写的。只有threadIdlabelIdsraw 是可写对象。事实证明,您根本不应该使用有效负载来设置ToFrom 等。您应该将它们包含在您的原始文件中。

    我的新代码看起来像这样

    let create = (toAddress, subject, content, callback) => {
      gmail.users.drafts.create(
        {
          'userId'  : 'me',
          'resource' : {
            'message' : {
              'raw' : base64.encodeURI(
                        `To:${toAddress}\r\n` + // Who were are sending to
                        `Subject:${subject}\r\n` + // Subject
                        `Date:\r\n` + // Removing timestamp
                        `Message-Id:\r\n` + // Removing message id
                        `From:\r\n` + // Removing from
                        `${content}` // Adding our actual message
                      )
            }
          }
        },
        (err, response) => {
          // Do stuff with response
          callback(err, response);
        }
      )
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 2016-05-23
      • 1970-01-01
      • 2021-01-27
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2016-08-21
      相关资源
      最近更新 更多