A Little Help on Importing Gmail Contacts using Ruby on Rails
We came across a fabulous article on getting your RoR app to pull contacts from your Gmail account at Atlantic Domain Solutions: Import Gmail Contacts using Ruby on Rails. Kudos to Atlantic Domain Solutions! This is a really great article. The only slight issue is that it omitted a couple of details which may confuse the newbie - in particular, where does all the code in points 1, 2 and 3 go and how do the code snippets relate to each other.
So here’s a set of clarifications to the original article (a ‘meta-guide’ if you will), as we didn’t want to just rip off these nice folks source code. Thus read these clarifications in conjunction with the original Atlantic Domain Solutions guide.
We’ll assume that you know how to create a new rails app from scratch and create a controller. While you’re following this you can use localhost as your server (eg. the default localhost:3000).
Create yourself a controller called ImportedContacts
class ImportedContactsController < ApplicationController
def authenticate
# Put the code from point 1 in the Atlantic Domain Solutions article here.
# Note:
# Set the next_param to the follow on controller action. For example,
# next_param = url_for(:action => 'authorise')
end
def authorise
# Put the code from point 2 in the article here.
# Note:
# I changed some of the code as shown below...
# if resp.code == "200"
# token = ''
# data.split.each do |str|
# if not (str =~ /Token=/).nil?
# token = str.gsub(/Token=/, '')
# end
# end
# redirect_to(:action => 'import', :token => token)
# else
# redirect_to ('/')
# end
end
def import
# Put the code point 3 from the article here.
# Some notes:
# Put the line 'authsub_token = params[:token]' at around line 4 of this method
# (just after the two requires). It was accidentally omitted in the original article.
# I also changed the local variable 'contacts' to '@contacts' so that it is available to views
end
end
To see the contacts after importing, I created a folder called ‘imported_contacts’ under app/views and placed the file import.rhtml in it, containing the following code.
<table>
<%= @contacts.map { |c| "<tr><td>Name: #{c['name']}</td><td>Email: #{c['email']}</td></tr>" } %>
</table>
Ok! You should be ready to rock! Ensure that you are not logged into gmail and visit http://localhost:3000/imported_contacts/authenticate
You should be brought to a Google account login page. Login and you will be brought to the Google page for allowing third party access to the Google API. Click ‘Grant’ and you should be brought to your app’s import.rhtml page which should hopefully be displaying all your lovely contacts! Hurrah!
July 2nd, 2008 at 3:37 am
thanks for nice followup article on this
July 10th, 2008 at 5:19 pm
Nice guide, thanks. To be a real nitpicker, you probably also want to add a line the end of the import method, along the lines of:
@contacts = contacts
Also remember if you’re using VI everything will break.
October 22nd, 2008 at 9:11 am
Just a quick word. When you go to deploy your app, localhost:3000 won’t work anymore for contacting the Gmail API. You’d need to register your Web Application with Google (this is outlined in the Atlantic Domain Solutions guide). As part of this you are asked to enter a Target URL path prefix (ie. the next parameter used in AuthSub). You should set this to be the path to the above controller action. So, for example, if your web app is lives at http://www.mycoolwebsite.net then the value for the Target URL path prefix should be
http://www.mycoolwebsite.net/imported_contacts/authorise
Pay particular attention to not forget the ‘www’ bit if that will be in your URLs!