javaone: creating serverside and mobile mashups with the opensocial java client libraries
DESCRIPTION
Presentation at JavaOne 09 on creating server-side and mobile mashups with the OpenSocial Java client libraries.TRANSCRIPT
Creating Server-Side andMobile Mashups withOpenSocial's JavaTM ClientLibraries
Chris SchalkRyan BoydGoogleJavaOne 2009
2
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - OAuth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
3
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - OAuth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
What is OpenSocial?
“OpenSocial defines a common set of APIs based onOpen Standards for building social applications acrossmultiple websites”
What is OpenSocial?
Before OpenSocial…
What is OpenSocial?
Standards Based!
What is OpenSocial?
Learn once… Write anywhere
Who owns OpenSocial?
OpenSocial is managed under the auspices of the“OpenSocial Foundation” - http://www.opensocial.org
No!?
Who’s Using it?
And many more…
OpenSocial now has over 800 Million users
Demonstration
> A simple OpenSocial gadget demonstration
12
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - OAuth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
The Evolution of the OpenSocial RESTSpecification
> Before v. 0.8, the only OpenSocial API was aJavaScript API Only worked within gadgets
> Initial OpenSocial API did not have a RESTSpecification. No server-to-server communication No options for Desktop or Mobile integration
Lack of REST support meant no server-side integration> Through OpenSocial’s open specification process,
the community responded and created theOpenSocial REST specification
OpenSocial REST support rolls out
> Version 0.8.1 RESTful and RPC protocols established
RPC protocol responds to HTTP POSTs and allows forbatching of requests
Server responded to REST requests in either XML orJSON
AtomPub operations supported
16
OpenSocial REST Support
Opens new development models
• Server-to-server communication
• Multiple client types
• Server based UIs (JSP/JSF)
• Mobile devices
17
RESTful Protocol• Resources are URLs.
/people/{guid}/@all
• All people connected to the given user:Example - People:
/people/{guid}/@friends
• All friends of the given user:
/people/{guid}/@self
• Profile of the given user:
/people/@me/@self
• Profile of the authenticated user:
/people/@supportedFields
• Supported Person fields:
18
• Request extra fields
• Filtering:
• Paging:
fields={-join|,|field}.
filterBy={fieldname}filterOp={operation}filterValue={value}updatedSince={xsdDateTime}networkDistance={networkDistance}
count={count} sortBy={fieldname}sortOrder={order}startIndex={startIndex}
format={format}
RPC Protocol• Http POST instead of GET
Demonstration
> Experimenting with the the RESTful protocol withlocal Apache Shindig and a browser
20
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - OAuth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
OpenSocial Client Libraries
21
A set of client libraries for thatenable direct communication toan OpenSocial server.
• Client libraries exist for PHP, Ruby, Python and Java• Supports both REST and RPC protocols• Documentation Wiki pages• Sample applications provided
OpenSocial Client Libraries
22
http://code.google.com/p/opensocial-php-clienthttp://code.google.com/p/opensocial-ruby-clienthttp://code.google.com/p/opensocial-python-clienthttp://code.google.com/p/opensocial-objc-clienthttp://code.google.com/p/opensocial-java-client
Hosted on Google code projects
Demonstration
> Getting familiar with the OpenSocial Java clientlibrary
24
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - OAuth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
What about authorization?
> OAuth to the rescue!
> “An open protocol to allow secure APIauthorization in a simple and standard methodfrom desktop and web applications” - Oauth.net
> The OpenSocial Client Libraries offer built-insupport for OAuth
The OAuth Authorization Process Also known as “3-legged OAuth”
WebDesktopMobile
Application
OAuth Provider
SecuredResource
1. HTTP Request for unauthorized token
2. Response with with unauthorized token
3. Authorization Request for request token
7. HTTP Request for access token
9. Request for secured resource with access token
6. Redirect with authorized token
8. Response with with access token
4. Redirect forAccess consent
5. User choice accept/deny access
10. Response with requested data
Access ConsentPage
2-legged vs. 3-legged OAuth
> “2-legged” OAuth End-user installs a social network application, which authorizes app developer to
have access to their data App developer is given a consumer key and consumer secret to access the data of
any user who has installed their app, without any additional authorization Access is revoked by the end-user when they remove the app
> “3-legged” OAuth End-user is using native app or web app and hasn’t necessarily installed an app by
the same developer on the SNS If necessary, end-user is asked to authenticate to data provider End-user is sent to a webpage to authorize access to their data After authorization is granted, end-user returns to the app Access can sometimes be revoked by the end-user using an account
settings/preferences page
> The OpenSocial client libraries support both authorization types. Not all OpenSocial containers support both authorization types however.
28
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - OAuth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
29
Fetching Data from an OpenSocial container Using “2-legged OAuth”
OpenSocialClient c = new OpenSocialClient(OpenSocialProvider.valueOf("ORKUT_SANDBOX"));
c.setProperty(OpenSocialClient.Property.CONSUMER_SECRET, "uynAeXiWTisflWX99KU1D2q5"); c.setProperty(OpenSocialClient.Property.CONSUMER_KEY, "orkut.com:623061448914"); c.setProperty(OpenSocialClient.Property.VIEWER_ID, "03067092798963641994");
try { // Retrieve the friends of the specified user using the OpenSocialClient Collection<OpenSocialPerson> friends = c.fetchFriends();
for (OpenSocialPerson friend : friends) { System.out.println("- " + friend.getDisplayName()); } } catch (org.opensocial.client.OpenSocialRequestException e) { System.out.println("OpenSocialRequestException thrown: " + e.getMessage()); e.printStackTrace(); }
Demonstration
> A 2-legged OAuth transaction using the Javaclient library. Requesting profile information and friends list from
orkut.com
Demonstration
> A 3-legged OAuth transaction example. Doing the “dance” with MySpace.com Using JSPs to show passing user to authorization
page
32
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - Oauth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
Building Server-Side Mashups with the OpenSocial client libraries
> Any server-side Java Web application can easilyintegrate with an OpenSocial server
> An example: A aviation themed website with social features
enabled via the client libraries App uses Java App Engine Connects to Google Friend Connect which is an
OpenSocial container that supports 2-legged OAuth.
Demonstration
> Examining the “Plane Crazy” demo site.
Plane Crazy getGFCProfileInfo()
final OpenSocialProvider provider = OpenSocialProvider.valueOf("FRIENDCONNECT");final OpenSocialClient client = new OpenSocialClient(provider);
OpenSocialPerson person; try { person = client.fetchPerson(); session.setAttribute("gfcid", person.getId()); session.setAttribute("displayname", person.getDisplayName()); session.setAttribute("thumbnailurl", person.getThumbnailUrl()); session.setAttribute("GFCUser", "yes"); }...
36
Agenda
> OpenSocial Background/Overview> OpenSocial REST support> Introducing the REST client libraries> OpenSocial Authorization - Oauth> Using the OpenSocial Java client library> Building Server-Side Mashups> Building Mobile Mashups> Summary
Building Mobile Mashups with the OpenSocial client libraries
> Any native mobile phone applications that cansupport OAuth and the REST protocol cancommunicate with an OpenSocial container
> For Java, this is easy. The OpenSocial Java client library comes with a
ready-to-run Android sample application whichdemonstrates extracting contact info from Plaxousing 3-legged OAuth.
Demonstration
> A simple 3-legged OAuth demonstration from amobile phone. Accessing Plaxo contacts info from an Android
phone.
REST
Building Mobile Mashups a more advanced Mobile example
> How an existing mobile game was made socialwith OpenSocial and the REST client libraries
+
Demonstration
> The Social divide and conquer game!
Divide and Conquer step-by-step
Divide and Conquer selecting a social network
Divide and Conquer onDialogClosed()
token = OpenSocialOAuthClient.getRequestToken( client, provider);persistRequestToken(token, providerString);String url = OpenSocialOAuthClient. getAuthorizationUrl(provider, token, ANDROID_SCHEME + "://opensocial");
// Browse to webpageIntent i = new Intent(Intent.ACTION_VIEW);i.setData(Uri.parse(url));getContext().startActivity(i);
Divide and Conquer in the background
Divide and Conquer getTopScoreList()
ArrayList<Hashtable<String,String>> returnList = null;List<Object> args = new ArrayList<Object>();args.add("GetTopScores");try { returnList = getScoreInfoFromJson(callBackendRPC(args));} catch (Exception ex) { ex.printStackTrace();}return returnList;
Divide and Conquer getSelfAndFriends()
friends = c.fetchFriends();self = c.fetchPerson();
for (OpenSocialPerson person : friends) { returnHash.put(provider.toString() + "_" + person.getId(), person);}if (self != null) { returnHash.put(provider.toString() + "_" + self.getId(), self);}
Divide and Conquer getTopScoresForFriends()ArrayList<Hashtable<String,String>> returnList = null;OpenSocialClient c = osClient;
List<Object> args = new ArrayList<Object>();args.add("GetTopScoresForFriends");
while (friends.hasMoreElements()) { args.add(friends.nextElement());}try { returnList = getScoreInfoFromJson(callBackendRPC(args));} catch (Exception ex) { ex.printStackTrace();}return returnList;
Divide and Conquer getPicturesForFriends()
Hashtable<String,String> userPictures = new Hashtable....for (Hashtable<String,String> score : topScoreList) { String user = score.get(“user”); OpenSocialPerson person = friends.get(user); if (person.getField(“thumbnailUrl”) != null) { thumbnailUrl = person.getField(“thumbnailUrl”). getStringValue(); // ** download thumbnail here, save to file ** userPictures.put(user, user + “.img”); }}
Divide and Conquer when you die
Divide and Conquer postUpdate()
List<Object> args = new ArrayList<Object>();args.add("SetScore");args.add(provider.toString() + "_" + person.getId());args.add(level);args.add(timeTaken);args.add(person.getDisplayName());callBackendRPC(args);
Divide and Conquer social scoreboard
Summary
>OpenSocial supports innovative server-sideand mobile integrations via its support ofRESTful and RPC protocols
>The OpenSocial Java client libraries take allthe heavy lifting out of communicating viathese protocols.
>Any mobile, desktop or server-side Javadeveloper can easily integrate OpenSocialand social features into their applications!
53
Chris SchalkRyan Boyd
Google Developer AdvocatesJavaOne 2009