AppData Update working with Spacer (hacked in)

0 views
Skip to first unread message

scott persinger

unread,
Jul 22, 2008, 2:09:28 PM7/22/08
to Spacer: The MySpace Platform Ruby Client
Ok, I finally got this baby working. The following code is not a
proper patch to Spacer, but is rather a function I hacked up that
performs the 'PUT' operation successfully.

Some notes:
-Added to spacer's client.rb file, but most of the code is
independent
-I happen to be using HMAC::SHA1 from the open-id library. You'll
need something similar.
-I've left all the debug cruft in there if anyone wants to trace
what's happening

------- spacer/lib/spacer/client.rb -------

def appdata_put(user_id, params)
response = do_put("users/#{user_id}/appdata", params)
end

def do_put(resource, params, oa_consumer_key = MYSPACE_API_KEY,
oa_consumer_secret = MYSPACE_API_SECRET)
request_uri = request_uri(resource)

# Get the Oauth parameters. We're going to pass them in the
header
oa_values = {'oauth_consumer_key' =>
CGI.escape(oa_consumer_key), 'oauth_nonce' => rand(2**128).to_s,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => Time.new.to_i.to_s,
'oauth_token' => ''}
param_string = params.merge(oa_values).to_a.sort.collect {|t|
"#{t[0]}=#{t[1]}"}.join("&")
@log.debug("Body string:" + param_string)
request_url = CGI.escape(resource)

base_string = "PUT&" + CGI.escape(request_uri.to_s) + '&' +
CGI.escape(param_string)
@log.debug("Base string:" + base_string)
hmac = HMAC::SHA1.new(oa_consumer_secret + "&")
hmac.update(base_string)
oa_sig = Base64.encode64(hmac.digest).chomp
@log.debug("OA SIG: " + oa_sig)

http = Net::HTTP.new(request_uri.host, request_uri.port)
#http.set_debug_output $stderr
request = Net::HTTP::Put.new(request_uri.path)
request.set_form_data( params )
request['Authorization'] = "OAuth realm=\"\", " +
oa_values.update({'oauth_signature' => oa_sig}).collect{|t| "#{t[0]}=
\"#{t[1]}\""}.join(", ")
@log.debug "REQUEST PATH: #{request.path}"
response = http.request(request)
@log.debug "RESPONSE BODY: #{response.body}\n"

return response.code
end

def request_uri(query)
URI.parse("http://#{SERVER}/#{VERSION}/#{query}.#{FORMAT.to_s}")
end

This code was adapter from the php example someone had posted to the
Myspace forum.

Enjoy!

--scott
Reply all
Reply to author
Forward
0 new messages