当我使用Gmail API Node.js客户端发送一封附件大于5MB的电子邮件时,我得到一个错误消息"413请求实体太大“。
我首先创建了一个字符串mimeMessage,其中包含多部分/混合类型的MIME消息。此邮件的一部分是大小大于5MB的base64编码附件。然后我试着发送它:
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请求实体太大“。
根据应用编程接口文档,应该使用可恢复上传(https://developers.google.com/gmail/api/guides/uploads#resumable)。但是文档只给出了超文本传输协议请求的示例,并没有描述如何使用Node.js客户端来实现。我希望避免将对google-api-nodejs-client的调用与HTTP请求混淆。如果这是无法避免的,我会非常感谢一个很好的例子如何在Node.js中做到这一点。
我尝试将uploadType设置为resumable:
gmailApi.users.messages.send({
userId: 'me',
uploadType: 'resumable',
resource: { raw: encodedMimeMessage }
}, (err, res) => {
...
});
我从服务器响应中看到它在查询字符串中结束,但它没有解决问题。
我在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)和Python (Error 10053 When Sending Large Attachments using Gmail API)中找到了示例。但是他们分别使用了'Google_Http_MediaFileUpload','MediaHttpUploader‘和'MediaIoBaseUpload’,我不知道如何将其应用到nodejs客户端。
我在Python (Using Gmail API to Send Attachments Larger than 10mb)中找到了一个使用uploadType = 'multipart‘的示例和一个不是base64编码的消息。但是当我没有对消息进行base64编码时,我总是得到一个错误响应。
发布于 2019-04-14 21:12:12
如果其他人遇到这个问题:在发送电子邮件时,不要使用第一个方法参数的resource.raw
属性。请改用media
属性:
const request = {
userId: 'me',
resource: {},
media: {mimeType: 'message/rfc822', body: mimeMessage}
};
gmailApi.users.messages.send(request, (err, res) => {
...
});
在这种情况下,不得使用base64对mimeMessage
进行编码。在resource
中,您可以选择指定属性threadId
。
const request = {
userId: 'me',
resource: {threadId: '...'},
media: {mimeType: 'message/rfc822', body: mimeMessage}
};
gmailApi.users.messages.send(request, (err, res) => {
...
});
https://stackoverflow.com/questions/55639575
复制相似问题