如何使用 Omniauth 实施 Gmail IMAP

发布于 2025-01-01 07:08:32 字数 712 浏览 0 评论 0 原文

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

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

最后,ominauth 用于处理身份验证: https://github.com/Yesware/omniauth-google

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

I've read through several desperate sources of information on connecting to Google's Gmail through XOAUTH:
http://code.google.com/apis/gmail/oauth/protocol.html#imap

And I'm trying the use the 'gmail' gem which implements IMAP:
https://github.com/nu7hatch/gmail

Finally, ominauth for handling the authentication:
https://github.com/Yesware/omniauth-google

How do I actually tie these codes together to make something usable?
Please let me know of any real world implementations, here's some examples of connecting to Gmail:
http://otherinbox.com
http://goslice.com

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2025-01-08 07:08:32

我和你一样,在使用现有的 gems 时遇到了麻烦,因为 Google 的 XOAUTH 现已被弃用。您应该使用他们的新 XOAUTH2。

以下是使用 XOAUTH2 协议从 Google 获取电子邮件的工作示例。此示例使用 mailgmail_xoauthomniauth omn​​iauth-google-oauth2 宝石。

您还需要在 Google 的 API 控制台中注册您的应用才能获取 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

I had trouble, like you, using existing gems since Google's XOAUTH is now deprecated. You should use their new XOAUTH2.

Here is a working example of fetching email from Google using their XOAUTH2 protocol. This example uses the mail, gmail_xoauth, omniauth, and omniauth-google-oauth2 gems.

You will also need to register your app in Google's API console in order to get your API tokens.

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文