Tải bản đầy đủ (.pdf) (50 trang)

Tài liệu Building OpenSocial Apps- P1 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (524.76 KB, 50 trang )

ptg
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Building
OpenSocial Apps
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
T
he Developer’s Library Series from Addison-Wesley provides
practicing programmers with unique, high-quality references and
tutorials on the latest programming languages and technologies they
use in their daily work. All books in the Developer’s Library are written by
expert technology practitioners who are exceptionally skilled at organizing
and presenting information in a way that’s useful for other programmers.
Developer’s Library books cover a wide range of topics, from open-
source programming languages and databases, Linux programming,
Microsoft, and Java, to Web development, social networking platforms,
Mac/iPhone programming, and Android programming.
Visit developers-library.com for a complete list of available products
Developer’s Library Series
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Building
OpenSocial Apps
A Field Guide to Working with the
MySpace Platform
Chris Cole
Chad Russell


Jessica Whyte
Upper Saddle River, NJ

Boston

Indianapolis

San Francisco
New York

Toronto

Montreal

London

Munich

Paris

Madrid
Capetown

Sydney

Tokyo

Singapore

Mexico City

From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and the
publisher was aware of a trademark claim, the designations have been printed with initial
capital letters or in all capitals.
The screenshots and other depictions of myspace.com contained in this book may not
accurately represent myspace.com as it exists today or in the future, including without
limitation with respect to any policies, technical specs or product design.
The authors and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or
omissions. No liability is assumed for incidental or consequential damages in connection
with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales, which may include electronic versions and/or custom covers
and content particular to your business, training goals, marketing focus, and branding
interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419

For sales outside the United States, please contact:
International Sales

Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Cole, Chris, 1974-
Building OpenSocial apps : a field guide to working with the
MySpace platform/Chris Cole, Chad Russell, Jessica Whyte.
p. cm.

Includes bibliographical references and index.
ISBN-13: 978-0-321-61906-8 (pbk. : alk. paper)
ISBN-10: 0-321-61906-4 (pbk. : alk. paper)
1. Entertainment computing. 2. Internet programming. 3. MySpace.com. 4. OpenSocial.
5. Web site development. 6. Social networks—Computer network resources.
7. Application program interfaces (Computer software) I. Russell, Chad. II. Whyte, Jessica. III. Title.
QA76.9.E57C65 2010
006.7'54—dc22
2009032342
Copyright © 2010 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by
copyright, and permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means,
electronic, mechanical, photocopying, recording, or likewise. For information regarding
permissions, write to:
Pearson Education, Inc
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax: (617) 671-3447
ISBN-13: 978-0-321-61906-8
ISBN-10: 0-321-61906-4
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, October 2009
Editor-in-Chief
Mark L. Taub
Acquisitions Editor
Trina MacDonald
Development
Editor

Songlin Qiu
Managing Editor
John Fuller
Full-Service
Production
Manager
Julie B. Nahil
Project
Management
diacriTech LLC
Copy Editor
Barbara Wood
Indexer
Jack Lewis
Proofreader
George Seki
Technical
Reviewers
Cassie Doll
Bess Ho
Benjamin Schupak
Book Designer
Gary Adair
Compositor
diacriTech LLC
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg

This book is dedicated to my

ever-suffering wife, Kristen, and our two
crazy and wonderful children, Darien and
Reece.Thanks for working overtime with
the kids, baby
.
—Chris Cole
To the reader, we hope this book
serves you well.
—Chad Russell and Jessica Whyte

From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Contents at a Glance
Contents
viii
Foreword
xvi
Acknowledgments
xviii
About the Authors
xix
Introduction
xxi
I: Building Your First MySpace Application
1
Your First MySpace App
3
2
Getting Basic MySpace Data

9
3
Getting Additional MySpace Data
29
4
Persisting Information
47
5
Communication and Viral Features
67
6
Mashups and External Server
Communications
91
7
Flushing and Fleshing: Expanding Your
App and Person-to-Person Game Play
117
II: Other Ways to Build Apps
8
OAuth and Phoning Home
153
9
External Iframe Apps
177
10
OSML, Gadgets, and the Data Pipeline
213
11
Advanced OSML: Templates, Internationalization,

and View Navigation
239
III: Growth and How to Deal with It
12
App Life Cycle
265
13
Performance, Scaling, and Security
283
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
viivii
14
Marketing and Monetizing
305
15
Porting Your App to OpenSocial 0.9
329
References
351
Index
355
Contents at a Glance
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Contents
Foreword xvi
Acknowledgments xviii

About the Authors xix
Introduction xxi
I: Building Your First MySpace Application
1 Your First MySpace App 3
Creating the App—“Hello World” 3
Step 1: Sign Up for a Developer Account 3
Step 2: Create an App 4
Step 3: Enter Your Source Code 4
Installing and Running Your App 7
Summary 7
2 Getting Basic MySpace Data 9
The Two Concepts That Every Developer
Should Know 9
Basic Concepts: Owner and Viewer 9
Basic Concepts: Permissions for Accessing
MySpace Data 10
Starting Our Tic-Tac-Toe App 10
Accessing MySpace User Data 11
Accessing Profile Information Using the
opensocial.Person Object 15
Getting More than Just the Default Profile Data 18
opensocial.DataResponse and opensocial.
ResponseItem (aka, Using MySpace User Data) 19
Error Handling 24
Summary 27
3 Getting Additional MySpace Data 29
How to Fetch a Friend List and Make Use of
the Data 29
Getting the Friend List 30
Filters and Sorts 31

From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
ix
Contents
Paging 32
Using the Data 37
Fetching Media 39
Photos 39
Albums and Videos 41
Using opensocial.requestPermission and
opensocial.hasPermission to Check a User’s
Permission Settings 43
Summary 45
4 Persisting Information 47
App Data Store 47
Saving and Retrieving Data 48
Refactoring to Build a Local App Data Store 51
Cookies 56
Why You Shouldn’t Use Cookies 57
Building the Cookie Jacker App 59
Third-Party Database Storage 64
Summary 65
5 Communication and Viral Features 67
Using opensocial.requestShareApp to Spread Your App to
Other Users 67
Defining requestShareApp 70
Writing the requestShareApp Code 71
Calling requestShareApp 72
The requestShareApp Callback 72

Using opensocial.requestSendMessage to Send Messages
and Communications 74
Defining requestSendMessage 75
Writing the requestSendMessage Code 76
Callback in requestSendMessage 78
Getting Your App Listed on the Friend Updates with
opensocial.requestCreateActivity Basics 79
Defining opensocial.requestCreateActivity 79
Using the Template System to Create Activities 80
Data Types 80
Reserved Variable Names 81
Aggregation 82
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
x
Contents
Body and Media Items 82
Using the Template Editor to Create Templates 83
Using opensocial.requestCreateActivity 85
Sending Notifications 88
Summary 90
6 Mashups and External Server Communications 91
Communicating with External Servers 91
Mashups 92
Adding a Feed Reader to Our App 93
Overview of gadgets.io.makeRequest 94
Response Structure 96
Handling JSON Content 97
Handling Partial HTML Content 97

Handling RSS Feed Content 97
Handling XML Content 98
“User’s Pick” Feed Reader 98
Setup and Design of the Feed Reader 98
FEED Content Type 104
XML Content Type with Parsing 105
TEXT Content Type 107
Adding a Feed Refresh Option 109
Feed Automation Candy 110
Secure Communication with Signed
makeRequest 111
Adding an Image Search 112
Overview of JSONP 112
Implementing the Image Search 113
Posting Data with a Form 114
Summary 114
7 Flushing and Fleshing: Expanding Your App and
Person-to-Person Game Play 117
Turn-Based Games 117
Design Overview 118
Adding FriendPicker 119
App Data Game Store 125
Supporting Person-to-Person Game Play 133
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
xi
Contents
Adding P2P Game Play Support in the Game
Engine 133

Adding User Feedback 135
Fleshing Out P2P Game Logic 138
Finishing and Clearing a Game 144
“Real-Time” Play 146
Advantages and Disadvantages of
App Data P2P Play 148
Summary 148
II: Other Ways to Build Apps
8 OAuth and Phoning Home 153
What Is OAuth? 153
OAuth Libraries 154
Setting Up Your Environment 154
When Is OAuth Not OAuth? 157
Secure Phone Home 157
Unsigned GET Request 158
Signed POST Request 162
Testing Your OAuth Implementation Locally 166
Making Real MySpace Requests 169
Spicing Up the Home and Profile Surfaces Using
makeRequest 173
Summary 174
9 External Iframe Apps 177
REST APIs 178
How a REST Web Service Is Addressed 178
Setting Up an External Iframe App 179
The Server Code 181
REST API List 183
The Client Code 197
Friends Web Service and Paging 199
The Profile Endpoint 203

Sending Messages Using IFPC 208
Using the 0.7 Container for postTo 210
The Friends Response from the REST API 211
Summary 212
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
xii
Contents
10 OSML, Gadgets, and the Data Pipeline 213
The Big Picture 213
Gadget XML 214
Data Pipeline 214
OSML 214
Writing a Gadget 214
“Hello World” Gadget 214
Adding a Second Surface to the Gadget 217
Declaring and Using Basic Data 218
Data Pipelining 219
DataContext 220
Data Tags 220
In-Network versus Out-of-Network Data 221
Data Tags os:ViewerRequest and
os:OwnerRequest 222
Data Tag os:PeopleRequest 222
Data Tag os:ActivitiesRequest 223
Data Tag os:DataRequest 223
JavaScript Blocks in OSML Apps 225
OpenSocial Markup Language (OSML) 225
Basic Display Tags 226

Remote Content Display Tags 226
Control Flow Tags 226
Putting It Together: OSML Tic-Tac-Toe 226
Setting Up the Gadget 227
Reusing Common Content 230
Working with Data 235
Displaying Data Lists 237
Summary 238
11 Advanced OSML: Templates, Internationalization,
and View Navigation 239
Inline Tag Templates 239
Defining and Using a Tag Template 240
Using Client-Side Templates 242
Working with Subviews 245
Converting Tabs to Subviews 245
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
xiii
Contents
HTML Fragment Rendering 248
Adding Content with os:Get 248
Adding Targeted Content with
myspace:RenderRequest 249
Data Listeners 250
Displaying JSON Results with a Data Listener 251
Internationalization and Message Bundles 255
Creating Our First Message Bundle 256
Creating Translations of the Message Bundle 257
Including Translations in an App and Testing 258

Future Directions 260
Summary 261
III: Growth and How to Deal with It
12 App Life Cycle 265
Publishing Your App 265
What’s Allowed, or Why So Many Apps
Get Rejected 266
Dealing with Rejection 267
Contesting a Rejection 267
Managing Your App 274
Hiding and Deleting an App 274
Making Changes to a Live App
(Multiple Versions) 274
Republishing a Live App 275
Changing the App Profile/Landing Page 275
Managing Developers 279
Managing Testers 279
Event Handling—Installs and Uninstalls 279
Suspension and Deletion of Your App 280
Summary 281
13 Performance, Scaling, and Security 283
Performance and Responsiveness 283
What Is Responsive Performance and What Is
Scale Performance? 283
Design for Responsiveness 284
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
xiv
Contents

Responsive OpenSocial App Performance
Guidelines 285
Design for Scale 292
App Guidelines for Internet-Scale Performance 293
Stability and Fault Tolerance 299
Rule 1: Validate Inputs 299
Rule 2: Test OpenSocial DataResponse Objects
for Errors 300
Rule 3: Provide Time-Outs and Error Flow 300
Rule 4: Don’t Assume That Weird Error Was
an Anomaly 300
User and Application Security 300
User Data Security 301
Application Security 301
Hacking and Cracking 302
Summary 303
14 Marketing and Monetizing 305
Using MySpace to Promote Your App 306
The App Gallery 306
App Profile, or Bringing Out the Bling 308
MySpace’s Own MyAds 308
User Base and Viral Spreading 309
Listen to Your Customers 311
Ads 311
Google AdSense 311
Cubics 313
RockYou! Ads 314
Micropayments 316
PayPal 316
Boku 317

Others 318
Interviews with Successful App Developers 318
Dave Westwood: BuddyPoke
(www.myspace.com/buddypoke) 318
Eugene Park: Flixster
(www.myspace.com/flixstermovies) 321
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
xv
Contents
Tom Kincaid: TK’s Apps
(www.myspace.com/tomsapps) 322
Dan Yue: Playdom
(www.myspace.com/playdom) 324
Summary 326
15 Porting Your App to OpenSocial 0.9 329
Media Item Support 330
opensocial.Album 330
Fetching Albums 333
Fetching Media Items 335
Updating Albums and Media Items 338
Uploading Media Items 340
Simplification of App Data 341
REST APIs 343
Summary 348
References 351
Index 355
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

ptg
Foreword
The Internet is constantly evolving, with vast arrays of information on every topic
growing at a remarkable pace. Google’s 1998 search index had only 26 million Web
pages; a decade later it recognizes more than 1 trillion URLs.With so much
information, we need a new set of tools to make sense of the Internet.The transition is
from a strict focus on informational content, to being able to take advantage of our con-
text, our relationships, and our activities.
Enter the social Web, a relatively new twist to the Internet, which is being built up by
social networks, portals, and even more traditional businesses.The “Open Stack” is a set
of specifications being developed by grass-roots communities all over the world, enabling
developers to create new products and services enhanced by user-specific data.The
Open Stack includes specifications such as OAuth, which provides secure access to data;
OpenID, a global identity standard; and OpenSocial, a common API for building applica-
tions.These specifications are becoming the underlying infrastructure for the social Web,
weaving a social fabric throughout the Web.
OpenSocial enables developers to learn a single core programming model that can
be applied to all “OpenSocial containers,” those sites that support the OpenSocial
specification.With standards-based tools, including a JavaScript-based gadget API,
a REST-based data access API, lightweight storage capabilities, and access to common
viral channels, developers can build inside those containers as well as create applications
for mobile phones or other sites. In late 2009, less than two years after its introduction,
more than 50 sites have implemented support for the OpenSocial specification. In
aggregate, these sites provide developers with access to more than 750 million users all
over the world.
By taking advantage of the OpenSocial API and the information available in this
book, you will have a great opportunity to reach a lot of users. For example, you’ll
find the OpenSocial API supported by many major sites that span the globe:Yahoo!,
iGoogle, Xiaonei and 51.com (China), Mixi (Japan), orkut (Brazil), Netlog (Europe),
and, of course, MySpace. Beyond that, OpenSocial is also supported by more

productivity-oriented sites like LinkedIn and by Fortune 100 companies as diverse as
IBM and Lockheed Martin.
With this book, you can quickly get up and running with OpenSocial on MySpace,
and you’ll be poised to leverage that experience to reach users on other sites as well.The
in-depth programming examples provide a good introduction to the design options
available when building with the OpenSocial API, and the code is open source for ease
of use and future reference. Additionally, the MySpace Platform tips sprinkled
throughout will help you avoid common mistakes and understand the intricacies of their
platform policies. Getting a feel for how social platforms operate will be valuable as you
continue to explore the wide world of OpenSocial.
OpenSocial is constantly evolving, just like the rest of the Internet.The OpenSocial
specification is managed through an open process where anyone can contribute their ideas
to influence the next version of the specification and help move the social Web forward.
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Since its creation, there have been several significant revisions to the specification,
introducing some new programming methodologies and improvements that make it easier
for new developers to start using OpenSocial. As you’re getting into the OpenSocial API,
be sure to contribute back to the OpenSocial.org community your ideas on how to
improve the specification.
It’s open. It’s social. It’s up to you.
—Dan Peterson, president, OpenSocial Foundation
San Francisco, California
August 2009
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
Acknowledgments
Chris Cole: I’d like to acknowledge the great team at MySpace that helped build the

developer platform and all the people who’ve contributed to refining the OpenSocial
specification. Compromise is hard, but a bad spec would have been even harder.
Chad Russell: Thank you to Dan Peterson, for being a resource and agreeing to write
our Foreword.And, of course, a big thanks to our technical editors, Bess Ho, Cassie Doll,
and Ben Schupak, who all found a number of issues that would otherwise slipped
through the cracks.
Jessica Whyte: Thank you to Trina MacDonald and Olivia Basegio at Addison-Wesley for
your support. I’d also like to acknowledge Tom Kincaid, Eugene Park, Dave Westwood,
Dan Yue, Jon Nguyen, and Katie Simpkins for taking the time to answer all of my (many)
questions about the platform.
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
About the Authors
Chris Cole is a software architect at MySpace and is a major contributor to building
the MySpace Open Platform and keeping it committed to OpenSocial. He’s been a core
contributor to the OpenSocial 0.9 specification and is the primary architect and imple-
menter of OSML (OpenSocial Markup Language) on the MySpace platform. Chris has
been in software for fifteen years and has spent much of that time riding the various
waves of the Internet and pushing the boundaries of the Web.
Chad Russell is the lead developer on the MySpace OpenSocial team. He knows the
OpenSocial spec front to back, in addition to every tip, trick, and nuance of the
MySpace platform itself. Chad holds an engineering degree from the University of
Toronto and currently resides in Seattle,Washington.
Jessica Whyte has worked for several years as a journalist, most recently with Journalists
for Human Rights, and is currently a graduate student at the University of Washington,
studying Human-Centered Design and Engineering. She lives in Seattle with her
husband and coauthor, Chad Russell.
From the Library of Lee Bogdanoff
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×