53
Creating Server-Side and Mobile Mashups with OpenSocial's Java TM Client Libraries Chris Schalk Ryan Boyd Google JavaOne 2009

JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Embed Size (px)

DESCRIPTION

Presentation at JavaOne 09 on creating server-side and mobile mashups with the OpenSocial Java client libraries.

Citation preview

Page 1: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Creating Server-Side andMobile Mashups withOpenSocial's JavaTM ClientLibraries

Chris SchalkRyan BoydGoogleJavaOne 2009

Page 2: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 3: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 4: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

What is OpenSocial?

“OpenSocial defines a common set of APIs based onOpen Standards for building social applications acrossmultiple websites”

Page 5: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

What is OpenSocial?

Before OpenSocial…

Page 6: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

What is OpenSocial?

Standards Based!

Page 7: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

What is OpenSocial?

Learn once… Write anywhere

Page 8: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Who owns OpenSocial?

OpenSocial is managed under the auspices of the“OpenSocial Foundation” - http://www.opensocial.org

No!?

Page 9: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Who’s Using it?

And many more…

Page 10: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

OpenSocial now has over 800 Million users

Page 11: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> A simple OpenSocial gadget demonstration

Page 12: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 13: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 14: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Lack of REST support meant no server-side integration> Through OpenSocial’s open specification process,

the community responded and created theOpenSocial REST specification

Page 15: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 16: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

16

OpenSocial REST Support

Opens new development models

• Server-to-server communication

• Multiple client types

• Server based UIs (JSP/JSF)

• Mobile devices

Page 17: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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:

Page 18: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 19: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> Experimenting with the the RESTful protocol withlocal Apache Shindig and a browser

Page 20: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 21: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 22: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 23: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> Getting familiar with the OpenSocial Java clientlibrary

Page 24: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 25: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 26: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 27: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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.

Page 28: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 29: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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(); }

Page 30: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> A 2-legged OAuth transaction using the Javaclient library. Requesting profile information and friends list from

orkut.com

Page 31: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> A 3-legged OAuth transaction example. Doing the “dance” with MySpace.com Using JSPs to show passing user to authorization

page

Page 32: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 33: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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.

Page 34: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> Examining the “Plane Crazy” demo site.

Page 35: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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"); }...

Page 36: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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

Page 37: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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.

Page 38: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> A simple 3-legged OAuth demonstration from amobile phone. Accessing Plaxo contacts info from an Android

phone.

REST

Page 39: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Building Mobile Mashups a more advanced Mobile example

> How an existing mobile game was made socialwith OpenSocial and the REST client libraries

+

Page 40: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Demonstration

> The Social divide and conquer game!

Page 41: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Divide and Conquer step-by-step

Page 42: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Divide and Conquer selecting a social network

Page 43: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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);

Page 44: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Divide and Conquer in the background

Page 45: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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;

Page 46: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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);}

Page 47: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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;

Page 48: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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”); }}

Page 49: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Divide and Conquer when you die

Page 50: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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);

Page 51: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

Divide and Conquer social scoreboard

Page 52: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

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!

Page 53: JavaOne: Creating Serverside and Mobile Mashups with the OpenSocial Java client libraries

53

Chris SchalkRyan Boyd

Google Developer AdvocatesJavaOne 2009