Skip to content

Instantly share code, notes, and snippets.

@johnknott
Created September 15, 2014 19:13
Show Gist options
  • Save johnknott/28a9565d256c446298d9 to your computer and use it in GitHub Desktop.
Save johnknott/28a9565d256c446298d9 to your computer and use it in GitHub Desktop.
Creating a Google Contact with Ruby
require 'rubygems'
require 'bundler/setup'
Bundler.require
refresh_token = 'REDACTED'
CLIENT_ID = 'REDACTED'
CLIENT_SECRET = 'REDACTED'
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, {
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
:token_url => 'https://accounts.google.com/o/oauth2/token'
})
access_token = OAuth2::AccessToken.from_hash(client, {:refresh_token => refresh_token})
access_token = access_token.refresh!
body = <<EOF
<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'
xmlns:gd='http://schemas.google.com/g/2005'>
<atom:category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/contact/2008#contact'/>
<gd:email rel='http://schemas.google.com/g/2005#work'
primary='true'
address='monkey.head@gmail.com'/>
</atom:entry>
EOF
# Couldn't get this working easily with accesstoken.post/faraday
result = RestClient.post("https://www.google.com/m8/feeds/contacts/default/full?access_token=#{access_token.token}", body, content_type: 'application/atom+xml')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment