Last active
March 31, 2021 07:19
-
-
Save pramodshinde/7a8bbea1b9c9fa872aba to your computer and use it in GitHub Desktop.
Gmail API (via IMAP) using gmail_xoauth - Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An example demonstrating Gmail API via IMAP using gmail_xoauth gem | |
# Add this file to your rails project to use if you don't want to use this in rails project then require | |
require 'net/imap' | |
require 'gmail_xoauth' | |
class Gmail | |
attr_accessor :email, :access_token | |
def initialize(args) | |
email = args[:email] | |
access_token = args[:access_token] | |
end | |
# xoauth_imap_authorize | |
# authorizes access to user's @email mailbox with @access_token | |
def xoauth_imap_authorize | |
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false) | |
imap.authenticate('XOAUTH2', @email, @access_token) | |
imap.select("[Gmail]/All Mail") | |
imap | |
end | |
def fetch_email | |
imap = xoauth_imap_authorize | |
# searches email message with subject "hello" and not a "New" messages | |
# Response | |
# [7293, 7474, 7475, 7476, 7555, 7557, 7558, 7559, 7573, 7675, 7702, 7703, 7796] | |
# alternatively you can use various search keys | |
# specified at http://ruby-doc.org/stdlib-2.0/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-searchlike | |
message_ids = imap.search(["SUBJECT", "hello", "NOT", "NEW"]) | |
# fetches a message envelope by specific message_id | |
imap.fetch(message_ids[0], "ENVELOPE") | |
end | |
end | |
# Note: | |
# access_token is user access token got from Gmail after authorizing user via Google using omniauth-google-oauth2 | |
client = Gmail.new(email: "[email protected]", access_token: "user-access-token-XXXX") | |
response = client.fetch_email | |
# Sample Response | |
# [#<struct Net::IMAP::FetchData | |
# seqno=7293 | |
# attr= | |
# {"ENVELOPE"=> | |
# #<struct Net::IMAP::Envelope | |
# date="Tue, 14 Oct 2014 11:02:07 -0400 (EDT)", | |
# subject= | |
# "hello", | |
# from= | |
# [#<struct Net::IMAP::Address | |
# name="hello", | |
# route=nil, | |
# mailbox="inbox", | |
# host="sender.com">], | |
# sender= | |
# [#<struct Net::IMAP::Address | |
# name="hello", | |
# route=nil, | |
# mailbox="inbox", | |
# host="sender.com">], | |
# reply_to= | |
# [#<struct Net::IMAP::Address | |
# name="hello", | |
# route=nil, | |
# mailbox="inbox", | |
# host="sender.com">], | |
# to= | |
# [#<struct Net::IMAP::Address | |
# name=nil, | |
# route=nil, | |
# mailbox="myname", | |
# host="receiver.com">], | |
# cc=nil, | |
# bcc=nil, | |
# in_reply_to=nil, | |
# message_id="<[email protected]>">}> | |
# ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ENV["GOOGLE_CLIENT_ID"] and ENV["GOOGLE_CLIENT_SECRET"] can be obtained | |
# from https://console.developers.google.com/project | |
# after registering google project on developers console | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], | |
{ | |
:scope => "userinfo.email, userinfo.profile, calendar https://mail.google.com", | |
:prompt => "consent" | |
} | |
end | |
# **OPTIONALLY** you can put this is in config/initializers/devise.rb if you are using devise | |
config.omniauth :google_oauth2, | |
ENV["GOOGLE_CLIENT_ID", | |
ENV["GOOGLE_CLIENT_SECRET"], | |
{ | |
scope: "userinfo.email, userinfo.profile, calendar https://mail.google.com", | |
prompt: "consent" | |
} | |
# Omniauth callback will receive follwoing sample omniauth hash in request.env["omniauth.auth"] | |
# from this you can get the access token required to query Gmail API | |
access_token = request.env["omniauth.auth"][:credentials][:token] | |
# The sample auth hash looks like this | |
{ | |
:provider => "google_oauth2", | |
:uid => "123456789", | |
:info => { | |
:name => "John Doe", | |
:email => "john@company_name.com", | |
:first_name => "John", | |
:last_name => "Doe", | |
:image => "https://lh3.googleusercontent.com/url/photo.jpg" | |
}, | |
:credentials => { | |
:token => "token", | |
:refresh_token => "another_token", | |
:expires_at => 1354920555, | |
:expires => true | |
}, | |
:extra => { | |
:raw_info => { | |
:sub => "123456789", | |
:email => "[email protected]", | |
:email_verified => true, | |
:name => "John Doe", | |
:given_name => "John", | |
:family_name => "Doe", | |
:profile => "https://plus.google.com/123456789", | |
:picture => "https://lh3.googleusercontent.com/url/photo.jpg", | |
:gender => "male", | |
:birthday => "0000-06-25", | |
:locale => "en", | |
:hd => "company_name.com" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment