Skip to content

Instantly share code, notes, and snippets.

@rtdp
Created December 15, 2010 19:27
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rtdp/742461 to your computer and use it in GitHub Desktop.
Save rtdp/742461 to your computer and use it in GitHub Desktop.
Importing Gmail Contacts list to Rails Application.
class ImportedContactsController << ApplicationController
require 'net/http'
require 'net/https'
require 'uri'
#THIS METHOD TO SEND USER TO THE GOOGLE AUTHENTICATION PAGE.
def authenticate
# initiate authentication w/ gmail
# create url with url-encoded params to initiate connection with contacts api
# next - The URL of the page that Google should redirect the user to after authentication.
# scope - Indicates that the application is requesting a token to access contacts feeds.
# secure - Indicates whether the client is requesting a secure token.
# session - Indicates whether the token returned can be exchanged for a multi-use (session) token.
next_param = PLANNER_HOST.to_s + authorise_imported_contacts_path.to_s
scope_param = &quot;https%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2F&quot;
session_param = &quot;1&quot;
root_url = &quot;https://www.google.com/accounts/AuthSubRequest&quot;
#TO FIND MORE AOBUT THESE PARAMTERS AND TEHRE MEANING SEE GOOGLE API DOCS.
query_string = &quot;?scope=#{scope_param}&amp;session=#{session_param}&amp;next=#{next_param}&quot;
redirect_to root_url + query_string
end
# YOU WILL BE REDIRECTED TO THIS ACTION AFTER COMPLETION OF AUTHENTICATION PROCESS WITH TEMPORARY SINGLE USE AUTH TOKEN.
def authorise
#FIRST SINGEL USE TOKEN WILL BE RECEIVED HERE..
token = params[:token]
#PREPAIRING FOR SECOND REQUEST WITH AUTH TOKEN IN HEADER.. WHICH WILL BE EXCHANED FOR PERMANENT AUTH TOKEN.
uri = URI.parse(&quot;https://www.google.com&quot;)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
path = '/accounts/AuthSubSessionToken'
headers = {'Authorization' =&gt; &quot;AuthSub token=#{token}&quot;}
#GET REQUEST ON URI WITH SPECIFIED PATH...
resp, data = http.get(path, headers)
#SPLIT OUT TOKEN FROM RESPONSE DATA.
if resp.code == &quot;200&quot;
token = ''
data.split.each do |str|
if not (str =~ /Token=/).nil?
token = str.gsub(/Token=/, '')
end
end
return redirect_to(:action =&gt; 'import', :token =&gt; token)
else
redirect_to PLANNER_HOST.to_s + planner_path.to_s
end
end
#USING PERMANENT TOKEN IN THIS ACTION TO GET USER CONTACT DATA.
def import
# GET http://www.google.com/m8/feeds/contacts/default/base
token = params[:token]
uri = URI.parse(&quot;https://www.google.com&quot;)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
path = &quot;/m8/feeds/contacts/default/full?max-results=10000&quot;
headers = {'Authorization' =&gt; &quot;AuthSub token=#{token}&quot;,
'GData-Version' =&gt; &quot;3.0&quot;}
resp, data = http.get(path, headers)
# extract the name and email address from the response data
# HERE USING REXML TO PARSE GOOGLE GIVEN XML DATA
xml = REXML::Document.new(data)
contacts = []
xml.elements.each('//entry') do |entry|
person = {}
person['name'] = entry.elements['title'].text
gd_email = entry.elements['gd:email']
person['email'] = gd_email.attributes['address'] if gd_email
@imported_contact = current_user.imported_contacts.create(person)
end
redirect_to applications_planner_path
end
@miketucker
Copy link

formatting got screwed up, here's a fixed version:

https://gist.github.com/852849

@wrtdp
Copy link

wrtdp commented Mar 4, 2011

Thanks dude.. :)

@miketucker
Copy link

after about 4 hours of getting nowhere with omniauth, i found your code and it worked, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment