【问题标题】:How to implement Gmail IMAP with Omniauth如何使用 Omniauth 实现 Gmail IMAP
【发布时间】:2012-01-31 18:12:19
【问题描述】:

我已经阅读了几个关于通过 XOAUTH 连接到 Google 的 Gmail 的绝望信息来源: http://code.google.com/apis/gmail/oauth/protocol.html#imap

我正在尝试使用实现 IMAP 的“gmail”gem: https://github.com/nu7hatch/gmail

最后,省略处理身份验证: https://github.com/Yesware/omniauth-google

我实际上如何将这些代码结合在一起以使某些东西可用? 请让我知道任何现实世界的实现,这里有一些连接到 Gmail 的示例: http://otherinbox.com http://goslice.com

【问题讨论】:

  • 我遇到了同样的问题,我一直在努力完成这项工作,但我做不到

标签: ruby-on-rails gmail omniauth


【解决方案1】:

自从 Google 的 XOAUTH 现已弃用以来,我和您一样在使用现有 gem 时遇到了麻烦。您应该使用他们的新 XOAUTH2。

这是一个使用 Google 的 XOAUTH2 协议从 Google 获取电子邮件的工作示例。此示例使用 mailgmail_xoauthomniauthomniauth-google-oauth2 宝石。

您还需要在Google's API console 中注册您的应用才能获取您的 API 令牌。

# in an initializer:
ENV['GOOGLE_KEY'] = 'yourkey'
ENV['GOOGLE_SECRET'] = 'yoursecret'
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
    scope: 'https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email'
  }

end

# in your script
email = auth_hash[:info][:email]
access_token = auth_hash[:credentials][:token]

imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', email, access_token)
imap.select('INBOX')
imap.search(['ALL']).each do |message_id|

    msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
    mail = Mail.read_from_string msg

    puts mail.subject
    puts mail.text_part.body.to_s
    puts mail.html_part.body.to_s

end

【讨论】:

  • 这真的很有帮助,谢谢。我正在使用“gmail”gem,切换到“gmail_xoauth”似乎解决了这个问题。谢谢。
  • 我认为这个解决方案不再有效。我不断收到:Net::IMAP::NoResponseError: Invalid credentials (Failure)
猜你喜欢
  • 2023-03-19
  • 2015-01-29
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 2021-09-06
  • 2016-01-12
相关资源
最近更新 更多