Created
September 15, 2014 19:13
-
-
Save johnknott/28a9565d256c446298d9 to your computer and use it in GitHub Desktop.
Creating a Google Contact with Ruby
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
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='[email protected]'/> | |
</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