Thursday, January 08, 2009

OpenSocial REST API Is Here

I just noticed the Google-led OpenSocial REST/RPC APIs are finally available. These allow you to build Java, PHP, Python, and Ruby clients that can access and manipulate your social network data.

Blog announcement: http://opensocialapis.blogspot.com/

Java code: http://code.google.com/p/opensocial-java-client/

Orkut Sandbox information: http://code.google.com/apis/orkut/docs/rest/developers_guide_protocol.html

Client Examples
As you might expect, you can use Orkut's sandbox as your "backend" service. The Java code and examples are pretty simple but work. As a bonus, here is a recursive method that you can use in the DisplayProfileData.java example to display all information about a particular person. It will also work in DisplayFriends.java. You'll need to add imports for OpenSocialObject and OpenSocialField.

protected void processFields(OpenSocialObject osObj) throws Exception {
//See all fields
String[] fields=osObj.fieldNames();
for(String fieldName : fields) {
OpenSocialField afield=osObj.getField(fieldName);
if(!afield.isComplex()) {
System.out.println(fieldName+" "+afield.getStringValue());
}
else {
System.out.println(fieldName+" is complex:" );
Collection complexValues=afield.getValues();
for(OpenSocialObject compVal : complexValues) {
processFields(compVal);
}
}
}

Call it anywhere after creating the sample person, like so:

OpenSocialPerson person = c.fetchPerson("03067092798963641994");
...
processFields(person);

You may also want to change the Person ID in the above line. You get this ID when you add sample.xml social gadget to your sandbox. Mine, for example, is 08354253340777199997. This ID does not need to match the viewer ID discussed below.

Here is the sample output:
156-56-104-143:java mpierce$ java -classpath samples/bin:$CP DisplayProfileData
----------
Info. for API DWH
ID: 03067092798963641994
----------
Printing fields
----------
photos is complex:
value http://www.orkut.com/img/i_nophoto64.gif
type thumbnail
thumbnailUrl http://www.orkut.com/img/i_nophoto64.gif
name is complex:
givenName API
familyName DWH
id 03067092798963641994
isOwner false
isViewer true
------Done---------
Thumbnail URL: http://www.orkut.com/img/i_nophoto64.gif
----------

Modifying the Backend
Here's were the problems start. The sample clients above include a default view ID, consumer secret, and consumer key as part of the OAuth infrastructure:

c.setProperty(OpenSocialClient.Properties.CONSUMER_SECRET,
"uynAeXiWTisflWX99KU1D2q5");
c.setProperty(OpenSocialClient.Properties.CONSUMER_KEY,
"orkut.com:623061448914");
c.setProperty(OpenSocialClient.Properties.VIEWER_ID,
"03067092798963641994");

Of course you will want to replace these with your own values. To do this, you will need to deploy a gadget and verify that you own it. The steps are simple:
  1. Write a gadget. You can use http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml for example. I'm not sure if the gadget in anyway affects the REST/RPC server side calls that you can use.
  2. Get an Orkut sandbox account and log into sandbox.orkut.com.
  3. Install your application into your Orkut account.
  4. Verify that you own the gadget: https://www.google.com/gadgets/directory/verify
This last step is the problem. It involves two steps. First you provide the URL for your gadget and submit. You will receive a long signature string, which you need to add to your gadget. Replace the one in the sample.xml if you are using a copy of that. Save your gadget and click the verification web site button to complete the process. Presumably this will give you the consumer key and consumer secret that you need, but I have not gotten this to work for some reason.
See
http://groups.google.com/group/opensocial-orkut/browse_thread/thread/43640406754ab7f6

No comments: