Finally You can able to post Your data into your google docs.
class Myspreadsheet
require 'net/http'
require 'net/https'
require 'rubygems'
require 'xmlsimple'
def posts #(uri, data, headers)
http = Net::HTTP.new('www.google.com', 443)
http.use_ssl = true
path = '/accounts/ClientLogin'
data = 'accountType=HOSTED_OR_GOOGLE&Email=username@gmail.com&Passwd=password&service=wise'
headers ={ 'Content-Type' => 'application/x-www-form-urlencoded'}
resp, data = http.post(path, data, headers)
cl_string = data[/Auth=(.*)/, 1]
headers["Authorization"] = "GoogleLogin auth=#{cl_string}"
spreadsheets_uri = 'http://spreadsheets.google.com/feeds/spreadsheets/private/full'
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
my_spreadsheets = get_feed(spreadsheets_uri, headers)
puts my_spreadsheets.body
doc = XmlSimple.xml_in(my_spreadsheets.body, 'KeyAttr' => 'name')
require 'pp'
puts pp doc
spreadsheet_key = doc["entry"][0]["id"][0][/full\/(.*)/, 1]
worksheet_feed_uri = "http://spreadsheets.google.com/feeds/worksheets/#{spreadsheet_key}/private/full"
worksheet_response = get_feed(worksheet_feed_uri, headers)
worksheet_data = XmlSimple.xml_in(worksheet_response.body, 'KeyAttr' => 'name')
puts pp worksheet_data
listfeed_uri = worksheet_data["entry"][0]["link"][0]["href"]
response = get_feed(listfeed_uri, headers)
listfeed_doc = XmlSimple.xml_in(response.body, 'KeyAttr' => 'name')
puts pp listfeed_doc
cellfeed_uri = worksheet_data["entry"][0]["link"][1]["href"]
response = get_feed(cellfeed_uri, headers)
# Parse into datastructure and print
cellfeed_doc = XmlSimple.xml_in(response.body, 'KeyAttr' => 'name')
puts pp cellfeed_doc
# Set up our POST url
#"http://spreadsheets.google.com/feeds/list/tcIStC1pH2D2ohWewXnPIbQ/od6/private/full"
end
def encode_query(params)
puts params.map(){ |k, v| uri_encode(k) + "=" + uri_encode(v) }.join("&")
return params.map(){ |k, v| uri_encode(k) + "=" + uri_encode(v) }.join("&")
end
def uri_encode(str)
return URI.encode(str, /#{URI::UNSAFE}|&/n)
end
def h(str)
return CGI.escapeHTML(str.to_s())
end
def http_request(method, url, data, header = {})
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start() do
path = uri.path + (uri.query ? "?#{uri.query}" : "")
if method == :delete
response = http.__send__(method, path, header)
else
response = http.__send__(method, path, data, header)
end
if !(response.code =~ /^2/)
raise(GoogleSpreadsheet::Error, "Response code #{response.code} for POST #{url}: " +
CGI.unescapeHTML(response.body))
end
return response.body
end
end
def post(uri, data)
@auth_token = nil
headers = Hash.new
headers["Content-Type"] = "application/atom+xml"
params = {
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => 'username',
"Passwd" => 'password',
"service" => "wise",
}
response = http_request(:post,
"https://www.google.com/accounts/ClientLogin", encode_query(params))
@auth_token = response.slice(/^Auth=(.*)$/, 1)
#headers = { 'Content-Type' => 'application/atom+xml'}
headers["Authorization"] = "GoogleLogin auth=#{@auth_token}"
uri = URI.parse(uri)
http = Net::HTTP.new(uri.host, uri.port)
puts http.post(uri.path, data, headers)
return http.post(uri.path, data, headers)
end
p = Myspreadsheet.new
p.posts
post_url = "http://spreadsheets.google.com/feeds/list/tcIStC1pH2D2ohWewXnPIbQ/od6/private/full"
new_row = '
p.post(post_url, new_row)
end
No comments:
Post a Comment