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 = 'user@gmail.com'
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

8 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. If you are attempting to use the analytics service, you will need to use SSL. Modify the line that says

    Net::HTTP.start(uri.host, uri.port) do |http|

    to this:

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.start do |http|

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I lost authencation token after this step.
    # resp, data = http.post(path, data, headers)
    Now when I started from the beginning, terminal is giving error at above step can you tell how to this.

    thank you

    ReplyDelete
  5. Among other courses, nursing healthcare coursework writing services have become popular since students seek Healthcare Research Writing Services and healthcare essay writing services.

    ReplyDelete