Using Google Contact API to import Gmail Contacts in Rails App

The Google Contacts API allows users to retrieve their email contacts from a gmail account without giving a gmail password to the requesting site. This system allows social networking site users to invite friends and find existing users they know without exposing their gmail accounts to malicious site owners or poor security practices.

The Contacts API allows developers to create, read, update, and delete contacts using the Google Data protocol, based on AtomPub," the announcement says. "It also allows for incremental sync by supporting the 'updated-min' and 'showdeleted' parameters.

邀請朋友是現今的社群網站必備的功能,許多社群網站都允許讓用戶透過GMail,Hotmail,Yahoo! Mail等郵件服務匯入自己的聯絡人列表來邀請朋友。早期在ASP.Net,JAVA,PHP這些程式語言的典型作法是透過一些Library來實作匯入聯絡人清單,而在Ruby on Rails則有Contacts Gem這類的套件可以使用;但缺點是用戶必須在第三方網站所提供的表單上鍵入自己的GMail帳號密碼,如此一來安全性問題就產生了,儘管提供此類功能的網站都會在旁邊註明不會儲存用戶的帳號密碼,但是你真的信的過這些網站嗎?所幸Google Contacts API的推出解決了這個問題,下面我們將實作透過Google的Authsub驗證方式在Rails網站上從Google Contact API匯入聯絡人列表的功能。


下圖是Twitter匯入GMail聯絡人功能的畫面:


首先需要到Google的註冊頁面註冊並驗證你的網域名稱,這裡的callback_url必須對映到應用程式裡的方法,然後我們還要上傳一個X.509 certificate檔案:

https://www.google.com/accounts/ManageDomains

Registration for Web-Based Applications
http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html

在*NIX和OS X平台上可以使用OpenSSL來產生RSA Keys和certificate(將www.example.com改成你的域名):

這個指令會產生rsakey.pem和rsacert.pem兩個檔案,將rsacert.pem上傳至Google的Manage Domains頁面,並將rsakey.pem放置到#{RAILS_ROOT}/config目錄下。

AuthSub in the Google Data Protocol Client Libraries
http://code.google.com/intl/zh-TW/apis/gdata/docs/auth/authsub.html

Authentication for Web Applications
http://code.google.com/intl/zh-TW/apis/accounts/docs/AuthForWebApps.html

之後當使用者按下"從GMail匯入聯絡人"連結後,使用者會被導向Google帳戶登入頁面(如果尚未登入),然後同意授權應用程式存取他們的GMail聯絡人資料,之後Google會傳回一個AuthSub Token給我們的應用程式。


授權應用程式存取Google聯絡人資料


下面是實作的Ruby程式碼:

首先我們在Google的Manage Domains頁面將callback_url設為http://www.example.com/authsub,因此我們可以在/config/routes.rb裡面添加路徑映射:

map.connect 'authsub', :controller => 'gmail_contacts', :action => 'authsub'

接下來新增一個GmailContacts Controller 內容如下:

然後在Application Controller添加下面內容:

接著在config/initializers目錄下建立一個google.rb 內容如下:

最後編輯/app/views/gmail_contacts/import.html.erb 內容如下:


現在我們已經可以成功從GMail匯入使用者的聯絡人列表,而且使用者並不需要在我們的網站上輸入任何GMail帳號密碼,所有的驗證過程都是在Google網站上以SSL安全連線來完成。接下來只要將這個部份整合到你網站上的邀請朋友功能,就能夠讓使用者選擇要發送邀請給哪些GMail上的聯絡人了!

參考資料:

Google Contacts API
http://code.google.com/intl/zh-TW/apis/contacts/

Contacts Gem at Github
http://github.com/cardmagic/contacts

ricardoalmeida / contacts_rails at Github
http://github.com/ricardoalmeida/contacts_rails

Google Data on Rails
http://code.google.com/intl/zh-TW/apis/gdata/articles/gdata_on_rails.html

Using Google OAuth in Ruby on Rails Sites
http://www.manu-j.com/blog/add-google-oauth-ruby-on-rails-sites/214/

Import Gmail Contacts using Ruby on Rails
http://blog.adsdevshop.com/2008/04/18/import-gmail-contacts-using-ruby-on-rails/

A Little Help on Importing Gmail Contacts using Ruby on Rails
http://www.theirishpenguin.com/2008/06/25/a-little-help-on-importing-gmail-contacts-using-ruby-on-rails/

Import contacts list in rails
http://blog.brijeshshah.com/import-contacts-list-in-rails/

blackbook gem (Contact Importer gem for Ruby on Rails)
http://saravani.wordpress.com/2009/03/13/blackbook-gem-contact-importer/

Fetching Cotacts From Gmail, Yahoo And Hotmail
http://puneetitengineer.wordpress.com/2008/09/26/fetching-cotacts-from-gmail-yahoo-and-hotmail/


Here is a (incomplete) list of pages with documentation for different open apis:

Hotmail: http://dev.live.com/contacts/
Gmail: http://googledataapis.blogspot.com/2008/03/3-2-1-contact-api-has-landed.html
Yahoo: http://developer.yahoo.com/social/contacts/ , http://developer.yahoo.com/addressbook/

熱門文章

Nov 2024【陽明山溫泉】老字號「馬槽花藝村」的溫泉管線已恢復|在木造山景湯屋享受半露天硫磺溫泉

Jan 2025【日本東北】溫泉三昧之旅② 在大雪飄落的雪國,追尋秘湯之宿與極上美食(仙台~品嚐極上米澤牛&奧羽的百年藥湯-鎌先溫泉)

Jan 2025【苗栗泰安】泰安警光山莊泡湯&彰化CP值極高的日本料理|沐藏料理所X海龍王|彰化板前料理 ♨️🍁🥢🍲

Jan 2025【台中北屯】大坑步道|頭嵙山健行|大坑5-1號步道上,大坑5號步道下(小百岳No.41)

Jan 2025【苗栗三義】富貴牡丹(三義館)人文藝術餐廳|預約制|在美術館裡吃無菜單料理

Jan 2025【日本東北】溫泉三昧之旅⑤ 在大雪飄落的雪國,追尋秘湯之宿與極上美食(福島~被時光遺忘下的江戶原風景,日本三大茅葺聚落大內宿)

Feb, 2024【台中西區】桃太郎日本料理|隱身巷弄裡的39年老字號無菜單料理|食材新鮮、自然美味

2019.10.9~13【令和元年の紅葉の山旅へ PART①】秋の贅沢、黒部川の源流へ北アルプス深部を縦走5日間!Day0、Day1(前泊、折立~藥師岳山莊)

Feb 2023【台中和平】裡冷林道Off Road單車野營~順登谷關七雄之首八仙山!單車+野營+登山,三個願望一次滿足

【2025.1.17 更新】有關 AI Agent、Gemini 2.0 與 Multimodal Live API 的一些隨想和實作

文章列表

Contact

名稱

以電子郵件傳送 *

訊息 *