如何使用Omniauth实现Gmail IMAP [英] How to implement Gmail IMAP with Omniauth
问题描述
我已阅读了通过XOAUTH连接到Google Gmail的几个绝望的信息来源:
http://code.google.com/apis/gmail/oauth/protocol.html#map
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
而我尝试使用实现IMAP的'gmail'gem:
https://github.com/nu7hatch / gmail
And I'm trying the use the 'gmail' gem which implements IMAP: https://github.com/nu7hatch/gmail
最后,处理认证的ominauth:
https://github.com/Yesware/omniauth-google
Finally, ominauth for handling the authentication: https://github.com/Yesware/omniauth-google
我怎样才能将这些代码一起使某些东西可用?
请让我知道任何真实世界的实现,这里有一些连接到Gmail的例子:
http://otherinbox.com
http://goslice.com
推荐答案
我现在不喜欢使用现有的宝石,因为现在已弃用Google的XOAUTH。您应该使用他们的新XOAUTH2。
I had trouble, like you, using existing gems since Google's XOAUTH is now deprecated. You should use their new XOAUTH2.
以下是一个使用XOAUTH2协议从Google获取电子邮件的工作示例。本示例使用 邮件
, gmail_xoauth
, omniauth
和 omniauth-google-oauth2
gems。
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.
您还需要在 Google的API控制台上注册您的应用,以获得您的API令牌。
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
这篇关于如何使用Omniauth实现Gmail IMAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!