Last active
December 10, 2015 04:08
-
-
Save sunny0425/4379165 to your computer and use it in GitHub Desktop.
use oauth 2.0 to get google contact list (use httparty gem)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# access_token = "abcdefg123456hijklmn" | |
# curl -H "Authorization: OAuth abcdefg123456hijklmn" "https://www.google.com/m8/feeds/contacts/default/full?alt=json&v=3.0" | |
# https://developers.google.com/google-apps/contacts/v3/ | |
# https://developers.google.com/google-apps/contacts/v3/reference | |
# OAuth 2.0 Playground: https://developers.google.com/oauthplayground/ | |
# Get a contact photo: http://stackoverflow.com/questions/5983484/google-api-getting-a-contacts-photo | |
def contact_list | |
url = "https://www.google.com/m8/feeds/contacts/default/full" | |
response = HTTParty.get(url, | |
:query => {:alt => "json", :"max-results" => "100000000"}, | |
:headers => {"Authorization" => "OAuth #{access_token}", "GData-Version" => "3.0"} | |
) | |
return {:error => "#{response.code}: authorization error"} if response.code == 401 | |
entries = response["feed"]["entry"] | |
list = entries.collect do |e| | |
next if e["gd$name"].blank? || e["gd$email"].blank? | |
{ | |
:name => e["gd$name"]["gd$fullName"]["$t"], | |
:email => e["gd$email"].first["address"], | |
:photo => e["link"].select{ |l| l["rel"] == "http://schemas.google.com/contacts/2008/rel#photo" }.first["href"] | |
} | |
end | |
return list.compact! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment