You’ve probably heard about Google launching OAuth to access Gmail and Google Apps Mail.
Developers can now grab emails thanks to the IMAP protocol, without asking for a password. It’s awesome to see an “old” technology, IMAP, meeting a new one, OAuth. And no need to use a new API.
To help other Ruby developers integrating Gmail+OAuth, we published a new gem, gmail_xoauth. Once installed, you get updated Net::IMAP and Net::SMTP libraries, ready to authenticate via XOAUTH. And of course, it works with Google Apps too.
To authorize on ‘imap.gmail.com’, instead of giving a string password, you give a hash of options so the SASL Initial Client Request will be generated and sent over the wires for you.
require 'gmail_xoauth' imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false) imap.authenticate('XOAUTH', 'roger.moore@gmail.com', :consumer_key => 'anonymous', :consumer_secret => 'anonymous', :token => '4/nM2QAaunKUINb4RrXPC55F-mix_k', :token_secret => '41r18IyXjIvuyabS/NDyW6+m' ) messages_count = imap.status('INBOX', ['MESSAGES'])['MESSAGES'] puts "Seeing #{messages_count} messages in INBOX"
The principle is the same for SMTP:
require 'mail' require 'gmail_xoauth' mail = Mail.new do from 'roger.moore@gmail.com' to 'marcel@amont.com' subject 'This is a test email' body 'Hi!' end smtp = Net::SMTP.new('smtp.gmail.com', 587) smtp.enable_starttls_auto secret = { :consumer_key => 'anonymous', :consumer_secret => 'anonymous', :token => '4/nM2QAaunKUINb4RrXPC55F-mix_k', :token_secret => '41r18IyXjIvuyabS/NDyW6+m' } smtp.start('gmail.com', 'roger.moore@gmail.com', secret, :xoauth) do |session| session.send_message(mail.encoded, mail.from_addrs.first, mail.destinations) end
Feel free to fork the public github repository and add new features !
Note: we just launched OAuth support for Gmail and Google Apps Mail on Silentale.
