首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Gmail API发送大于10mb的附件

使用Gmail API发送大于10mb的附件
EN

Stack Overflow用户
提问于 2018-08-07 03:47:06
回答 1查看 415关注 0票数 2

我正在努力寻找一个好的例子,说明通过Gmail API发送包含超过10mb附件的电子邮件所需的完整请求集。

我见过https://developers.google.com/gmail/api/v1/reference/users/messages/sendhttps://developers.google.com/gmail/api/guides/uploads#resumable,但没有任何东西将它们联系在一起。

我们正在使用ruby客户端,但是我们无法完成这个流程。使用下面的代码,我们在尝试发出第二个请求时得到以下错误:Google::APIClient::ClientError: Recipient address required

响应的完整正文如下:

代码语言:javascript
运行
复制
{"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"invalidArgument", "message"=>"Recipient address required"}], "code"=>400, "message"=>"Recipient address required"}}

以下是用于生成请求的代码:

代码语言:javascript
运行
复制
raw = Base64.urlsafe_encode64 message_string
result1 = api_client.execute!(
  :api_method => gmail_api.users.messages.to_h['gmail.users.messages.send'],
  :parameters => {
    :uploadType => 'resumable',
    :userId => 'me'
  },
  :headers => {
    'Content-Type' => 'application/json',
    'X-Upload-Content-Type' => 'message/rfc822',
    'X-Upload-Content-Length' => raw.bytesize.to_s
  }
)

upload_id = result1.headers['x-guploader-uploadid']

result2 = api_client.execute!(
  :api_method => gmail_api.users.messages.to_h['upload.gmail.users.messages.send'],
  :body_object => {
    :raw => raw
  },
  :parameters => {
    :uploadType => 'resumable',
    :upload_id => upload_id,
    :userId => 'me'
  },
  :headers => {
    'Content-Type' => message.content_type,
    'Content-Length' => raw.bytesize.to_s
  }
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-07 21:55:11

所以(感谢@tholle)的问题是,当发送大于5mb和小于35mb的附件(但也适用于没有附件的消息)时,您不会对请求的正文进行base64编码,而是使用multipart作为uploadType。不幸的是,文档根本没有提到这一点,错误消息也没有指明这一点。

这是一个可以发送20mb附件的工作示例。希望这篇文章能帮助那些浪费了无数时间试图弄清楚这个问题的人!

代码语言:javascript
运行
复制
  result = api_client.execute!(
    :api_method => gmail_api.users.messages.to_h['gmail.users.messages.send'],
    :body => rfc822_message_string,
    :parameters => {
      :uploadType => 'multipart',
      :userId => 'me'
    },
    :headers => {
      'Content-Type' => 'message/rfc822',
    }
  )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51714674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档