使用Gmail API Node.js客户端发送大型附件(大于5 MB) [英] Send large attachments (> 5 MB) with the Gmail API Node.js client

查看:82
本文介绍了使用Gmail API Node.js客户端发送大型附件(大于5 MB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Gmail API Node.js客户端发送附件大于5 MB的电子邮件时,出现错误"413请求实体太大".

When I use the Gmail API Node.js client to send an email with an attachment larger than 5 MB I get an error "413 Request Entity Too Large".

我首先创建一个字符串mimeMessage,其中包含类型为multipart/mixed的MIME消息.该消息的一部分是大小大于5 MB的base64编码附件.然后,我尝试发送它:

I first create a string mimeMessage which contains a MIME message of type multipart/mixed. One part of this message is a base64 encoded attachment with a size > 5 MB. Then I try to send it:

gmail = google.gmail({ version: 'v1', auth: authentication });

encodedMimeMessage = Buffer.from(mimeMessage)
  .toString('base64')
  .replace(/\+/g, '-')
  .replace(/\//g, '_')
  .replace(/=+$/, '');

gmail.users.messages.send({
  userId: 'me',
  resource: { raw: encodedMimeMessage }
}, (err, res) => {
  ...
});

这将导致错误响应"413请求实体太大".

This results in an error response "413 Request Entity Too Large".

根据api文档,应使用可恢复的上传( https://developers.google.com/gmail/api/guides/uploads#resumable ).但是文档仅提供HTTP请求的示例,而没有描述如何使用Node.js客户端完成请求.我想避免将对google-api-nodejs-client的调用与HTTP请求混在一起.如果无法避免这一点,我将非常感谢一个很好的示例,说明如何在Node.js中执行此操作.

According to the api documentation a resumable upload should be used (https://developers.google.com/gmail/api/guides/uploads#resumable). But the documentation only gives examples for HTTP requests and does not describe how it can be done using the Node.js client. I would like to avoid mixing calls to the google-api-nodejs-client with HTTP requests. If this cannot be avoided I would highly appreciate a good example how to do this in Node.js.

我尝试将uploadType设置为可恢复:

I tried to set uploadType to resumable:

gmailApi.users.messages.send({
  userId: 'me',
  uploadType: 'resumable',
  resource: { raw: encodedMimeMessage }
}, (err, res) => {
  ...
});

我从服务器响应中看到,它最终出现在查询字符串中,但是并不能解决问题.

I see from the server response that it ended up in the query string, but it did not solve the problem.

我在PHP中找到了示例(使用Gmail API发送大型附件如何使用gmail API发送大附件),Java( https://developers.google. com/api-client-library/java/google-api-java-client/media-upload )和Python(

I found examples in PHP (Send large attachment with Gmail API, How to send big attachments with gmail API), Java (https://developers.google.com/api-client-library/java/google-api-java-client/media-upload) and Python (Error 10053 When Sending Large Attachments using Gmail API). But they are using a 'Google_Http_MediaFileUpload', 'MediaHttpUploader' and 'MediaIoBaseUpload' respectively and I do not know how to apply this to the nodejs-client.

我在Python中找到了一个示例(使用Gmail API来发送大于10mb的附件),该附件使用uploadType ='multipart'和未经base64编码的邮件.但是当我不对消息进行base64编码时,总是会收到错误响应.

I found an example in Python (Using Gmail API to Send Attachments Larger than 10mb) which uses uploadType = 'multipart' and a message that is not base64 encoded. But I always get an error response when I do not base64 encode the message.

推荐答案

如果其他人遇到此问题:发送电子邮件时,请不要使用第一个方法参数的resource.raw属性.而是使用media属性:

In case someone else comes across this problem: Do not use the resource.raw property of the first method argument when sending an email. Instead use the media property:

const request = {
  userId: 'me',
  resource: {},
  media: {mimeType: 'message/rfc822', body: mimeMessage}
};

gmailApi.users.messages.send(request, (err, res) => {
  ...
});

在这种情况下,mimeMessage不得使用base64进行编码.在resource中,您可以选择指定属性threadId.

The mimeMessage must not be encoded with base64 in this case. In the resource you can optionally specify the property threadId.

const request = {
  userId: 'me',
  resource: {threadId: '...'},
  media: {mimeType: 'message/rfc822', body: mimeMessage}
};

gmailApi.users.messages.send(request, (err, res) => {
  ...
});

这篇关于使用Gmail API Node.js客户端发送大型附件(大于5 MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆