Fetch Google Contacts using access token ( omniauth-google-oauth2 )



For Ruby on Rails Developers :

>> If you are looking for the functionailty for fetching the google contacts than you can follow the below mentioned instructions.

>> Firstly You need to define the google contact and contact groups url in the scope for fetching the google contacts. It will allow our retrieve access token to use google contact api.

>> We can define multiple scopes with the google keys :

   config.omniauth :google_oauth2, client_id, client_secret, {
      name: ‘google’,
      scope: ’email https://www.google.com/m8/feeds/’
  }

>> In the scope I have defined email for  (user’s email access access) and https://www.google.com/m8/feeds/ for (read/write access to Contacts and Contact Groups)

>> I am assuming you have added the omniauth-google-oauth2 gem and added the functionality of auth login.

>> After successful auth login when you will return back to the website using callback method with some josn. You can see that using request.env[“omniauth.auth”].

Example : 

 {“provider”=>”google”,
 “uid”=>”100376057291650719821”,
 “info”=>
  {“name”=>”ASHISH PRAJAPATI”,
   “email”=>”[email protected]”,
   “first_name”=>”ASHISH”,
   “last_name”=>”PRAJAPATI”,
   “image”=>
    “https://lh6.googleusercontent.com/-g8vajlG-tqY/AAAAAAAAAAI/AAAAAAAAAg0/ZrBTnqNzqm4/photo.jpg?sz=50”,
   “urls”=>{“Google”=>”https://plus.google.com/100376057291650719821”}},
 “credentials”=>
  {“token”=>
    “ya29.OQG4DLpFUdJnGTGL8jeMkrlHyZQYhWCtLZT1dLsC9kNqJkJ5tiOTJ3JyxKofxtbJamo3vdQGG1YE_w”,
   “refresh_token”=>
    “1/ITx5zveLtBUzdSvxhp8W2tFku5tROwngh2_xS0hVI_kMEudVrK5jSpoR30zcRFq6”,
   “expires_at”=>1426625308,
   “expires”=>true},
 “extra”=>
  {“id_token”=>
    “eyJhbGciOiJSUzI1NiIsImtpZCI6IjRhZmM4MGZjMzRlMzMyMDE1ZmI5MjZlMDg2NjdhZWM0ZDZhMGI3NTgifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTAwMzc2MDU3MjkxNjUwNzE5ODIxIiwiYXpwIjoiMzAwNDk5MDU1NDMtZDlkZDc5MHA0OTZkZHNscm92NnAzbGQ5MXZhMzkxa2YuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJlbWFpbCI6ImFzaGlzaHByYWphcGF0aTA0MDRAZ21haWwuY29tIiwiYXRfaGFzaCI6InV4MDctOV9HODI1X1RGUENhckh0NWciLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXVkIjoiMzAwNDk5MDU1NDMtZDlkZDc5MHA0OTZkZHNscm92NnAzbGQ5MXZhMzkxa2YuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJpYXQiOjE0MjY2MjE1MjksImV4cCI6MTQyNjYyNTMwOX0.CljbjcU9lGNAzDpovf-7pc5iLWZdGf-nPW2j9eoWMWjVoZKvbsUSTMYnk3d4znyfsGAMnDinzizr5uKW-bckZavOe2zhNjsb3Rnwu0uZS_TFynZOeOFdzuvV5oCFz8i8c6tAnDNDq0QyIwpR7B4vz3-o-eVpClXOhXXmoJ1j07w”,
   “raw_info”=>
    {“kind”=>”plus#personOpenIdConnect”,
     “gender”=>”male”,
     “sub”=>”100376057291650719821”,
     “name”=>”ASHISH PRAJAPATI”,
     “given_name”=>”ASHISH”,
     “family_name”=>”PRAJAPATI”,
     “profile”=>”https://plus.google.com/100376057291650719821”,
     “picture”=>
      “https://lh6.googleusercontent.com/-g8vajlG-tqY/AAAAAAAAAAI/AAAAAAAAAg0/ZrBTnqNzqm4/photo.jpg?sz=50”,
     “email”=>”[email protected]”,
     “email_verified”=>”true”}}}

>> Now you need to use access token for accessing the contacts of this user. You can find the token inside the json that is retrived by request.env[“omniauth.auth”]

#So you can find token like this
token = request.env[“omniauth.auth”][“credentials”][“token”]
#we are using it for using the methods of open uri.
require ‘open-uri’
## We are fetching the contacts using acccess token for by below mentioned contact api.
contacts_josn = JSON.parse(open(“https://www.google.com/m8/feeds/contacts/default/full?access_token=”+token+”&alt=json”).read)

#After this successfully running this command you will get the contacts json with bulk of data
#I am showing you the example json of feeds that will be inside contacts api json object.

contacts = contacts_josn[“feed”][“entry”]

=> [{“id”=>{“$t”=>”http://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/base/27f2fff09693f8c”},
  “updated”=>{“$t”=>”2012-09-29T06:35:40.785Z”},
  “category”=>[{“scheme”=>”http://schemas.google.com/g/2005#kind”, “term”=>”http://schemas.google.com/contact/2008#contact”}],
  “title”=>{“type”=>”text”, “$t”=>””},
  “link”=>
   [{“rel”=>”http://schemas.google.com/contacts/2008/rel#edit-photo”,
     “type”=>”image/*”,
     “href”=>”https://www.google.com/m8/feeds/photos/media/ashishprajapati0404%40gmail.com/27f2fff09693f8c/1B2M2Y8AsgTpgAmY7PhCfg”},
    {“rel”=>”self”,
     “type”=>”application/atom+xml”,
     “href”=>”https://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/full/27f2fff09693f8c”},
    {“rel”=>”edit”,
     “type”=>”application/atom+xml”,
     “href”=>”https://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/full/27f2fff09693f8c/1348900540785001”}],
  “gd$email”=>[{“address”=>”[email protected]”, “primary”=>”true”, “rel”=>”http://schemas.google.com/g/2005#other”}]},
 {“id”=>{“$t”=>”http://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/base/2eb85048f8c4567”},
  “updated”=>{“$t”=>”2014-07-28T17:14:55.125Z”},
  “category”=>[{“scheme”=>”http://schemas.google.com/g/2005#kind”, “term”=>”http://schemas.google.com/contact/2008#contact”}],
  “title”=>{“type”=>”text”, “$t”=>””},
  “link”=>
   [{“rel”=>”http://schemas.google.com/contacts/2008/rel#edit-photo”,
     “type”=>”image/*”,
     “href”=>”https://www.google.com/m8/feeds/photos/media/ashishprajapati0404%40gmail.com/2eb85048f8c4567/1B2M2Y8AsgTpgAmY7PhCfg”},
    {“rel”=>”self”,
     “type”=>”application/atom+xml”,
     “href”=>”https://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/full/2eb85048f8c4567”},
    {“rel”=>”edit”,
     “type”=>”application/atom+xml”,
     “href”=>”https://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/full/2eb85048f8c4567/1406567695125001”}],
  “gd$email”=>[{“address”=>”[email protected]”, “primary”=>”true”, “rel”=>”http://schemas.google.com/g/2005#other”}]},
 {“id”=>{“$t”=>”http://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/base/efbd2748d01a6fa”},
  “updated”=>{“$t”=>”2013-04-11T17:27:18.839Z”},
  “category”=>[{“scheme”=>”http://schemas.google.com/g/2005#kind”, “term”=>”http://schemas.google.com/contact/2008#contact”}],
  “title”=>{“type”=>”text”, “$t”=>””},
  “link”=>
   [{“rel”=>”http://schemas.google.com/contacts/2008/rel#edit-photo”,
     “type”=>”image/*”,
     “href”=>”https://www.google.com/m8/feeds/photos/media/ashishprajapati0404%40gmail.com/efbd2748d01a6fa/99kFmNNkF7RISFgiG6kCpA”},
    {“rel”=>”http://schemas.google.com/contacts/2008/rel#photo”,
     “type”=>”image/*”,
     “href”=>”https://www.google.com/m8/feeds/photos/media/ashishprajapati0404%40gmail.com/efbd2748d01a6fa”},
    {“rel”=>”self”,
     “type”=>”application/atom+xml”,
     “href”=>”https://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/full/efbd2748d01a6fa”},
    {“rel”=>”edit”,
     “type”=>”application/atom+xml”,
     “href”=>”https://www.google.com/m8/feeds/contacts/ashishprajapati0404%40gmail.com/full/efbd2748d01a6fa/1365701238839001”}],
  “gd$email”=>[{“address”=>”[email protected]”, “primary”=>”true”, “rel”=>”http://schemas.google.com/g/2005#other”}]},

>> Is it looking very complicated to fetch data. No problem, I am doing that for you.

You can use this for looking directly the name and email of all contacts.

 contacts = contacts_josn[“feed”][“entry”].collect{|p| {name: p[“title”][“$t”], email: p[“gd$email”][0][“address”]} }
 [{:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>”arvind kushwah”, :email=>”[email protected]”},
 {:name=>”Amitesh Jain”, :email=>”[email protected]”},
 {:name=>”Ashish Prajapati”, :email=>”[email protected]”},
 {:name=>”[email protected]”, :email=>”[email protected]”},
 {:name=>”Ravikant jain”, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>”ashish prajapati”, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>”Deep Bajaj”, :email=>”bajaj[email protected]”},
 {:name=>”ashish”, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>”ASHISH PRAJAPATI”, :email=>”[email protected]”},
 {:name=>”Aman Jain”, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”},
 {:name=>””, :email=>”[email protected]”}]

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Leave a Reply

Your email address will not be published. Required fields are marked *