【问题标题】:Send gmail messages with google-api-ruby-client '0.9.pre3'使用 google-api-ruby-client '0.9.pre3' 发送 gmail 消息
【发布时间】:2015-10-21 22:07:47
【问题描述】:

在 rails 4 应用程序中使用较新的 google-api-ruby-client 发送 gmail。

require 'google/apis/gmail_v1'

Gmail = Google::Apis::GmailV1

class MailService

  def initialize(params)
    @params = params
  end

  def call
    message = Gmail::Message.new
    service = Gmail::GmailService.new
    message.raw = (redacted)
    service.request_options.authorization = current_user.token.fresh_token
    result = service.send_user_message(current_user.email, message)
  end

end

这是调用 API 的结果:

Sending HTTP post https://www.googleapis.com/gmail/v1/users/me/messages/send?
200
#<Hurley::Response POST https://www.googleapis.com/gmail/v1/users/me/messages/send == 200 (63 bytes) 858ms>
Success - #<Google::Apis::GmailV1::Message:0x007fc9cf9b52dd
 @id="15096369c05cdb1d",
 @thread_id="15096369c05cdb1d">

原始消息从 API 资源管理器发送没有问题,但从我的应用程序执行时,我在收件箱中收到一封退回电子邮件。在上面的示例中,经过编辑的示例是有效的 RFC 2822 格式的 base-64 url​​ 安全字符串,fresh_token 表示当前用户的 oauth2 访问令牌。

查看退回的邮件

Bounce <nobody@gmail.com>
2:43 PM (19 minutes ago)
to me

An error occurred. Your message was not sent.

有人有什么想法吗?似乎我的(发件人)电子邮件可能在原始消息中被提取,但不是收件人......虽然我认为 API 可能会根据我的 oauth 访问令牌转发退回邮件。

我非常感谢任何帮助。谢谢!

编辑:解决方案是将 RFC 2822 字符串作为原始属性传递,而不使用 base64 编码。

【问题讨论】:

  • 您是否尝试过提供常量字符串'me' 而不是current_user.email
  • 是的,试过了,结果一样。电子邮件仍然退回。
  • 通常用于测试的 VCR gem 也可以方便地用于准确捕获 gem 发送的 HTTP 请求,因此您可以看到它们与您的不同。听起来有点像宝石中的错误。您是否尝试过将问题发布到 gem 的 github 存储库?
  • 跳过 base64 编码。 ruby 客户端会为您完成。
  • jrochkind:这实际上是一个非常棒的想法。我根本没想过。

标签: ruby gmail-api google-api-ruby-client


【解决方案1】:

Steve Bazyl 似乎是正确的。从 (0.9.13) 开始,关于 send_user_message 的文档是错误的。对于 raw,它说:“RFC 2822 格式和 base64url 编码字符串中的整个电子邮件消息。当提供 format=RAW 参数时,在 messages.get 和 drafts.get 响应中返回。对应于 JSON 属性 raw。”据我所知,这简直是不正确

我在从 google-api-client 0.8 更新到 0.9 并删除 base64 编码解决了这个问题时遇到了这个问题。 IE。调用 0.8:

response = @service.execute(
  api_method: api.users.messages.to_h['gmail.users.messages.send'],
  body_object: {
    raw: Base64.urlsafe_encode64(mail.to_s)
  },
  parameters: {
    userId: 'me',
  }
)

成为

message = { raw: mail.to_s }
res = @service.send_user_message('me', message, {})

在 0.9 中。

报告为https://github.com/google/google-api-ruby-client/issues/474

【讨论】:

    猜你喜欢
    • 2014-10-13
    • 1970-01-01
    • 2014-09-16
    • 2017-03-03
    • 1970-01-01
    • 2020-07-12
    • 2016-12-31
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多