hag*_*ope 3 gmail ruby-on-rails omniauth
我已经阅读了几个关于通过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
我和你一样有困难使用现有的宝石,因为Google的XOAUTH现已弃用.你应该使用他们的新XOAUTH2.
以下是使用XOAUTH2协议从Google获取电子邮件的工作示例.本例使用mail,gmail_xoauth,omniauth,和omniauth-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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1540 次 |
| 最近记录: |