Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Google's OAuth1 protocol is now deprecated and many gems have not yet updated to use their OAuth2 protocol. Here is a working example of fetching email from Google using their OAuth2 protocol. This example uses the <a href="https://github.com/mikel/mail" rel="nofollow"><code>mail</code></a>, <a href="https://github.com/nfo/gmail_xoauth" rel="nofollow"><code>gmail_xoauth</code></a>, <a href="http://www.omniauth.org/" rel="nofollow"><code>omniauth</code></a>, and <a href="https://github.com/zquestz/omniauth-google-oauth2" rel="nofollow"><code>omniauth-google-oauth2</code></a> gems.</p> <p>You will also need to register your app in <a href="http://code.google.com/apis/console" rel="nofollow">Google's API console</a> in order to get your API tokens.</p> <pre><code># 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 # ...after handling login with OmniAuth... # 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload