My favorites | Sign in
Project Logo
                
Search
for
Updated Dec 08, 2008 by api....@google.com
Labels: Featured
GettingStarted  
Introduction to the Ruby Client Library

Getting started with the Ruby Client Library

Installation

The following gems and their dependencies are required by this client library:

From gem

Download the newest version of the gem on the downloads page. In a console, navigate to your downloads directory and enter:

gem install opensocial-x.x.x.gem

replacing x.x.x with the version number.

Now, in your code, you can include the code with:

require 'rubygems'
require 'opensocial'

From source

Once you have the required gems installed, check out the project code from SVN, and copy opensocial.rb and opensocial/ into your application's path. Then add:

require 'opensocial'

in your code.

List of restful endpoints you can use to test the library

List of RPC endpoints you can use to test the library

Sample code

To make the most of this example, install this gadget on your sandbox.orkut.com profile, and use the ID it presents to you in place of the example 'requestor' below.

Enter the following in an IRB session, initiated from the root of your app (which contains the files as outlined above):

require 'opensocial'

# Loads credentials for the example gadget above.
consumer_key = 'orkut.com:623061448914'
consumer_secret = 'uynAeXiWTisflWX99KU1D2q5'
requestor = '03067092798963641994'

# Initializes a new connection orkut.
c = OpenSocial::Connection.new(:consumer_key => consumer_key, :consumer_secret => consumer_secret, :xoauth_requestor_id => requestor)

# Creates a request for profile data for 'requestor'.
r = OpenSocial::FetchPersonRequest.new(c)

# Loads the data, and displays it on the console, using REST with HMAC-SHA1 signing.
puts r.send.inspect

# Creates an RPC request for information on 'requestor's friends.
r = OpenSocial::RpcRequest.new(c)
r.add(:friends => OpenSocial::FetchPeopleRequest.new)

# Loads the data, and displays it on the console using RPC with HMAC-SHA1 signing.
puts r.send.inspect

# Creates an RPC request for 'requestor's appdata.
r = OpenSocial::RpcRequest.new(c)
r.add(:data => OpenSocial::FetchAppDataRequest.new)

# Loads the data, and displays it on the console using RPC with HMAC-SHA1 signing.
puts r.send.inspect

MySpace sample

The following uses the same gadget XML as above, on MySpace, with its REST endpoint.

require 'opensocial'

# Loads MySpace credentials for the example gadget.
consumer_key = 'http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml'
consumer_secret = '6a838d107daf4d09b7d446422f5e7a81'
requestor = '425505213'

# Initializes a new connection MySpace.
c = OpenSocial::Connection.new(:container => OpenSocial::Connection::MYSPACE, :consumer_key => consumer_key, :consumer_secret => consumer_secret, :xoauth_requestor_id => requestor)

# Creates a request for profile data for 'requestor'.
r = OpenSocial::FetchPersonRequest.new(c, requestor)

# Loads the data, and displays it on the console, using REST with HMAC-SHA1 signing.
puts r.send.inspect

Comment by zabouti, Jan 01, 2009

I don't understand where I'm supposed to copy opensocial.rb and opensocial/ from? Thanks!

-- ge

Comment by rvalyi, Jan 01, 2009

Just tried using JRuby (trunk), install is a bit tricky because the json lib is actually replaced by the json-jruby gem on the Java platform, so I had to hack a gemspec by hand to get the opensocial gem installed (I a rubygem noob, so I did it the hugly way, nothing clever too share so far), but then I was able to run all the examples above (at least on my Orkut sandbox account, no Myspac account). Sounds really exciting guys, thank you for that lib.

Raphaƫl Valyi.

Comment by RobertREvans, Jan 03, 2009

I've added a mirror on github: http://github.com/revans/opensocial/tree/master

Comment by ramesh.meet, Feb 12, 2009

i am new to opensocial, i followed gifts sample, in gifts_contoller replaced my consumer key and secret key. while verify i always getting 'lack of authenticatiion'

Validation method.

def check_signature
key = params:oauth_consumer_key? unless validate(key, KEYSkey?:secret?)
render :text => '401 Unauthorized', :status => :unauthorized
end
end

validate method always return 'false'

any body knows whats the issue..

Ramesh

Comment by brucewan...@gmail.com, May 27, 2009

I think this can be a configuration problem but,

I registered gifts.xml on my Orkut sandbox, and received the keys. And I ran your sample code here, but this line generates an error.

r = OpenSocial??::FetchPersonRequest??.new(c)

When I run this, it says like this.

Net::HTTPFatalError: 500 "hasUserId failed"

from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2097:in `error!' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2106:in `value' from /Library/Ruby/Gems/1.8/gems/opensocial-0.0.4/lib/opensocial/request.rb:135:in `check_for_http_error!' from /Library/Ruby/Gems/1.8/gems/opensocial-0.0.4/lib/opensocial/request.rb:115:in `dispatch' from /Library/Ruby/Gems/1.8/gems/opensocial-0.0.4/lib/opensocial/request.rb:69:in `send_request' from /Library/Ruby/Gems/1.8/gems/opensocial-0.0.4/lib/opensocial/person.rb:104:in `send' from (irb):52

Best Regards.,

Bruce Wang.

Comment by brucewan...@gmail.com, May 29, 2009

Hmmm. I figured out that I need to use the 'ID of the user who installed my application" for 'requestor' variable. This ID is not Orkut ID which can be found on the User's home URL address, but the OpenSocial? ID.

You can get the real OpenSocial? ID with JS API, if you are working on the client side, and if your Application Container Server need to know that, you have to just wait till the user (who installed your application) connect to your server with this plugin installed. If that request is forwarded from the Orkut to your Server, you will get whole information of that requester (the user) including his/her OpenSocial? ID.

After that, is ~ just fun.

Bruce.

Comment by brucewan...@gmail.com, Jun 03, 2009

I think in new opensocial-0.0.4.gem, that sample doesn' work properly. I am getting 400 Error response from Orkut when I send the 'friend list' request.

Instead, this way is working.

# Profile data...
r = OpenSocial::FetchPersonRequest.new(c)
result = r.send.inspect

# Friend List
r = OpenSocial::FetchPeopleRequest.new(c)
result = r.send.inspect
Comment by dc.pikkle, Jun 03, 2009

so it seems latest plugin - 0.0.4 does NOT work with rails 2.3 can anyone confirm this?

there's a 0.0.2 branch in the repo that does, however.

strange.

Comment by brucewan...@gmail.com, Jun 04, 2009

Dear,

I tried to implement the 'sendMessage' function by myself, by generating the raw HTTP request message.

But it doesn't go well.. I tried that with following code (tested in Rails console) so could anyone kindly consider together and suggest any???

	##########################################################################
	# Here comes the Test Code...
	##########################################################################
	require 'opensocial'
	consumer_key = 'orkut.com:1111111111111111' # of course, your key
	consumer_secret = 'ffffffffffffffffff' # of course, your secret
	requestor = '222222222222' # of course, use proper OpenSocial ID number, not Orkut ID

	#///////////////////////////////////////
	# Create the connection
	#///////////////////////////////////////
	conection = OpenSocial::Connection.new(:consumer_key => consumer_key, :consumer_secret => consumer_secret, :xoauth_requestor_id => requestor)
	#///////////////////////////////////////
	# Get the Person Info, with Raw http message...
	# Refer to following URL (OpenSocial-Spec-v081 site) for the specification of the request message
	# http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/rpc-protocol
	# in Section (2.1 Single element request & response for the Person associated with principal in the OAuth token
	)
	#///////////////////////////////////////
	http = Net::HTTP.new('sandbox.orkut.com', 80)
	req = Net::HTTP::Get.new('/social/rest/people/@me/@self?xoauth_requestor_id=11111111111111&fields=id,dateOfBirth,name,emails,gender,state,postalCode,ethnicity,relationshipStatus,thumbnailUrl,displayName')
	conection.sign!(http, req)
	resp = http.get(req.path)
	#
	# The above code will generate exactly the same HTTP request message as
	# with following code.
	#
	# > req = OpenSocial::FetchPersonRequest.new(c)
	# > result = req.send.inspect
	#
	#
	# Or we can do the same thing using RPC calls.
	#
	#
	# Using RPC Call
	http = Net::HTTP.new('sandbox.orkut.com', 80)
	req = Net::HTTP::Get.new('/social/rpc?method=people.get&userId=@me&groupId=@self&xoauth_requestor_id=1111111111111111111')
	conection.sign!(http, req)
	resp = http.get(req.path)


	#///////////////////////////////////////
	# Now, I did following to Send a mail to my friend,
	# with the specification (mentioned in Section '8.4 Messaging' on OpenSocial-Spec-v081 site)
	# in my mind.
	#///////////////////////////////////////
	http = Net::HTTP.new('sandbox.orkut.com', 80)
	req = Net::HTTP::Get.new( '/social/rpc?method=messages.send&userId=@me&recipients=orkut.com:11111111111&title=test&body=tesstbody&type=EMAIL&xoauth_requestor_id=2222222222222222222')
	connection.sign!(http, req)
	resp = http.get(req.path)
	#
	# Then the Orkut says like this..
	#
	# > HTTP/1.1 200 OK..Date: Fri, 05 Jun 2009 04:18:19 GMT..Expires: Fri, 05 Jun 2009 04:18:19 GMT..Cache-Control: private, max
	# > -age=0..X-Content-Type-Options: nosniff..Content-Length: 94..Connection: close..Server: GFE/2.0..Content-Type: text/html.
	# > ...{"error":{"message":"notImplemented: The method messages.send is not implemented","code":501}}
	#
	# which seemed some what strange. Because they (orkut) say they have supported this 'sendMessage' function
	# from the year 2008.. Am I wrong?
	#
	# According to another document I found,
	# <http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/restful-protocol>
	# this sendMessage should use HTTP PUT.
	#
	# So I tried to send the HTTP Put or Post request, but some tries all failed because of
	# my bad skill.
	#

	#///////////////////////////////////////
	# So I suspected my mistake, and decided to refer to the original implementation of
	# 'FetchAppDataRequest' class, which is also using RPC call.
	#
	# Now I made this class.. somewhere in the opensocial library source code.
	# Nothing but generate 'mail message' form data.
	#///////////////////////////////////////
	class InviteRequest < Request
		SERVICE = 'messages'

		def initialize(connection = nil)
			super
		end

		def send(unescape = true)
			json = send_request(SERVICE, @guid, @selector, @pid, unescape)

			return parse_response(json['entry'])
		end

		def parse_rpc_response(response)
			return parse_response(response['data'])
		end

		def to_json(*a)
			value = {
			  'method' => SERVICE + 'send',
			  'params' => {
			    'recipients' => 'orkut.com:15724004553698970652', 
			    'title' => 'test title', 
			    'body' => 'tewfasdfasdf', 
			    'type' => 'MAIL'
			  },
			  'id' => @key
			}.to_json(*a)
		end

		private

		def parse_response(response)
			appdata = Collection.new
			response.each do |key, value|
			  data = AppData.new(key, value)
			  appdata[key] = data
			end
			return appdata
		end
	end
	#
	# And tested with the new class..
	# (restarted the rails console..)
	#
	require 'opensocial'
	consumer_key = 'orkut.com:1111111111111111' # of course, your key
	consumer_secret = 'ffffffffffffffffff' # of course, your secret
	requestor = '222222222222' # of course, use proper OpenSocial ID number, not Orkut ID
	conection = OpenSocial::Connection.new(:consumer_key => consumer_key, :consumer_secret => consumer_secret, :xoauth_requestor_id => requestor)
	req = OpenSocial::RpcRequest.new(conection)
	req.add(:data => OpenSocial::InviteRequest.new)
	puts req.send.inspect
	#
	# And then the Orkut says the following answer..
	# > HTTP/1.1 200 OK
	# > message":"badRequest: Invalid batch - Cannot use disallowed Content-Type application/x-www-form-urlencoded"

So, I wonder whether Orkut really doesn't support 'sendMessage', or there is something wrong in my request???

Best regards.,,

Bruce.

Comment by LeonidShevtsov, Jul 03, 2009
so it seems latest plugin - 0.0.4 does NOT work with rails 2.3 can anyone confirm this?

just find ActionController?::AbstractRequest? in the source code and replace it with ActionController?::Request now opensocial-0.0.4 works with rails 2.3.

Comment by brendan.ribera, Jul 09, 2009

To work in both scenarios:

proxies(defined?(ActionController?::AbstractRequest?) ? ActionController?::AbstractRequest? : ActionController?::Request)

Comment by jcolag, Aug 13, 2009

I must be missing something, since everybody else is getting further than I am. After installing the gem (0.0.4) on an Ubuntu Linux server, though that shouldn't make a difference, with Ruby 1.8.6 and Rails 2.3.3, the MySpace? example gives me this:

./opensocial.rb:10: uninitialized constant OpenSocial? (NameError?)

from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from opensocial.rb:2

Yes, I added "require 'rubygems'," figuring that might resolve it, but no go.

It sounds like it didn't install correctly, but I found everything to make Brendan's suggested patch.

Might anybody have any thoughts on this?

Comment by toyosystem, Yesterday (21 hours ago)

Sign in to add a comment
Hosted by Google Code