36,40 €
Twitter is rapidly moving up the social networking food chainand is currently outranked by only Facebook and MySpace. Itfeatures a programming API that allows you to build Web sites andapplications (both desktop and mobile) for reading and posting toTwitter, finding other Twitter users, aggregating Twitter content,and other uses. This book walks you through the process ofcombining many programming tools in order to build exciting,useful, and profitable applications.You'll begin with a look at RESTful services and examine how tostructure your queries, handle asynchronous operations, useheaders, and post binary data. From there, author and TweetSharpdeveloper Daniel Crenna explains how to authenticate with the OAuthspecification for Web and Windows applications.* Twitter is growing in popularity at a rapid pace and this bookshows you how to take advantage of its programming API to buildapplications* Explains the various ways to design a Twitter application,including caching, third party application interoperability,real-time data binding, push vs. pull data scenarios, and more* Takes an in-depth look at TweetSharp, a .NET library fordeveloping Twitter applications-whose creator is also the author ofthis book* Walks you through requesting and retrieving responses fromTwitter's API* Warns you of considerations to take into account regardingauthentication and securityProfessional Twitter Development shows you how to get themost out of Twitter so that you can build your own applications forthis exciting new platform.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 482
Veröffentlichungsjahr: 2009
Table of Contents
Chapter 1: Working with Restful Services
What is REST?
The REST Paradigm
HTTP and URIs
The Anatomy of a URI
The Anatomy of HTTP Requests and Responses
Planning Features using HTTP Headers
Twitter and REST
Communicating with the Web and .NET
WebClient, HttpWebRequest and HttpWebResponse
Creating a Request Utility
Summary
Chapter 2: The Twitter Rest Api
Obtaining a Twitter Account
Overview
Resources
Representations
Authenticating Users
API Rate Limits
Twitter Features
Statuses
Users
Direct Messages [A] [R]
Friendships
Social Graphing
Account
Favorites
Notifications
Blocks
Help
Twitter Objects
Statuses
Users
Direct Messages
Rate Limit Status
Errors
Working with the API
Twitter’s DateTime Format
Configuring a Custom Application Source
Handling Errors
Requesting Data from Twitter
Summary
Chapter 3: Working with XML, JSON, and Jsonp
Working with XML Responses
Using LINQ to XML
Using XML Attributes and XmlSerializer
Working with JSON Responses
Using ASP.NET AJAX’s JavaScriptSerializer
Using WCF’s DataContractJsonSerializer
Calling Back to Client Code with JSONP
Using Silverlight
Security Restrictions with JSONP
Summary
Chapter 4: The Twitter Search API
Overview
Search Features
Search Operators
Boolean Logic
Exact Phrase
Exclusions
Hashtags
Referencing Users
Timelines
Context
Search Parameters
Pagination
Timelines
Callbacks
Globalization
Search Objects
Queries
Trends
Converting Queries and Trends
Summary
Chapter 5: Working with RSS and Atom Syndication
Syndication on the Web
Feed Objects
RSS 2.0
Atom 1.0
Consuming Feeds
Why Syndication?
With LINQ to XML
Synchronizing Applications with Feed Updates
Summary
Chapter 6: Basic Authentication and OAuth
The Pitfalls of Basic Authentication
User Credentials are Visible
Credentials are Reusable
Credentials are One-Way
Existing Alternatives to Basic Authentication
What Is Data Portability?
What Is OAuth?
User Credentials are Hidden
Credentials are Not Reusable
Credentials are Two-Way
The OAuth Specification
OAuth Workflow
Retrieving an Unauthorized Request Token
Walkthroughs
Setting Up Your Twitter Application to Use OAuth
Authenticating an ASP.NET Web Application
Authenticating a Desktop, Console, or Mobile Application
Summary
Chapter 7: Maximizing Performance and Functionality
Caching
User Photos
Statuses and Users
Working with Twitter Constraints
Storing Authentication Values
Uploading Files with Multi-Part Form Posts
Rate Limiting in the Response
Designing Applications with Rate Limits
Retrieving Data for Popular Users
Filtering Data
Compressing Response Data
Extending Twitter with Third-Party Applications
Adding Photo Features with TwitPic, yFrog and Twitgoo
Stay on Top of Trends with TwitScoop
Twitter Application Directories
Unit Testing Twitter
Mocking RESTful Services
Generating Data
Summary
Chapter 8: Data Push vs. Pull
Pushing Data
Advantages
Disadvantages
Using WCF Duplex Services
Using Gnip
Using the Twitter Streaming API
Summary
Chapter 9: Introduction to TweetSharp
Hello, TweetSharp!
What Is a Fluent Interface?
Core Feature Overview
Hello, Twitter!
Fetching Timelines
Performance
Caching Queries
Features
Unit Testing
Summary
Chapter 10: Building a Cross-Platform Twitter Application
The Application: Twiticism.com
Problem
Design
The Azure Cloud
Shared Key Authentication
Designing Applications for the Cloud
Working with Azure Queues
Working with Azure Tables
Hosting a Twitter Proxy in the Cloud
Running a Global User Cache in the Cloud
ASP.NET Membership in the Cloud
Silverlight Application Highlights
Building a Twitter Image Handler
Building a Twitter Search Poller
Going Cross-Platform
Summary
Professional Twitter® Development With Examples in .NET 3.5
by Daniel Crenna
Professional Twitter® Development With Examples in .NET 3.5
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
ISBN: 978-0-470-53132-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online atwww.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2009931751
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Twitter is a registered trademark of Twitter, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Dedicated to my family:
Those we recently lost who remain, those who will join with us soon, and those yet to be. There is so much love here, and if we seldom speak of our feelings it is only because it is difficult to express the irreplaceable.
About the Author
Daniel Crenna is a Microsoft MVP and the creator of TweetSharp, an open-source .NET Twitter API development library. He runs www.dimebrain.com, a blog and software company in Canada. His Twitter client project, currently in development in partnership with Jason Diller, was a Top 5 Finalist in the Microsoft BlueSky Innovation Excellence Awards in 2009. Daniel was a contributor to the Microsoft AJAX Control Toolkit, and is a Microsoft Certified Professional Developer.
Credits
Acquisitions Editor
Paul Reese
Project Editor
Ed Connor
Technical Editor
Jason Diller
Production Editor
Rebecca Anderson
Copy Editor
Tricia Liebig
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Nate Pritts, Word One
Indexer
J&J Indexing
Acknowledgments
Writing a book is never a solo effort. This particular one would not exist without the efforts of Ed Connor at Wiley keeping me on track and focused on the next milestone, and Jason Diller for a matchless technical review and development assistance, and keeping the lights on our community efforts whileI was consumed by this project. Ingrid Alongi at Gnip was an immediate source of encouragement and advice, and I was fortunate to have Doug Williams and Alex Payne at Twitter on tap for any questions I had throughout the evolutions that occurred with the Twitter API. Above all my wife Beth, who single-handedly moved us across the country and tolerated my mental and physical absence during the writing of this book, is as much a part of this project as I am.
Introduction
Chapter 1: Working with Restful Services
Chapter 2: The Twitter Rest Api
Chapter 3: Working with XML, JSON, and Jsonp
Chapter 4: The Twitter Search API
Chapter 5: Working with RSS and Atom Syndication
Chapter 6: Basic Authentication and OAuth
Chapter 7: Maximizing Performance and Functionality
Chapter 8: Data Push vs. Pull
Chapter 9: Introduction to TweetSharp
Chapter 10: Building a Cross-Platform Twitter Application
Since Twitter launched in March 2006, it has grown steadily into one of the simplest, most popular, and most powerful social networking platforms. Perhaps more compelling than Twitter itself is the growing community of developers building applications for the service and taking it far beyond its roots as a simple messaging service. This book will teach you how to get the best out of Twitter to build your own applications for this exciting new technology, as well as provide some guidance on using some of the latest developer evolutions in the .NET Framework, like Silverlight and Windows Azure.
After spending a great deal of time building TweetSharp, an open source Twitter library, as well as a prototype for a commercial Twitter client application, it was clear that many developers would find a single resource on consuming RESTful services, and Twitter’s RESTful API in particular, a valuable addition to their capacity to build applications on this new and exciting service. With Twitter’s growth exploding and already enjoying major mainstream adoption, application developers need a timely resource to prepare to build applications for this new and lively audience. Twitter has many millions of users and this book can help you support that growth in the developer community.
Who This Book Is For
The primary readers for this book are web or Windows developers who are pursuing Twitter application or client development, or are developing part of a system that integrates core Twitter functionality, such as a web-based widget. The early chapters of the book deal with establishing a foundation for web programming using HTTP and RESTful service concepts. If you have lots of experience working with social APIs in multiple data formats using REST, you might want to advance to Chapter 2 and Chapter 4 right away to start learning the particulars of Twitter’s API, otherwise beginning at the beginning will help you understand Twitter, but also any social web API that employs REST concepts.
What This Book Covers
The book will focus on C# and .NET with supporting examples using a broad range of .NET technologies, such as Windows Forms, ASP.NET, Silverlight 3, WCF, LINQ, and Windows Azure. At the time of writing, Silverlight 3 is in Beta 1, and Windows Azure is in Community Technology Preview (CTP) status. Most of the examples of the book are designed to run in .NET 3.5, though you may reasonably adapt the code and understanding to function in previous versions of .NET, by rewriting any LINQ expressions to programmatically achieve similar results, and replacing extension methods with classic static methods. You will get the most benefit out of this book by using .NET 3.5, however, which supports development for Silverlight and Azure, which, combined with Twitter, will allow you to build next generation applications that will appeal to the broadest base of users.
How This Book Is Structured
Chapters 1 through 6 focus on RESTful services, the backbone of Twitter and many social data APIs are covered in sufficient depth for the developer to know how to structure their queries, handle asynchronous operations, utilize headers and post binary data. Continuing from an understanding of RESTful web communication, you will learn how to request and retrieve responses from Twitter’s API, with forays into data processing with XML and JSON (for REST API data), and RSS and Atom (for Search API data). Core topics conclude with coverage for application authentication and security, including Twitter’s upcoming OAuth security model at the forefront; you will learn how to authenticate with the OAuth specification for web and Windows applications.
Following from authentication and security are performance and feature considerations. In Chapters 7 through 10 you are introduced to a variety of important topics for developing custom applications; caching, third party application interoperability, push vs. pull data scenarios, and Twitter’s anatomy and constraints are explored in detail to paint a bigger picture of how a Twitter application is best designed. Next, you will get an in-depth look of TweetSharp, a .NET library for developing Twitter applications that will speed up your development and time to market for your own application ideas. Finally, you will bring many new concepts together using Windows Azure to go through the steps needed to build a Twitter application in the cloud.
Armed with a solid understanding of Twitter’s API, and the ability to fetch and process web data, you can now tackle the specific challenges of Twitter application development. You’ll learn how to design and build responsive applications that consider caching, multi-threading, and real time data binding to ensure your ideas convert smoothly to compelling experiences for Twitter users.
What You Need to Use This Book
This book focuses on building Twitter applications on the .NET Framework 3.5 SP1, including some emerging technologies like Silverlight 3 Beta 1 and the Windows Azure CTP. To ensure your environment is ready to work with the concepts and code provided in the book and its accompanying source, you can follow along with this introductory chapter. While some nascent technologies are showcased, using them is not necessary to benefit from the fundamentals of web programming and Twitter development and design concepts that you’ll learn and employ.
Setting Up Your Development Environment
The following steps outline the required server components, frameworks, and tools necessary for the various examples provided in the book. To help streamline the process, you will use a new Microsoft installation experience designed specifically for web application development; the Microsoft Web Platform Installer. With it, you can easily locate, download, install, and configure your web environment, saving time locating and setting up separate libraries around the web.
Using the Microsoft Web Platform Installer
Microsoft’s new Web Platform Installer, available at http://www.microsoft.com/Web/, provides a unified experience for installing and configuring most Microsoft web infrastructure components as well as popular open source software from a simple interface. After downloading and running the application, you are presented with an interface similar to Figure I-1, which shows the Web Platform Installer 2.0 Beta in action.
Figure I-1
IIS 7.0 with ASP.NET Components
Windows Azure’s SDK relies on Internet Information Services (IIS) 7, which means your development operating system must be Windows Vista or above to work with those features. You can find the required ASP.NET support through the Web Server section of this installer. Selecting ASP.NET from the menu will install several dependencies to enable .NET extensions in IIS 7.
ASP.NET MVC 1.0
While the book doesn’t cover the particulars of ASP.NET MVC, most of the code examples apply to any .NET environment. If you are planning to build your Twitter application on the ASP.NET MVC framework, now is a great time to install the bits. You can find this download under Frameworks and Runtimes.
Microsoft® Silverlight™ 3 Tools Beta 1 for Visual Studio 2008 SP1
To take advantage of the latest advancements in rich client technology, including running Silverlight applications out of the browser, you can download this dependency from the Tools section of the platform installer. You can also elect to download Silverlight 2, instead, if your projects don’t require or are not ready for out of browser support.
SQL Server 2008 Express
While not specifically required, SQL Server 2008 is installed as part of the Web Platform Installer components, and is used by Windows Azure to run a simulated cloud service environment locally, so you are able to test your applications before deploying them.
Visual Studio Web Developer Express and Above
Both the Windows Azure CTP and Silverlight 3 Tools for Visual Studio Beta 1 support Visual Studio Web Developer Express, so it is possible to run the examples in this book without a full-fledged professional development environment. You will also need to ensure you have installed the .NET Framework 3.5 SP1 from either Windows Update or the web before installing supporting libraries and SDKs. Figure I-2 demonstrates Visual Studio Web Developer Express with the code examples loaded and ready for testing.
Figure I-2
There are a few Windows Forms-based examples in the book as well, which you can run as executables, or use Visual C# Express available at http://www.microsoft.com/express/vcsharp/ to compile and edit those examples.
SDKs and Libraries
Beyond the Web Platform Installer are an SDK and a third-party library you will use to build cloud-based web applications. The SDK provides utility code for communicating with core Azure services, and the library helps you to work effectively with .NET and the Twitter API to simplify the programming tasks and challenges you’ll learn throughout the book.
Windows Azure Tools for Microsoft Visual Studio (including the Azure SDK)
You can download this SDK directly from Microsoft at http://go.microsoft.com/fwlink/?LinkId=128752 to enable an integrated environment to develop cloud-based services in a familiar setting in Visual Studio. The SDK examples also include valuable libraries for communicating with Azure Storage, and activating ASP.NET Provider model support for cloud services.
TweetSharp v1.0
You’ll need the official first release of TweetSharp, an open source .NET Twitter library that is useful for both beginning Twitter API developers and those who are already experienced (or have read this book) and want to accelerate development time by using TweetSharp to abstract away the details and get a prototype running quickly. You can fetch the latest release by visiting http://tweetsharp.googlecode.com/downloads. To use it on your projects, choose Add Reference from your Visual Studio project and select both Dimebrain.TweetSharp.dll and Newtonsoft.Json.dll if you are targeting ASP.NET, and the corresponding Compact and Silverlight suffixed libraries of the same name for mobile and rich development targets, respectively. Figure I-3 shows the TweetSharp page on Google Code, where you can find additional support, releases, and documentation.
Figure I-3
After installing your environment and its dependencies, you may want to visit http://www.microsoft.com/azure to familiarize yourself with the Azure platform and sign up for a Windows Live ID and Microsoft Connect account if you do not have either of these. You will go through the Azure sign up and configuration process in Chapter 10 when learning how to build a cloud-hosted web application, but you will save time if these services are in place.
Debugging HTTP Traffic
The vast majority of your time developing modern web applications is spent consuming external web services. Twitter application development is no exception, and the ability to work with HTTP traffic at a lower level than what’s immediately available in the .NET Framework provides valuable insight into what occurs under the hood when you program against the live web. To help you diagnose and understand what you send and receive when communicating with Twitter, you can download and configure Wireshark at http://wireshark.org, a free and open source HTTP analysis tool whose homepage is shown in Figure I-4. This section will explain how to use it effectively when consuming the Twitter API.
Figure I-4
Configuring Wireshark
Wireshark is a detailed and comprehensive tool, and as such requires customization to filter out all traffic other than what aids your development efforts with Twitter. To trace Twitter HTTP traffic in Wireshark exclusively, first locate the network adapter that is associated with your live Internet connection, as highlighted in Figure I-5. You can reach the dialog displayed by visiting the Capture menu and choosing Interfaces . . . from the options provided.
Figure I-5
Clicking the Options button for the network adapter you intend to trace will bring you to a form where it is possible to focus tracing efforts on a single domain. Following the example in Figure I-6, specifying host twitter.com and clicking Start will begin a new session for Twitter.
Wireshark will now display all communication occurring between your network card and http://twitter.com. The next step is to further constrain these results by protocol, so that only HTTP requests and responses are shown, to avoid viewing raw TCP traffic that occurs frequently when establishing connections. To accomplish this, Figure I-7 illustrates applying a visual filter, specifying http.request || http.response as a filter expression.
Figure I-6
After the filter expression is enabled using the Apply command, Twitter API traffic is displayed conveniently. Figure I-7 shows the HTTP GET request to Twitter for statuses on a user’s timeline, followed by the HTTP response from Twitter containing special headers and the XML message result. You will learn how to interpret this data in Chapter 1, but for now, you can set up Wireshark so that you’re ready to get into the details at a lower level. Whenever you encounter unexpected behavior, remember that you have an excellent diagnostic tool at your disposal.
Figure I-7
Summary
You set up a development environment to build, run, and modify the examples in this book to build your own Twitter applications in ASP.NET, Silverlight 3, and Windows Azure, as well as monitor and troubleshoot the Twitter API with an HTTP traffic monitoring tool. You used the new Microsoft Web Platform Installer to streamline your configuration experience, installing most key components of your environment in one place. In the first chapter, you’ll begin at the beginning, with HTTP web programming and the services model that embraces it. You’re ready to build your next Twitter application!
Conventions
To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.
Notes, tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.
As for styles in the text:
We highlight new terms and important words when we introduce them.
We show keyboard strokes like this: Ctrl+A.
We show file names, URLs, and code within the text like so: persistence.properties.
We present code in two different ways:
We use a monofont type with no highlighting for most code examples.
We use gray highlighting to emphasize code that is of particularly importance in
the present context.
Source Code
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download at http://www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book.
Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-0-470-53132-7.
Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.
Errata
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.
To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to provide and click Submit.
4. You will receive an e-mail with information describing how to verify your account and complete the joining process.
You can read messages in the forums without joining P2P but in order to post your own messages, you must join.
Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
