Get Gmail contacts in Ruby on rails

>> 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"=>"Arvind kushwah",
   "email"=>"arvindkushwah9@gmail.com",
   "first_name"=>"arvind",
   "last_name"=>"kushwah",
   "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"=>"arvind Kushwah",
     "given_name"=>"arvind",
     "family_name"=>"kushwah",
     "profile"=>"https://plus.google.com/100376057291650719821",
     "picture"=>
      "https://lh6.googleusercontent.com/-g8vajlG-tqY/AAAAAAAAAAI/AAAAAAAAAg0/ZrBTnqNzqm4/photo.jpg?sz=50",
     "email"=>"arvindkushwah9@gmail.com",
     "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"=>"ashish@ashishprajapati.com", "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"=>"prajapatiu60@gmail.com", "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"=>"himanshubdjt@gmail.com", "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=>"arvind kushwah", :email=>"arvindkushwah9@gmail.com"},
 {:name=>"Amitesh Jain", :email=>"09.amitesh@gmail.com"},
 {:name=>"Ashish Prajapati", :email=>"mail@ashishprajapati.com"},
 {:name=>"ankitadangi11@gmail.com", :email=>"ankitadangi11@gmail.com"},
 {:name=>"Ravikant jain", :email=>"ravikant.jain331@gmail.com"},
]


By javascript:

<!-- <script type="text/javascript">
$.ajax({
            url: 'https://www.google.com/m8/feeds/contacts/default/full?alt=json',
            dataType: "jsonp",
            data: {'access_token': 'ya29.Glv4A1aQy4F2Y9nkpSORJJ1uei9lkijhUVWo3210AEtElnqO6ikMXURdEGrsIuy75dcd0AobsC3ZN6KkcnGv_MlsEOQYWR_cP4nA_ipox8dnm_AfIyWSVmYRbvGS'},
            success:function(data) {
                var entryOfEmails = data.feed.entry;
                for (var i = 0; i < data.feed.entry.length; i++) {
                        console.log(entryOfEmails[i].gd$email[0].address);
                    }
            }
         });  
</script> -->

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails