Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Accessing a feed using ClientLogin & Ruby


Example of accessing the DocList API using ClientLogin in Ruby:
require 'net/http'
require 'net/https'
require 'cgi'
require 'rubygems'
require 'xmlsimple'
require 'pp'

def get_feed(uri, headers=nil)
  uri = URI.parse(uri)
  Net::HTTP.start(uri.host, uri.port) do |http|
    return http.get(uri.path, headers)
  end
end

email = '[email protected]'
password = CGI::escape('pa$$word')
service = 'writely'
source = 'MyCompany-MyApp-0.1'
path = '/accounts/ClientLogin'

data = ["accountType=HOSTED_OR_GOOGLE", 
        "Email=#{email}",
        "Passwd=#{password}",
        "service=#{service}",
        "source=#{source}"].join('&')

http = Net::HTTP.new(host='www.google.com', port=443)
http.use_ssl = true
http.start

headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
resp, data = http.post(path, data, headers)

token = data[/Auth=(.*)/, 1]  # parse out the Auth token

headers['Authorization'] = "GoogleLogin auth=#{token}"
resp = get_feed('http://docs.google.com/feeds/documents/private/full', headers)

doc = XmlSimple.xml_in(resp.body, 'KeyAttr' => 'name')
pp doc

Signed AuthSub in Ruby


Example code for signing an AuthSub request in Ruby. Contributed by an awesome guy named Immad. Watch out for the groups linebreak in his line that starts with header =, though.