Building Facebook Applications with HTML and JavaScript

Building Facebook Applications with HTML and JavaScript

with Ray Villalobos

 


Facebook application development is one of the most lucrative and fast-paced niches of the programming world. This course introduces developers and designers who are already familiar with HTML and JavaScript to Facebook app development. Author Ray Villalobos reviews the different types of Facebook apps and tours the Facebook Developer site, reviewing the requirements to be an apps developer. At the end of the course, Ray shows how to create a complete app, from building the initial containers and pages to adding social plug-ins and game controls.
Topics include:
  • Working with the Facebook API
  • Creating a container for an app
  • Creating the basic app page
  • Loading up the JavaScript SDK and initializing Facebook
  • Checking for registered users
  • Adding JSON data feeds
  • Adding social plug-ins
  • Creating feedback with the Feed, Message, and Request dialogs
  • Creating achievements for the app
  • Debugging the app

show more

author
Ray Villalobos
subject
Developer, Web, Programming Languages, Web Development
software
Facebook
level
Intermediate
duration
3h 0m
released
Jun 14, 2012

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:03Hey there! This is Ray Villalobos, and welcome to Building Facebook Applications with JavaScript.
00:09In this course we'll look at how to build Facebook applications using the JavaScript
00:12APIs and JavaScript SDK.
00:15I'll start by showing you how Facebook applications work and how Facebook stores data through the social graph.
00:20Then I'll show you how to register users and ask them to authorize your application using authorization dialogs.
00:26We'll also see how to use JSON to read data from the Facebook social graph and generate
00:30content for your app using YouTube and WordPress JSON feeds.
00:34Finally, I'll show you how to add social plug-ins, dialogs, and create achievements for your Facebook application.
00:39We'll be covering all these features and lots more, so let's get started with Building Facebook Applications with JavaScript.
Collapse this transcript
Using the exercise files
00:00If you're a premium member of the lynda.com online training library, or if you're watching
00:04this tutorial on a DVD-ROM you have access to the exercise files used throughout this title.
00:09In the exercise files folder you'll find folders for each of the different videos.
00:13If you open up one of those folders you'll see a folder called finished with the project
00:19after I finish the video.
00:21You'll also find a working folder, I'll start each project by copying that working folder onto the Desktop.
00:26Now I'm going to hold down the Option key, while I drag to make a copy onto the Desktop.
00:33Unlike in most projects, we're going to be working directly off the server, so inside
00:37the working folder you should see a _start folder.
00:41That has a copy of the facebook folder as it stood on our server when we began this video.
00:46There is also a codesnippets file and a lot of the times I will already have opened this
00:50file into a text editor.
00:54You might see some additional files that need to be copied onto our server.
00:57So I also have my FTP application running, and I have the server already open, I'll double-click
01:06on the shortcut here to open up more servers, and I'm usually working inside the facebook folder.
01:12We're going to open files directly from our server, so most of the time I'll be clicking
01:16on one of these files and selecting Open by right-clicking and choosing Open.
01:21We're using the Command key on a Mac, it is Command+O.
01:24So open this file, and you will see something like this.
01:28So I'll work with the codesnippets to copy some of the code that we're going to be using
01:31in the video, and I'll be inserting things in different files like, this index file.
01:36If you need help learning how to work with servers, make sure you check out my video
01:40on Managing Hosted Web servers on the lynda.com online training library.
01:44If you're a monthly member or an annual member of lynda.com, you don't have access to the
01:48exercise files, but you can follow along from the Scratch with your own assets.
01:53So let's get ready to build Facebook applications.
Collapse this transcript
1. Understanding App Development
Introducing Facebook applications
00:00Before we get into the nitty-gritty of Facebook app development, let's take a look at what
00:04Facebook applications are and how they work.
00:07Facebook applications are regular web applications that are accessible from within Facebook.
00:12A web application is a set of web technologies that performs some type of function, like a game.
00:18A game Angry Birds, for example, is one of the thousands of web applications you can
00:22add to your Facebook account.
00:24What makes Facebook applications unique is that they're able to integrate with data Facebook
00:28already has on you, to create unique experiences.
00:32Facebook connects lots of data about its users and their friends.
00:35So, for example, you can compare scores with some of your other friends who also have installed
00:39the Angry Birds application, or you can invite friends to do so.
00:44Permissions are really important to Facebook.
00:46In order to add applications you'll need to grant the applications permissions to some
00:50of your social data, who your friends are, perhaps your email address or allow the app
00:55to post things like scores into your Timeline.
00:57Apps in Facebook can be built with a few languages like PHP or JavaScript using different tools
01:03called Software Development Kits or SDKs and target different platforms like the Desktop
01:09or iOS and Android devices.
01:12When you add an app, it will appear as a Bookmark on your home screen, the screen you first
01:17log into when you visit Facebook.
01:19Apps can communicate with users in a variety of ways through requests like when you ask
01:23your friends to join you in playing an app, notifications which is perhaps letting you
01:28know when you have reached the high score or bookmark counts, which appear as red rectangles
01:33with numbers and let users know that there's some action waiting for them in the game.
01:38Apps can also post to a user's feed as long as the user allows them. Spotify, for example,
01:44can automatically post on your FEED when you play a song.
01:47Once an app reaches 10 users, it will also be searchable through the search bar at the
01:51top of Facebook, before then, it's only available through its own Facebook URL.
01:56Apps also get their own special page with the Facebook navigation and search at the
02:01top and a special app sidebar on the right.
02:04This is different than a regular Business or Fan Page, and although you can add one
02:08of those pages as well when you create the app, this is the standard look that the app
02:13has when you launch it.
02:14The part of the app that is not the top Facebook nav or the special sidebar is called the canvas,
02:19it's where the user interacts with the application.
02:22This is a real important concept and one that you'll hear a lot when working with apps or
02:26reading the developer documentation.
02:28The canvas is an iFrame that is a window onto a web site that is not hosted on Facebook,
02:34but on a separate server.
02:36This is really important, the app itself doesn't exist on Facebook, it lives on its own server.
02:41If you're an app developer, you'll have to get your own server, so you can develop and
02:46host your app. When you create your app, the app will be drawn into that special space
02:51on Facebook?s app page called the Canvas.
02:54Facebook applications are very powerful, because you run the application on your own server,
02:59you have a lot of control over what your application does.
03:02Facebook gives you access to the social connections and user information you need to create powerful
03:07applications that leverage the social network.
Collapse this transcript
Understanding the social graph
00:00Normally I'm the type of person that likes to get straight into coding pages, but with
00:04Facebook development, it's really important that you understand a few concepts that are
00:08critical to how Facebook apps work.
00:11In the last video we talked about the canvas, the iFrame or window that Facebook applications live in.
00:17In this video we'll cover something that is probably the most important concept for understanding
00:21how Facebook works with the social graph.
00:24Just take a look, the social graph is a representation of how a person uses the Facebook social network.
00:31The connections they make and the objects they utilize within Facebook.
00:35It's more than just a concept, it's actually how Facebook stores information about its
00:40users on its database.
00:42When working with applications, it's crucial to understand how these relationships are
00:46built, because it impacts the way you access information.
00:49When a user first joins Facebook, they immediately start friending people, describing their likes,
00:55posting content and adding applications and all these actions create a series of relationships
01:01or connections between these objects. So let's talk about those connections.
01:06Posting content, for example is one of the types of connections.
01:09As a user interacts with their version of the social network, they can generate content
01:13by creating posts, uploading photos or albums, posting videos, creating pages, or even building
01:20their own applications.
01:21Things you create become objects and what you do with those objects become connections.
01:27Posting like I said is a type of connection.
01:29If it's all there was to Facebook, it would be a boring place to visit.
01:32Facebook is about more than just posting content.
01:35A lot of the appeal of Facebook is that it lets us interact with content from other users.
01:40By interacting with content from others, we create connections to their content and the
01:45social graph is a representation of all the connections we make within Facebook.
01:48Facebook users can connect to other objects by friending, liking, sharing, playing, poking
01:55and making other types of connections.
01:58As a user connects to content from other users, those connections will be available to them.
02:03For example, once your friend someone, you'll be able to see their Posts, Photos, Likes, et cetera
02:08and make connections to those.
02:10So the important thing to remember about the social graph is that it's a way to describe
02:14the types of connections a person makes creating their own version of a social network.
02:19Facebook applications are different from traditional web applications, because they have access
02:24to the user's social data, otherwise known as the social graph. We have access to the
02:28social graph through something known as Facebook APIs.
Collapse this transcript
Working with the Facebook APIs
00:00Once you understand that an application lives in the Facebook canvas and how Facebook stores
00:05your user data through the social graph, it's time to learn about the APIs that Facebook
00:10provides for creating your apps. So what is an API?
00:14An API is an Application Programmers Interface.
00:18It's a way for developers to access content on certain web sites.
00:22APIs allow you to have access to information about user FEEDS, user data and other types
00:28of information from another company.
00:30All the major players like Google, Facebook and Twitter at APIs for working with their content. Why?
00:37Because APIs allow companies to let others extend the functionality of a web site beyond
00:42what the original developers thought possible. Part of the rise in popularity of Facebook
00:47is due to social games like Farmville, and Mafia Wars. Which were made possible through their APIs.
00:53Facebook provides three different ways for developers to interact with social graph.
00:58The web site API lets you access open graph data, a term used for certain social graph
01:03information you can put on your web sites.
01:06The mobile API lets you integrate Facebook into mobile device applications running on
01:11platforms like iPhone and Android operating systems.
01:14Apps on Facebook, the API we'll be exploring in this course lets you build apps that show
01:19up as pages within Facebook.
01:21Remember, the pages live on a separate server, but show up in the Facebook canvas.
01:26So when you develop apps on Facebook, Facebook lets you interface with the social graph for
01:31something called an SDK or Software Development Kit and SDK is a software that Facebook engineers
01:37have developed and make available to users.
01:40Facebook provides two flavors of the SDK for App development, one in PHP and another in JavaScript.
01:47In this course I am going to show you how to work with the JavaScript SDK.
01:51Most of the time you'll be able to do the same types of things using different SDKs,
01:55but there are some instances that require you to use one or the other.
02:00So remember that when we work with Facebook Apps, we are going to need to work with two things.
02:05First, we will need to work with the App container on Facebook.
02:09This is the canvas on Facebook's App page.
02:12The canvas is an iFrame that is a window to your application.
02:16The iFrame points to web site where you code the app using one of the SDKs.
02:22The series of pages you create on your web site is nothing more than regular HTML with
02:26hooks into Facebook data, using commands that we'll learn about in this course.
02:31So now you have a handle on the terminology, you're ready to start working with the SDKs
02:36to create the Apps.
02:37The process Works like this: first we'll create the container for our App on Facebook.
02:42Then we will create the page on our server unless you want an app that's just plain HTML,
02:48you want to take advantage and learn about the features and the social data that the
02:51Facebook APIs provide.
Collapse this transcript
2. Creating a Basic App
Installing the Facebook Developer App
00:00If this is the first time you are creating an application on Facebook, you will need
00:04to install a special app called the Facebook developer application.
00:08It allows you to manage the applications you are going to install on Facebook.
00:11So let's take a look.
00:12First we will need to sign into Facebook, and we will click on Login.
00:17Now to get to the Facebook would developer's page, we are going to navigate to the Facebook
00:21developer site, which is @ developers.facebook.com.
00:27Now you may want to check around this particular page, it has a lot of really good information,
00:32this is the hub for Facebook developers, so you can check for status updates on the application itself.
00:38Take a look at some of the news and see the information about the different types of APIs.
00:42For example, if you're interesting in adding things to web sites, and click on this link
00:46to go to the Facebook for Websites.
00:50If you click right here, you'll see the Build for Mobile section, which has a lot of information
00:55about adding Facebook data to existing applications and mobile devices.
01:00The one that we will be spending a lot of time with is this Build Apps on Facebook which
01:05is where we will work with the JavaScript or PHP SDK.
01:09In this course we are going to focus on the JavaScript SDK and the manual is right here.
01:14But for right now we are interested in adding the Facebook application to our page.
01:19So we are going to go up to the top of the page and find this Apps link.
01:22I am going to click on that and Facebook will ask me to add the Facebook developer app.
01:29So all I need to do here it is click on this Go to App link, and now I am ready to start creating apps.
01:37Now you are not going to see this if you've already installed other Facebook applications,
01:42so you're only to see that other add the application window one time.
01:46So that's the first step towards building your own apps.
01:49If you haven't already done it make sure you do go through the Facebook developer web site,
01:53it has a lot of great information that will make your apps better.
Collapse this transcript
Creating a container for your application
00:00Once you've got the Developer app installed, it's time to create the container for our application.
00:05This container is what creates the page that holds the infamous Facebook canvas, the iFrame
00:11into which our application will load, so let's check it out.
00:14So, to get to this page where you can create your new app, you want to go to developers.facebook.com/apps.
00:21Once you're here you can click on this button to create a new app.
00:25This is what the page looks like when you haven't created any apps, when you've got
00:28some apps, it's going to look a little bit different.
00:31So, once I hit the Create New App button, I'm going to need to type in an app Name this
00:35is just what our users going to know the app as.
00:38So I'll type in Source Foo here, and it's going to check to make sure that's a valid
00:43and available name, and then I need to type in an App Namespace.
00:47An App Namespace is Facebook's internal name for your app, it's formatting, it's much more
00:52restrictive than the app name, so let's try to typing in source foo, and you'll see that
00:57I'll get a Failed error, it doesn't really explain why until you roll over this little
01:02question mark, and it's going to tell you that the app space can only contain lower
01:06case letters, dashes and underscores, so this space right here is not good.
01:10So I'm going to hit the Delete there, and that name is available.
01:15If you want to, you can click on this, I would like free web hosting provided by Heroku.
01:19However, it's a little bit complicated to set up and the way that I have created my app
01:24is by hosting it on a separate server, which we'll talk about in another movie.
01:29So I'm going to click on Continue, and sometimes you might see a Security Check like this.
01:34These are getting really hard for me to figure out nowadays--I think, maybe that's a word--
01:40so let's try Submit, and that seems to have worked, that's great! Okay, so you may have
01:46to retype the App Namespace, I'm not sure why, but sometimes it doesn't take it.
01:51So sourcefoo, and you want to type in your site domain, so these are the web addresses of your web site.
01:58So whatever your web site address is this is what you would type in here.
02:01I'm going to type in iviewsource.com, hit the Tab key, and I'm going to click back in
02:07here, and I'll also type in the www version just in case.
02:12So iviewsource.com, hit Tab key again. You need to pick a Category for your app, and
02:17that is sometimes going to impact the types of things that you can do with the open graph data.
02:22So, for this one I'm going to choose Games, because although this is not really going
02:26to be a game per se, it's going to use some of the game functionality to add achievements.
02:31I'm going to click on that, it's not any of these types of Games, so I'm just going to choose Other.
02:36And next, we need to specify what types of integration we want to do with this Facebook app.
02:42I'm going to click on Website, and I am going to type in the Website URL here, that should
02:48just be your web site address, with http://ivsource.com.
02:54Make sure you put the trailing slash right there, or it might cause some problems later on.
02:58You also are going to be creating an app on Facebook, so I want to click that on and type
03:03in your canvas URL.
03:04This is the URL to where your Facebook application is on your web site, it's probably going to be in a folder.
03:11So I'm going to type in my web site address/ facebook, and I'm going to put that trailing slash.
03:17I'm also going to need a Secure Canvas URL, which will look something like this, it'll
03:21have an HTTPS, instead of an HTTP, that is something that you're going to have to set up
03:27with your hosting company.
03:28The ability to create a Secure version of your web site is really important, and it's
03:34required by Facebook as of last October.
03:37So, that's really all you need for this site, I want to make sure I typed in everything
03:41correctly, it's really, really important, I going to hit Save Changes, and looks like,
03:48because I was previously creating this App Namespace, it's not going to allow me to do
03:52it, so, if you can narrow like that, it means that I'm going to have type in a different
03:57App Namespace, and I want to just type in an extra O there, and hit Save Change, see if it takes that?
04:05So these two numbers are the app ID, which is just an ID for this particular application
04:10and the other is an app secret, which you'll need whenever you're working with certain
04:15types of functions in JavaScript.
04:17You obviously can't memorize this, but you'll be going back here to get the app secret and the app ID.
04:23So, now that you've set up the container for your app most of the Facebook work is done,
04:28in essence, we've created the page on Facebook and set up the canvass for our application.
04:34Now, you're ready for the second part of the equation, which is setting up your hosting
04:38and the page that you're going to use to feed into the canvas.
Collapse this transcript
Getting and setting up a web host
00:00The second part to the Facebook app equation is setting up a web host.
00:04Let's take a minute to talk about the kind of host you'll need, as well as the process
00:08for creating apps and some of the software you'll use.
00:11There are lots of different types of servers and many plans you can use to host your Facebook applications.
00:17Developers need just a few critical features to create apps on their server.
00:21First, you'll need a server with the ability to run PHP.
00:23PHP is a server site language that allows a server to process certain files before you
00:29send them to a client, even if you're only using the Facebook, JavaScript, API, you'll
00:34need to be able to do certain things with PHP.
00:36Facebook itself is written in PHP and is the largest PHP installation on the planet and
00:42although the JavaScript API is sometimes easier to work with, it's always good to have both options available.
00:48You'll need to set up an FTP account and download an FTP client.
00:51There are a lot of capable FTP applications.
00:55If you're on a Mac, you might want to check out Transmit. It's what I use for most of
00:58my project, and although it's not free, it's a super reliable and very capable FTP client.
01:03If you're on a Mac or a PC and are looking for free application then checkout Cyberduck,
01:09it's a pretty capable FTP client that works on both Macs and PCs.
01:13Also both of these, Transmit and Cyberduck, are available on the Mac App Store.
01:17As of last October, Facebook requires that you install or have an SSL certificate available on your server.
01:24An SSL certificate will allow you to create more secure connections between Facebook and your sites.
01:31Most web host will not provide an SSL certificate by default, so it's an add-on on most beginners or low-cost plans.
01:37It's not a lot of set up involved, other than purchasing the certificate and pointing it to your domain.
01:42So, let me go into the 1&1 Control panel that I use for a lot of sites, and I am going to
01:47go to the Domain section right here, so I am going to click on this, and if I have an
01:52SSL certificate installed, I should see an SSL button here in the navigation.
01:57This is one of the entry-level plans on this particular domain, so it's not available right now.
02:02So if I want to add it, I need to back into the Start menu and click on Order More Features
02:09under Contract Data.
02:11From here you want to go to the Dedicated SSL plan right here, so click on that, it'll
02:16open up a new window.
02:19So from here you want to scroll down a little bit and find the section called SSL certificate,
02:24you add an SSL certificate just by selecting 1 right here and clicking on this button,
02:29and once you go through the process of paying that, you'll be able to go back into your
02:33Control panel and see the button for SSL under your domains.
02:39So back over here, and you just see an additional button called SSL right there and all you
02:44do is click on a domain that you want to add the SSL plan to, and then click on the SSL
02:50and add domain there.
02:51So it's really, really simple to set up.
02:53If you need more information about setting up and working with web hosts or using FTP
02:58applications, make sure you check out my course on Managing a Hosted Web Site on the lynda.com
03:03online training library.
03:04One thing that is different about working with Facebook apps is that they need to be
03:08on a live server that Facebook has access to in order to work.
03:12When we develop normal web sites, we usually work on a local machine, say your laptop or your desktop.
03:17When you're finished, you usually upload the Finished files to your server where people
03:21can access them from the web .
03:23With Facebook, you usually load files from your live server, edit them in a text editor,
03:28and then save them back to your server.
03:30If you've got questions about how to do this, make sure you watch the movie called Creating
03:34a workflow on Managing a Hosted Website it's right here.
03:37To edit the files you can use a number of text editors, I like to use a program on my
03:42Mac called Espresso from MacRabbit, but you can use any text editor at your disposal.
03:48If you're on a Mac, you might want to check out TextWrangler, it's a free and very capable
03:52text editor that is available on the Mac apps store.
03:56As you get deeper into web development, you also might want to check out its cousin BBEdit,
04:00which has better web features.
04:02If you're on a Mac or PC, you might want to check out Aptana Studio, it's a really nice
04:08free open source editor, and I really like its code assist features.
04:12I like another text editor that also works on Macs or PCs called Sublime Text.
04:18It really doesn't matter which text editor you use, as long it is something that you're comfortable with.
04:22Pick one, and make sure you learn its shortcuts and advanced features.
04:26Once you have your server with the right installations and software, you are ready to get started
04:30developing your Facebook applications.
Collapse this transcript
Creating a basic app page
00:00And app on Facebook is nothing more than an HTML page that can have some additional ties
00:05to the Facebook data using one of the Software Development Kits or SDK.
00:09Let's start building our project by setting up our folder structure on our server and
00:13creating a basic page.
00:15So I am at the developer site for Facebook, and I want to click on this Apps link to get to the app.
00:21So this is what your app page looks like once you've created an app, and we have got one right here.
00:26So what I want to do is show you that there's a couple of important URLs right here.
00:31We created this page, we created this canvas URL.
00:33This is where the Facebook is going to look for the page that's going to be on our web site.
00:37So I need to create this folder on my Web server.
00:40The other import URL is the Canvas Page URL.
00:43So this is how users will be able to get to our page from within Facebook.
00:48Once you have 10 people liking your app, your app will be searchable through the search
00:52bar at the top of Facebook.
00:54But before we get ten people, we will be able to access our page by going to this URL.
00:58So I am going to go and minimize this, and I have a codesnippets.txt file already open, where
01:04I am going to grab some of the code that I am going to use for this particular project.
01:08And I have an FTP application running, so I am going to double-click to login to View Source.
01:13And this is just a standard WordPress installation, you can see all the wp files right there.
01:18And what I want to do is create a new folder, so I am in Transmit right now, I'm going to
01:23right-click and select New Folder, and this New Folder is going to be called Facebook,
01:28because that's what we told the Facebook App Page that we wanted to call our folder.
01:33So there it is I am going to open that up, and I need to create a few additional folders,
01:38so I am going to right-click and create New Folder.
01:40And I am going to make in images folder, I will also made an _ folder, I like to have
01:46all my JavaScript and CSS files organized at the very top of the page, so I like to
01:51create this folder to make sure that it goes on top of everything else.
01:55And in there I am going to create another folder and call it CSS.
02:00And another folder called js for my JavaScript files.
02:03I am going to go ahead and create the JavaScript and a CSS file, so I will double-click on
02:07this, and I will create a New file in there. And this file is going to be called mystyle.css.
02:16It's where we are going to be putting our styles in the future.
02:18So I am going to back-up a folder, on the Mac I can hit Command+Up, I am using Transmit.
02:25And I am going to double-click on the JavaScript folder and make a new file in there, this
02:31file is going to be called myscript.js, and that's going to hold our JavaScript.
02:37So I am going to backup a couple of levels, back to the Facebook folder, and I am going
02:42to go to the Desktop my working folder.
02:45In my working folder I have a file called vs_monogram.png, and I want to drop that into
02:50the images folder, because we are going to be needing that later.
02:52So I want to drag that in there, let it go, it's going to copy.
02:56And the last file we are going to need is an index file.
02:59Now normally I would name this index.html, but when working with Facebook, it's actually
03:05better to name them index.php.
03:08So even though we are just working with JavaScript SDK, there are some instances where we are
03:12going to need the power of the PHP SDK.
03:14So I am going to place the PHP extension there just in case that I'm going to need it for
03:20later, which I know I will. So I am going to just Enter to accept that.
03:24I am going to open this file with BBEdit, so I am right-clicking.
03:28I have set this Transmit up to actually automatic open this, so I could just double-click on these.
03:34And there is the empty index.php file.
03:36So I am going to switch over to codesnippets, and I am going to grab a very basic HTML page.
03:42You can see that this is just a simple HTML structure with a title, and I have a div for
03:48the content right there.
03:49So I am going to hit Copy and go back into index.php, paste our page, when I save this
03:55you will notice that it will upload to the server, you can tell, because there is a little
03:59bar that appears right here for a fraction of a second.
04:01Okay, next I want to go back to my codesnippets. php file, and I want to load up some Google fonts,
04:07because I am going to use some custom fonts for this particular app.
04:11So to do that what I did is go to a site called Google webfonts which is at google.com/webfonts.
04:19And find some fonts that I want to use within my project.
04:21I'm using a couple of fonts, one of them is called exo.
04:26So once you find the font that you want, all you have to is hit this Add to Collection button.
04:32And I also use another font called average, and I am going to add that to the collection.
04:38Once I'm ready to use the fonts, all I need to do is click on this Use button.
04:43Load up whichever weights I want, and then copy this URL right here.
04:48Now because I'm working in Facebook and Facebook wants all pages to be secure pages, I am actually
04:53going to have to modify this URL to be an https URL.
04:58Google is not going to mind, because their pages are securely served, and it will prevent
05:03those from getting an error in Facebook.
05:05So I am going to just close that out, or you have the line of code here with the two fonts that I'm using.
05:09So I am going to copy that, go back into index.php and right underneath the title, I will add that right there.
05:17So this is a link to my Google fonts. Next, I am going to go back into codesnippets.
05:22And I need to load the stylesheet that is in the _ folder and the CSS folder, and it's called mystyle.css.
05:29So I am going to copy that line of code, and I'll paste that right there.
05:34So back into codesnippets, the next thing I need is to download and load jQuery.
05:39Now here's a line that is linking to a local copy of jQuery, but you need to download it
05:43from the jQuery server.
05:44So I am going to go back into Safari, and I need to go to the jQuery web site, which is at jquery.com.
05:51And I need to download the latest production version.
05:53It happens to be 1.7.2, so I am going to hit Download.
05:59If you get something like this it means that it didn't actually download the file, it just
06:03loaded it up on your browser.
06:04All you got to do is just go the File menu and select Save As, and I am going to make
06:10sure that is set to Page Source, not Web Archive, and I do want to add the .js extension to that.
06:16So I am going to save it on the Desktop for easy access, hit the Save button, I am going to minimize this.
06:23And now I have a copy of jQuery 1.7.2, which has minified.
06:27So I am going to open this up right here, open up my JavaScript folder and copy that
06:33file into the JavaScript folder.
06:36So I will delete this one, because I don't need anymore.
06:40On a Mac I just hit Command +Delete to delete a file that is anywhere on the file structure.
06:46So back into my codesnippets, I want to get the jQuery file, and paste it right there.
06:54And of course, the next thing would be to actually load of My Scripts, which is in this
06:58file called myscript.js.
07:00So I am going to grab the next piece of code, this is just a link to myscript.js, back over
07:07here, paste that in, Save that, it's updating right here.
07:13Back to codesnippets, and I want to grab some very basic HTML code, this is nothing too
07:18exciting, I am going to grab this right here, I will copy it and paste it into the index.php file.
07:23So I am going to put this in the Content section, because it's on content right now just to
07:28make sure that our app is working correctly.
07:31So I am going to hit Save, and now that I've created this page, this page exists on iviewsource.com.
07:37So I am going to open up a browser, and I can close out of jQuery if I want to.
07:42I will make a new window here or a new tab, and I am going to go to http://iviewsource.com/
07:51and the location of my Facebook folder.
07:53If I hit Return, it should show me the view source app, and it looks a very, very simple
07:59HTML page, so there is really nothing to this.
08:02Cool thing is that if you remember, when we were in the Facebook Developer section of
08:07our app, it told us that there's also a Canvas URL for this application.
08:12So if you look at this right here that's the Canvas URL, so I am going to copy that, switch
08:17back to my tab and just paste that.
08:19This is how people can get to my application right now.
08:22And if you look at it, it looks exactly like a Facebook application with the search bar
08:27and the custom menu at the top and an additional sidebar on the right-hand side.
08:33So we have created an actual Facebook application by just creating a very basic HTML page.
08:39And this is a fully functional application that people can get to right now on Facebook.
08:44It's not very exciting, because it doesn't hook up to the user data, we will talk about
08:49how to do that later.
Collapse this transcript
3. Registering Users with the JavaScript API
Loading up the JavaScript SDK and initializing Facebook
00:00So right now our app is technically a Facebook App, but with no access to the social graph data.
00:06This is not necessarily a bad thing, you can create a fully functional app that doesn't
00:10require any kind of authentication and is visible to everyone on Facebook.
00:15But the beauty of Facebook is that you can have access to this data.
00:18In this movie I am going to show you how to initialize and load the JavaScript SDK.
00:22So let's go to the developer's page on Facebook at developers.facebook.com.
00:28And I am going to click on Build Apps on Facebook.
00:31And then I'm open up the SDK Reference just right there.
00:34I need to pick my SDK which in this case is the JavaScript SDK.
00:39I will click on that, and you will see that it gives you a lot of information about the
00:44core methods and other functions that you could use when you have the JavaScript SDK loaded.
00:50Now the first thing that you will need to do on any app is to make sure you initialize
00:53the SDK, and this is how Facebook wants you to do it.
00:57So to initialize the Facebook page we generally place this code after the opening body tag,
01:02but we have a slightly different structure in our project, I don't really want everything to be on one page.
01:08So the way that I've organized my application is by creating a JavaScript and CSS folder
01:14inside and _ folder.
01:17So we are going to need to change things a little bit.
01:19I already have my codesnippets.txt file.
01:22And one of things that we need to do is open up myscript.js file on our server. So I will do that.
01:28And we will go back into codesnippets, and we will grab our version of these two functions.
01:34So, grab all those right there, go back into myscript.js and paste them.
01:40Now I will go ahead and save.
01:41And I will show you what's going on here.
01:43This is the part of this code that loads the JavaScript SDK asynchronously.
01:49That really big word just means that it's loading it in the background as the rest of
01:53the page is loading, whenever it's ready it's going to fire up an additional function called
01:58window.fbAsyncInit.
02:00So once the function finishes loading the JavaScript SDK, it's going to execute this
02:05other function, and this other function is going to try to initialize the SDK, it's going
02:10to need a couple of things your AppID as well as your domain.com here for a channel file.
02:17So let's get the AppID, and I'll talk about the channel URL later.
02:20So you can get your AppID by going back to the developers page, and then clicking on
02:25this Apps link on the top right.
02:27When we get here, since this is the only app we have, it's the one that's showing up right now.
02:30Now you can just click on this link right here, and it will show the app that you want.
02:35Now we need this right here, which is our App ID/API key.
02:39So I am going to go ahead and copy that to the clipboard, and then I am going to go back
02:44into BBEdit, my code editor and make sure that I replace this App ID with the App ID from that page.
02:51So I am going to hit Save.
02:53There is one more thing that we need.
02:54If we go back into the SDK--let's go back by hitting the Back button a couple of times
03:00into the Reference JavaScript.
03:02Notice that there is an additional little line that I didn't copy, and this is a special
03:06div that should appear on any page that has Facebook data called fb-root.
03:11So we'll need to add that into our HTML.
03:13I am going to go back into my Text Editor and back in to Transmit, because I need to
03:19open my index.php file.
03:21So I will open that up, go into my codesnippets, and I will grab this fb-root div.
03:29Now this div is what Facebook uses when it needs to draw something onto the screen.
03:33So we need to make sure it's somewhere on the page.
03:36So I will just add it right next to the body tag, doesn't really matter where it is as
03:40long as it's somewhere.
03:41Looks like these aren't tabbed exactly, so let me go ahead and tab these.
03:46Okay, that looks a little bit better.
03:49So I am going to save that, and we have got most of the things that we need ready to go.
03:55There's another thing that you need to know about, and that is this thing called the Channel URL file.
04:00So if we go back into Facebook and read the initializing documentation, so back to the
04:05JavaScript SDK, we will see that there's a section here that talks about the channel file.
04:10This channel file is a file that addresses some problems that used to happen with cross
04:14domain communications in some browsers.
04:16It used to happen to me a lot.
04:18So this is really cool, I am glad they added this.
04:20And what you need to do is essentially create an HTML file with this single line.
04:25If you read a little bit further, you'll notice that they recommend that this file be cached
04:28properly and they give you a little bit of code right here, and that's what we are going to use.
04:32So I am going to go back into my Text Editor and grab this next piece of code, this is
04:37just a code from Facebook, and I am going to copy it to my clipboard, and I am going
04:42to need to create a new file.
04:44Now remember that they suggested that you call the file index.html, you're actually
04:50going to create a file called index.php. Why?
04:53Because we added a little bit of ph code at the beginning of the file, I will call this
04:58channel.php, and I'm open that up, and I'll paste the code in there.
05:04Because we have added a bit of PHP at the beginning of this page, we need to make sure
05:10that our extension is .php.
05:12Some servers will not process PHP files unless they are named .php.
05:16So I am going to save that, and I am going to go back into myscript file, and make sure
05:24they put in the right location of our channel file, which will be our domain, which is iviewsource.com/channel.php.
05:35And I'll save this file.
05:36Now there is one more thing that I want to do, this is sort of optional, but it's something
05:41I like very much, and this is adding a frictionless request option to our initialization code.
05:47So I am going to grab this, and I am going to copy it and go back into myscript file
05:53and just put this somewhere in here.
05:55I'll just add it right there and format it a little bit better.
06:00So let's go into Safari and take a look at frictionless request.
06:06It's the part of dialogs, when you go here if you do a search for frictionless requests,
06:11I will just do it a couple of times, right about in the middle of the page, there is
06:15a good explanation of why these are important.
06:18Many times when a user is within an app, they can make requests to their friends for certain
06:24things, so you could say, hey Joe! I want to play game with you, or check out my high
06:28score and frictionless requests allow you to send additional requests, once you've sent
06:35a user a request already without having to see another dialog box.
06:40So it kind of stream slice the process of sharing with friends by not making you have
06:44to see a box every time you send something to somebody.
06:48And that definitely makes things a little more efficient.
06:51So let's go back into our app and just reload, everything should be fine, nothing is really
06:59happening differently right now, but we have with these lines of code loaded the JavaScript API.
07:06Once you loaded the JavaScript API, you can check and see whether or not a user has logged
07:11into your app, and we will do that next.
Collapse this transcript
Checking for registered users
00:00Before we access social graph data, we can check on the user's login status.
00:05We can find out if a user is logged in to Facebook, and if they have authorized our app.
00:10We do that with the getLoginStatus method of the API.
00:14So let's take a look.
00:15When you use a Facebook API, you will be working with different types of methods.
00:19The ones that you are going to use more often are the core methods which are right here.
00:24They are FB.init, which handles the loading of the Facebook API, we've already used that one.
00:30The FB.api, which handles access to the social data or social graph.
00:34Finally, there's the FB.ui, which lets you access parts of the Facebook user interface like dialogs.
00:42The next section is what we are interested in, the Authorization Methods.
00:44To check to see if a user is logged in, we use the FB.getLoginStatus method.
00:50So let's click on that.
00:53The example right here gives you three options, and it's really nicely commented so you can
00:57read that if you want to, but essentially, we want to find out if the user is logged
01:01in to Facebook and has authorized our app.
01:04If they are logged in to Facebook, but haven't authorized our app, we won't be able to do something then.
01:09Finally, if the user is not even logged in to Facebook, we also want to know about that.
01:14Now remember that users can access your app outside of Facebook, because frankly, it's
01:19just a simple web page.
01:21So you need to figure out what your authorization strategy is going to be.
01:24Do you want users to go to your web site and see that page outside of Facebook or do you
01:30want them to only have access within Facebook, and that's what this function is about.
01:35So let's take a look at how we would implement that.
01:37First, I am going to go to BBEdit and pull up Transmit right here, and I have some versions
01:44of the getLoginStatus method.
01:46So what I am going to do is I am going to open up my JavaScript file over here and just
01:51double-click on that.
01:53So if you remember, we loaded the JavaScript SDK Asynchronously here, and once this SDK
02:00is loaded, then it's going to execute this fbAsyncInit function, and we wanted to make
02:05sure that we initialize the JavaScript API here, and we can add some additional stuff right here.
02:12This is still part of the function.
02:14So this is going to happen only after the SDK has successfully loaded.
02:18So we are going to go back into codesnippets and grab the first version of our function.
02:23So here we are going to check to see if a user is logged into Facebook and authorized our app.
02:29So I am just going to grab that, go back here and paste that code right there.
02:35We're finding out if the user is actually logged in, so this getLogin function gets
02:41a response from the Facebook API that tells us if a user is connected.
02:46If the user is connected, then it gives us a user ID as well as an accessToken.
02:51An accessToken is a security measure that allows us to communicate with certain parts
02:56of the Facebook API.
02:57Unfortunately, we haven't really discussed how to allow a user to have access to our app.
03:02So this is not going to do anything.
03:04So if I save this, go back into my browser and take a look at the app, this is going
03:11to load, but it's really not going to do anything, because we haven't added this app to this user.
03:17So let me go back into BBEdit, and I am going to get the next version of the code which
03:23lets us do something if the user is logged in to Facebook, but hasn't authorized the app.
03:29So if the response status is not authorized, the user is obviously in Facebook, because
03:33it's getting some sort of response, but hasn't authorized our app.
03:36So I am going to copy that and click right here and replace this function, now I am going
03:43to delete this part right here and give that some space.
03:47So if the user is not authorized, but is in Facebook, we could do something.
03:52So let's test that out by adding a dialog box.
03:54Let me just do an alert here, and I'll just put in "User is in Facebook. But not your app".
04:06So I'll save that, and I am going to go back into Facebook and reload this app, and we
04:12get this dialog box, because the user is in Facebook, but hasn't given access to our app.
04:17I am going to click OK.
04:18Now sometimes you may want to debug something without producing an alert.
04:22So for that you can use something called console .log, and treat it just like an alert, save it,
04:29and I'll go back into Safari, and I'll refresh.
04:32Now it's not going to give us the dialog box, but if we go to the Develop menu, and select
04:39Show Web Inspector, and then click on Console, we'll see that it gives us a message.
04:44Sometimes we want be able to push silent messages onto this Console, and that's a really, really
04:48good way of doing it, and it's a great way of debugging.
04:51If you don't have the Develop menu, make sure you go to the Safari menu > Preferences and
04:57under the Advanced tab turn on Show Develop menu in menu bar.
05:00This is in different places in different browsers, but just about every browser has some sort
05:05of developer tools.
05:08So let's go back into BBEdit and into our codesnippets and grab the last version of our function here.
05:14So it looks like I have a couple of the duplicated lines.
05:16So let me just get rid of this one.
05:18So here I find out, if the user is logged in, if they are logged in, then I can do something right here.
05:23This is if they're logged in and authorized our app.
05:26Next, if they're logged into Facebook, but not our app, then we could do some thing else
05:29here, which we've done in our sample app.
05:32So if the user is not logged into Facebook, but has access our app through our web site,
05:37then we could do some thing.
05:38And one of the things that we could do is just redirect a user to log into Facebook.
05:42So you don't necessarily have to do this, but if you want to make sure that users are
05:47logged in to Facebook before using your app, you want to maybe send them back to log in
05:52to Facebook, by changing the window location of the browser.
05:57So let's see how that would work.
05:58I am going to grab this, go back into my code here, and I'll replace this, and so now I
06:10am going to refresh this page, I am going to go ahead and close the Console here.
06:14You are not going to see anything, because I'm obviously logged into Facebook.
06:20So let's go to another browser, and I have another user right here.
06:24Let's try going to our app right now and see what happens.
06:27So you see this user is logged in and he's got into the app just fine.
06:31So now I am going to go over here and just log him out, and I am going to try to go to the app again.
06:39So it goes to the app for just a second, and then it redirects the user to the Facebook Login page.
06:45Now if somebody tries to access your app just on your server, it should do the exact same thing.
06:53So you could see there that it also kicked you back to the Facebook page because the
06:56only way to see that page or that app is by logging into Facebook first.
07:02So like I said, you really have to decide what your authorization strategy is going to be.
07:06Do you want people to access the page that is on your server, whether or not they're
07:11logged in, you can give special privileges or show different things to people that are
07:15both logged in to Facebook and your app, and it's just something that you have to decide yourself?
Collapse this transcript
Using the OAuth dialog
00:00After we check the registration status of a user, we can ask them to log into our application.
00:05Authorization can happen in a few ways.
00:08We can choose to make our application visible to any user, regardless of whether they're
00:11logged into Facebook or not, and this is what we've already done in a movie Creating a Basic App Page.
00:17We can also choose to show a normal version of our app to some users and a more enhanced
00:21version to users who are logged in.
00:23Finally, we can require anyone who comes into page to log into Facebook.
00:28Let's take a look at some simple authentication with the FB.login method.
00:32So if you go to the JavaScript Developer section of Facebook developers, you can see a function
00:38down here if you scroll down a bit called FB.login, and this is the one that we are
00:42interested in adding to our application.
00:45It's a pretty simple function, you call it, and it prompts the user to authenticate your
00:48app using a dialog.
00:50I have a slightly simpler version of this function.
00:52And I am going to switch over BBEdit, also open up Transmit, and I want to open my JavaScript file right here.
01:00I will go into codesnippets, and I am going to grab a super simple version of that function,
01:06it just says login a user.
01:09Go back into myscript, and I want to put it right here where if a user is not authorized
01:14to user application, I am going to ask the user to authorize it with FB.login call.
01:21So I'll save that, and I am going to go back into my application Safari, and I will refresh.
01:31If nothing happened, it's because Safari is blocking pop-up windows, and that's one of
01:35the problems with using the function like this.
01:37It causes the browser to launch a pop-up window, and because of years of pop-up misuse, most
01:43of the browses have learned to block them.
01:45So if you use this method, people might not see the dialog come up at all, for that reason,
01:50FB.login should be called as result of a click.
01:52We will do that in a minute.
01:54First I am going to show you that we will undo Block Pop-Up Windows, and I will refresh,
01:59and we should see the Authorization dialog come up, there it is.
02:03Now the other problem with this method is that it will only retrieve the basic information from the user.
02:10So it shows things like their ID, name picture and other things depending on their Privacy Settings.
02:17Basic permissions are not too exciting, so we can add permissions to our call by adding
02:21them to our FB.login in the Scope variable.
02:24I am going to hit Cancel here, go back into BBEdit.
02:29And I'll grab a different version of the FB.login function like this.
02:34You can see that this uses a JSON like object to add the Scope variable, where I can type
02:39in a number of different permissions I want.
02:42So I will copy that back into myscript, I will replace this one, that one and indent it a little bit. Let's get rid of that.
02:51I will save it, go back into my app.
02:55I will refresh the page and wait for it. There it is.
03:01And now you can see that it's asking, not only for my basic info, but my email address as well as my likes.
03:06Now, you might be asking yourself, where can I find all the different permissions that I can ask for?
03:11I am going to hit Cancel here.
03:14And I am going to into the Permissions Reference.
03:19So at this URL you can find all the different kinds of permissions that you can ask for
03:23in your application.
03:25You might be tempted to ask for all permissions and although you can do that, people are vary
03:29of giving certain types of information, and you should know that the more things you ask
03:34for, the less likely people are to install your application.
03:37Asking for certain things like email addresses will dramatically lower your chances of people
03:42installing your app.
03:43So we need to deal with the pop-up problem as I mentioned, because a lot of browsers
03:47block pop-ups, you should make the FB. login call when a user clicks on a link.
03:53This way you can also choose to show a version of the page to users who are logged in and
03:57a different version to users who are not.
03:59So let's do that. We are going to go back into BBEdit and also pull up Transmit.
04:04So I am going need to open a few other files.
04:06I am going to need the index. php as well as the stylesheet.
04:10I already have the script open, so I am going to open those two.
04:17And the first thing I need to do is make sure I replace the monogram with some content,
04:22so I will grab this, monogram was just a picture that was at the top of the application, which is right there.
04:29And I will paste this over here.
04:31So what we're doing here is we are creating two different sections, one that people will
04:36see if they're logged off and one that people will see if they're logged in.
04:41So if a user is logged off, they will get this welcome message, and it has a link they
04:45can click on to a function that we are going to create a bit later.
04:48I am going to save that, the next thing I need is to make sure that not everything is
04:53showing up when somebody loads the page, but just the parts that I want.
04:57So back into codesnippets, and I want to grab these styles.
05:01So by default the logged off section is going to be on or displayed and the logged in section
05:08is going to not be displayed.
05:09We are going to assume that people are logged off, when they come to our applications.
05:13So I will copy that, go in to mystyle.css, save it.
05:18Okay, so we've got that, the next thing we need is the actual gologin function that we
05:24are calling when somebody clicks.
05:25So I am going to grab this right here and copy it, go back into my script.
05:30I am going to take out this login function because I don't want to automatically pop-up
05:34the window anyways.
05:36What I want to do is just put this function outside of the AsyncInit.
05:42So this is the function that executes as soon as the SDK has been loaded.
05:47So I needed to just be a regular function, it's going to go outside that.
05:50Let me delete that, and you probably want to comment this a little bit better.
05:56When function start looking like this, it's a little bit confusing as to what goes with what.
06:00So I am going to type in a comment here, put fbAsyncInit right there and make sure I put
06:09in this goes with getLogInStatus, so I will add that comment, and this one is response.status.
06:19I want to make sure I comment it so, //.
06:22So now it's a little bit better, because I can read what's what, and that's a good practice
06:26when you're coding some of these pages.
06:28So as soon as somebody clicks on this, it's going to call the FB.login function and also
06:34turn on the logged in section or the logged in class, and turn off the logged off class.
06:40So people will see the proper things, it's still asking for the same permissions.
06:44So let's save that, and let's go back into Safari, back into our app, and we will reload this.
06:51We could see the logged off version of the application right now.
06:56In order to login, we will have to click on Log in.
06:59And here's the same Permissions, we will hit Play Game.
07:03Now that we've authorized our app, we could see the app with the logged in stuff showing.
07:08And that's pretty cool, but there's a problem with this app, let me go ahead and refresh
07:12the page to show you what's happening.
07:15So if I refresh the page, even though I'm logged in, the application things, I'm not
07:20logged in or at least it's showing me the things that I should see when I'm logged off.
07:24Let's switch back to BBEdit, that's because when it executes a reload of the page, it
07:30goes through all the stuff right here, and it notices that we are connected, creates
07:37the user ID and the access token, but let it never gets to the part right here where
07:42I am hiding the different elements.
07:45So I need to make sure that I copy that, and I paste it right here to make sure that I
07:50turn things on and off properly.
07:54And save that, that's also in the codesnippets at the very end.
07:57So actually this is wrong right here, let me paste that there, save it.
08:02And when I come over here, and if I refresh the page, I should see the logged in version.
08:08You are going to see the logged off version for just a little bit, you could fix that
08:12by messing around with the CSS files and controlling what people see when they log in and what
08:17people see when they're logged off.
08:20So this way we have got a pretty sophisticated login system, that makes your content available
08:24to users who are logged in or out of your app, and you can choose to show different
08:29things depending on a user status.
Collapse this transcript
Exploring Canvas app authorization and data output
00:00We have talked about ways of authorizing users for different scenarios.
00:04In this movie I am going to show you how to force a user to be registered in order to see our app.
00:08We will also take a look at the response we get back from our server and see how we can
00:13output it to the browser.
00:14So I am going to go over to the JavaScript SDK, and then I am going to click on Core Concepts.
00:20From here I am going to go over to the Authentication section and take a look at some of the options
00:24that we have right here.
00:25As you could see there are many different ways of authenticating an app, depending on
00:30where it's coming from.
00:31So you can do authentication for native Android apps, iOS apps, as a tab, which is really
00:37deprecated, and it probably won't be around that much longer, but we can also do authentication
00:42with a canvas page which is what we've been doing.
00:44So I am going to click on that, and you'll see that even on this page there is a lot
00:49of different options on the process that you can do to authenticate a user.
00:54I want to modify my page so that it just becomes a canvas app.
00:58If a user comes to the page they will be required to log in to Facebook and your app.
01:02To do that I am going to redirect the page to the Authentication dialog if the user is not logged in.
01:08Once the user logs in, they'll be Redirected back.
01:11This happens to be option 2a on this page.
01:14So this is just a bit of code, I am going to switch over to BBEdit, make sure I have
01:18Transmit, because I am going to need to open some of these files.
01:21So the first piece of code that we need is right here, I am going to open up the JavaScript
01:26file, as well as the CSS file and the Index file. So opens those up.
01:34Okay, and the first thing we need is to edit the JavaScript, so we'll go over here, and
01:42I want to take off some of these things that we did in the previous movie.
01:45So after the access token we are going to delete these two lines, and we are going to
01:50delete the goLogIn function here.
01:53And we are going to paste this code right here.
01:56So if the user is not authorized, then they will be taken to the Authentication dialog
02:02box which you can get to by taking the to this URL right here.
02:08And then you can specify a few additional parameters.
02:10So you are going to need your client ID to do that, we are going to go have to go back
02:14in to our application, so I need to come over here and click on this Apps tab.
02:20I am going to hold down Command while I click on this Apps, so that the app opens up in a new tab.
02:25So here it is I need this AppID/APIKey so I am going to copy that, switch back over
02:31BBEdit and make sure that I paste that here.
02:35This is our client or our App ID.
02:37So after the page finishes registering a user, it's going to come back to this URL, which
02:43is the URL of the page on Facebook.
02:45Under the Scope variable we are going to add all the different permissions that we want
02:48from the user, so I have quite a few right here.
02:52Finally, after we construct this URL, we just set the location of the window to the oauth_url, which is that.
03:00So I am going to save this, and I also need to edit the CSS file, we are no longer going
03:04to need these, so I will just delete that, save that.
03:07I need to go into my codesnippets and get a new version of our HTML, this is just going
03:12to be almost back to the original and a slightly simpler version.
03:18So I am going to take all of the stuff inside the content tag and just paste this new version,
03:23which actually happens to be the old code that we had before.
03:26I think we are done. Let's check out our app. I am going to come back here, I am going to
03:33refresh, and I'm probably already logged into this app.
03:36So you can see that this page is a little bit different than it used to be before.
03:40If you ever want to make sure that you see the Authentication page again after we have
03:44already logged into one of these examples, you want to go back to your page, and then
03:48find the Source Foo app here and just click on this Edit button, and then remove the app.
03:53This will delete the app, so that's no longer available to you.
03:57So once we do that, then we can go back to the App page, I think it's the Back button here.
04:02So I'll hit Cancel, when you try to go to that page, it's going to send you to this
04:06Authentication dialog page.
04:08And here you can hit this Play Game button.
04:14And so we verify that the changes that we have done, force the user to authenticate,
04:19and then take the user back to the original Facebook page.
04:22So now that we've done this, let's take a look at some of data that we get back from Facebook,
04:26because this is where the cool stuff happens.
04:29Once the user is logged in, I am going to post the data into the console, so we can peek at it.
04:33So let me go back into BBEdit and back into my codesnippets.
04:38So what I want to do is copy this FB.api method, and then get the data back from that function
04:45and just output it to the console.
04:47So I am going to copy this and go back into myscript file.
04:53And I want to do this after the user is logged in, and we get the response here in the accessToken.
05:02So right here I want to paste that here.
05:06And what we're doing is calling this FB.api function, that is one of the core functions
05:13for the JavaScript API, we can look up that page and the Developer tools, and that function
05:19returns this info, and that info can be output to the console.
05:24This is only going to work with browsers that let you preview the console.
05:28So make sure you have something like Chrome or Safari installed in your machine.
05:32I think Firefox might do it as well.
05:33Okay, I will hit Save, and I am going to come back into my App just refresh the page, it's
05:40not really going to do anything, but if we go to the Develop menu, and we select Show
05:45Web Inspector, what you want to do is you may be showing the Elements tab or the Resources
05:52tab depending on what you've been doing with your application.
05:55What you want to do is go to this Console tab, when you go the Console tab you are going
05:59to see this object right here, when you click on that, you are going to be able to see all
06:02the user data that you get from Facebook.
06:05So this is everything that you ask for, including Birthday, Education, the Email address, first
06:11name of the user, their gender etcetera, etcetera.
06:14you can see that some of these are objects or arrays so this is an array right here.
06:19And if I open that up I can see that there's another object here, and this object talks
06:25a little bit about the user's school information.
06:28So this is actually in a format called JSON, I'll be talking a lot about that format in another movie.
06:35So if you don't know how to work with JSON data, that's probably going to be a good thing for you to watch.
06:41So let's take a look at some of these other stuff, sports, I am going to click that Open,
06:46open this up, you can see they are into Cycling and Triathlons and Running, that's pretty cool.
06:54And the fact that we have access to this data is pretty awesome because now we can build
06:58apps that do a lot.
06:59So let's see how we could do that.
07:01I am going to go back into BBEdit and instead of just printing out everything to the Console,
07:06I am going to go back into my codesnippets.
07:08I am going to output something to the div called welcome.
07:13So if you look at the index.php document, one of the things that we added is this new div right here.
07:19So I want to print something right there.
07:21And what we want to print will go right here.
07:26So we will find in the element welcome and make the inside HTML, we enter HTML.
07:31It's "Hello there", and we'll get the info from the FB .api function and output the first name of the user.
07:40So I am going to save that, go back into my app, if I go right here, then you can just
07:47make that a little smaller, if it bothers you, you just close it, we don't really need
07:51it right now, so we will reload.
07:54And after a little bit you'll see that their name comes up Hello there Stuart, that's something
07:59that you got from the social graph of this user.
08:02So let's go back and take a look at a couple of things that we could do here.
08:06One of the things we did when we created this structure for our app is load up jQuery.
08:11So I really like the way jQuery gives you additional functions and additional capabilities
08:16when working with browsers.
08:18So I like to use that instead of straight JavaScript, so this is the jQuery version
08:22that does the same thing.
08:24I want to go back into my JavaScript file and just replace this with our new jQuery version.
08:31It does the same thing, it's just done with jQuery, instead of regular JavaScript.
08:37So anything that you want can be output by just putting it right there.
08:41Let's try outputting something else.
08:43Let's go back over here to Safari just take a look at the object data, and let's try outputting
08:49maybe the birthday.
08:50So we will switch back to BBEdit, and we'll type in here birthday, save it, back to our
08:57browser, refresh, and let's see if the birthday comes up.
09:00And there you go you have got the birthday.
09:02So anything from this object can be accessed since we have permission now.
09:07So you could do quotes, you could do one of the sports, time zone, anything that's on
09:13the user data, you can go ahead and output.
09:16So there is his last name, I guess a link to their profile etcetera, etcetera.
09:22This is pretty cool, there is a name right here that you can use to output the entire name.
09:26So that's pretty awesome, that's access to the data. Let's take a look at some other stuff.
09:30I am going to go back into my codesnippets, work on the API we've been using this uid
09:37that's in our script.
09:39You could use that if you want to.
09:40Notice right here, this uid is something that you get from the response that comes back
09:46from the login status.
09:47Now, you don't have to use uid, you can actually use something called me.
09:52And the way that you do that is you just put in quotes "/me" save it, and I want to come
10:00back here, refresh, and just show you that it's the same thing, it's going to work in
10:05the same way, but instead of using the user ID, because you're already logged in, you
10:11can use this little variable called me, which it's not really that big of a deal, but it
10:15seems a little slicker.
10:18So instead of this uid, you can come in and replace it with /me.
10:23Once you have access to the social data, otherwise known as the social graph of the user, the
10:29possibilities of what you can do with data are endless, you do have to understand a bit
10:33of how a JSON works, but I will be showing you that on the next chapter.
10:38In the meantime, get familiar with the user information you can gather from the social
10:41graph, because it is truly awesome.
Collapse this transcript
4. Adding JSON Data Feeds
Introducing JSON
00:00When working with the Facebook JavaScript API, you will need to understand how to access
00:04user data in the Open Graph, that data is stored in a format called JSON.
00:09Let's tack a look at what JSON is.
00:11So JSON stands for JavaScript Object Notation, it's a way to describe data so that JavaScript
00:17can easily interpret it.
00:19If you're used to RSS or XML feeds, you will find that JSON is much easier to parse, but
00:24it can be a little bit harder to read, especially when minified.
00:28JSON data is nothing more than text in a specific format.
00:31And when it all comes bunched together, it's a little bit tough to see.
00:35Lots of different companies like Facebook and Google provide access to their data in JSON format.
00:40And as I mentioned, the JavaScript SDK relies heavily on JSON.
00:44The most basic type of JSON data is a JSON object.
00:48It's simply text wrapped in curly braces, inside they have a string usually a variable
00:54name and a value separated by a colon.
00:57You can specify more than one element in the object by separating them with commas.
01:02So straight on the left is usually a variable name, but it's pretty much like a standard C string.
01:07If you need to escape a character, you can use the back slash.
01:10The value on the right of the object can be lots of different things that includes nesting
01:15objects inside other objects, or even an array, which in turn can also have other objects.
01:21Here's an example of a JSON data file.
01:23You can see an object with a string called users.
01:26Strings can and sometimes need to be quoted, and then you'll see a semicolon and an array
01:31with a bunch of other objects.
01:33So the other objects are in curly braces.
01:36You can see also that this word joined is also another object with a bunch of data in it.
01:42So that gives you an idea of how you can put objects inside of other objects.
01:47So although JSON works well with JavaScript, there are a number of PHP functions that can
01:52easily parse JSON data.
01:53JSON has some security features that usually prevent you from loading files from external
01:58URLs without some type of permission, and as I mentioned before, the Facebook Open Graph
02:03API calls return data in JSON format, especially in the JavaScript API.
02:09JSON is a wonderful tool for JavaScript, when you format data in this manner, it will be
02:14parsed, and it'll be easier for you to use in your projects.
Collapse this transcript
Creating Open Graph data with the Graph API Explorer
00:00Facebook provides a number of tools for working with apps.
00:03One of the coolest tools is the Graph API Explorer.
00:07It allows you to preview the data the API returns in JSON format. Let's take a look.
00:12If you have been following along with the exercises, we have seen the data we receive
00:16from the API, can be sent to the Console.
00:19So if I go to the Develop menu and Show Web Inspector, so if we open up this Object that
00:25we output to the Console, you can see all the data that we've gathered from the Facebook API.
00:31This is not exactly in JSON format, it has been interpreted for us, and this is actually
00:35a good way of visualizing the data.
00:37But sometimes you want to see--especially since we are talking about JSON itself--the
00:41raw data that comes from Facebook.
00:43To do that, we can use the open Graph API Explorer.
00:47To find it, just go to the Developers section and under the Tools section, if you're in
00:52the JavaScript SDK, it's right underneath it.
00:54Just click on Tools and open up the Graph API Explorer.
01:00So when you run it, you will see the basic user information that anyone can access about our account.
01:05So this is public data, the name of this user, first_name, last_name and the link to their
01:12profile plus their gender and their locale.
01:15Now if we try to access some of this other data, you can see the connections that can
01:19be made to other types of data.
01:21Let's try to click on family and see what happens.
01:23You will see that it says that "An access token is required to use this resource."
01:27So this is kind of interesting, too, if you actually put this in a browser, you will be able to
01:32get this to this data as well.
01:34So if we grab that number--and I am going to open up a new tab.
01:39The URL is a little bit funky, graph.facebook.com, so graph.facebook.com, and I'll put a slash and that number.
01:48So this is actually our ID number, and as you can see, anybody can actually type that
01:52into a browser and get this user information about you, including your ID, first_name and
01:58last_name and whatever you have made publicly available in your Permissions and your Profile.
02:05So Facebook is doing exactly this.
02:07It's actually sending a request to the graph. You can see this is graph.facebook.com and
02:13retrieving the data, and it comes back in JSON format.
02:16So if we go back to the API Explorer, and we switch over to our application--right now
02:21we only have one application, and it's this one right here--you'll see that it creates
02:26an Access Token, that's this security measure right here.
02:30It's just an encrypted piece of text that allows Facebook to make sure that this is a good request.
02:35We can see a lot more data, scroll down here because we have authorized this application
02:41to let us see a lot more data than we have.
02:44So now, if we do something like go to albums, we'll be able to see the albums for this user.
02:51Now still some of this data is not going to be available to us.
02:54Let's see this, activities might be just empty because the user didn't fill anything out.
02:58But if we try to click on addaccounts, you could see that managing this requires extended
03:02permissions that we didn't ask for.
03:05So we could see the friends lists, doesn't really have anything, the user's feed and
03:12notice how the Graph API URL also changes.
03:16So we can actually make calls within our application to the Facebook Graph API with these different
03:22URLs and get the data that we need for whichever part of the application we're working on.
03:29You also have the ability to do a GET or a POST or a DELETE command, so you can send
03:34different types of commands to the Graph API.
03:38And when you go through developing applications, you'll know that--let's go back to the Developers
03:45let's just Command-click to create a new tab-- a lot of times you will be asked to do certain
03:50things like create a post or a GET command to access different types of elements, so
03:56back to the JavaScript SDK here.
03:59So some of these functions say FB.api will perform some of those calls to the URL exactly
04:06like what we're doing in the Graph API, and get a response. The response will be the JSON
04:13data in the format that we saw in the Graph API.
04:16So by taking a look at the Graph API, you can see the data that returns. By taking a look
04:20at this URL, you can see what you need to type into the FB.api command, you just paste
04:26the URL right here, and then the response that you get will be that nicely JSON formatted
04:32data that we've been seeing so far.
04:34So if we go into our application, you could see that this Console doesn't exactly give
04:40you the data in JSON format, but it's sometimes a little bit more useful because you know
04:46that if you need to get say the education of this user, you need to go to the object
04:51that you get back, the data, and then get an array-- and right now, I guess this array only has one item.
05:00So we can get to this data in the exact same way that we would get to an array.
05:04So if I wanted to get to say, the user's first degree here, the degree name then we could
05:13do something like--and I'll switch back into BBEdit, scoot it over side by side.
05:20So if we wanted to print something else out, we could get to that same object by querying
05:25the Graph API with the me function--that just gets the data for whichever user happens to
05:31be logged in at the moment--and then we type in education because it's the next node right
05:36here, and then the education is going to be an array, and we want the first item in the
05:43array which is the 0th item.
05:45Don't get confused by this Array 1 thing. That just means that that there is one object in that array.
05:50The index of the array is zero.
05:53So we have to type in education 0, and then we just type in name which is the next thing
05:58that we want, and then we can just hit Save, and go back into our App and reload.
06:09And then it looks like we are undefined for some reason, so education, this should be education, and
06:16then zero, and then object, the 0th object and looks like I missed something.
06:22Let's go back into BBEdit and see how I wrote it.
06:25So info.education, and then it looks like I forgot--I put name, but I didn't put the
06:30name of which thing that I want which will be degree or school. Let's open up school here,
06:36school name, okay, great, so I'll switch back here, and I just forgot the words school right
06:43here, school.name, save that and Refresh and hopefully I get it right this time, and we
06:52get the school name for that particular user.
06:55So when you want to sort of visualize what the JSON data looks like, so that you know
07:01how to get this, you can either use the Console--just log in the object to the Console, it's
07:06a little bit easier to read. If you want to see the raw data, you can use the API Explorer.
Collapse this transcript
Creating the app's shell
00:00Now that we've handled registration, and we know how to dig into the graph API, I want
00:04to take some time to do a little bit a set up.
00:06We're going to update the app logo and start building the structure for our app.
00:11So I'm going to go to the developer's page and click on this Apps link at the very top,
00:17and I want to update the icon that Facebook uses for my app.
00:21Right now, it's just generic icon, and it's going to look really bad on the app page and
00:26in other places when we get notifications for this app.
00:29So I'm going to click on this Edit button, and I'm going to hit Choose File, and I have
00:34the working folder already on my Desktop, has a copy of the monogram that I use for
00:40this app, so I'll click on it, hit the Choose button, it should update it.
00:46If not I'm going to try refreshing it, and there is it.
00:49So that's pretty cool.
00:50It's nice to have an app icon, it should be probably about 200 pixels square I mean you
00:55could make other sizes but I like this rectangular size it works really well, and it looks nice, when it's small.
01:02So now that we've got our app icon it's time to change the structure of our app.
01:07Right now, this is really just a generic sort of placeholder text for what my app is going
01:12to be, I want to have two for columns for this app.
01:15So I need to just code that HTML and create some CSS to make sure that it can handle that structure.
01:21So, I'm going to switch over to BBEdit, my Text Editor.
01:28So I want to open up the working folder and open of the codesnippets into my Text Editor,
01:34and there it is it already exists up there.
01:36And I want to make sure my facebook folder is open.
01:40And I need to edit the CSS file just for some basic styles, so I'm going to open that up
01:47and also update my index. php file, with some changes.
01:52So I'll start with the CSS, go back into codesnippets and just grab the CSS, paste it into my CSS
01:59file, and I'll save it, and I'll just explain what some of the stuff is.
02:04I have this group class right here that is going to take care of some issues that happen
02:10when you float items.
02:13Sometimes, the containers of the floats do not understand the height of the content that
02:18they contain and so this is a fix for that.
02:21If you want to learn more about CSS, make sure you check out James Willison's Excellent
02:25CSS Fundamentals Course on the online training library.
02:29Let me go back to my CSS file, and explain some of the other things that I'm doing.
02:34So usually when you create any sort of HTML doc you want to create a default font for
02:40the entire document, and that's what I'm doing here.
02:42I'm setting the font to normal 100% size and just a standard font, lucida grande.
02:49I'm setting up standard paragraph size, and I set up the two sidebars or the main section
02:56of my code and the sidebar that are here and here.
03:01So this is just essentially the core structure of my page, I'm going to save this.
03:07And now I need to modify this because obviously, I don't have those sections included in here.
03:13So I'm going to go back in to codesnippets and just grab the next section of code, and
03:18this is just what goes inside the content area.
03:21So I'll copy that, and I will delete all this, leave the content tag right there. I'll paste it.
03:28Let me go ahead and indent it a little bit.
03:31So in our content, we have a main section as well as a sidebar, and we'll put some sidebar
03:38content in here in other movies.
03:41Now we have the fb-root div which is important because Facebook needs that div in order to
03:46place its content within the app.
03:49The next item we have here is a message, this is sort of my version of fb-roots.
03:56It's going to be a place for me to put messages that I want to communicate to the user of the app.
04:02Finally, we have the welcome ID, that we did before so we can publish some of the data
04:09that we get back from the Facebook API.
04:12So I'll save that, and we need one more thing need to update the JavaScript so that we just
04:17output the user's name.
04:19Right now, I believe it's outputting the location of the user school which doesn't necessarily
04:24look that great it's outputting this name here of the school.
04:27So I'm just going to reset that back to the user's name.
04:30So to do that, I'm going to need to open up my JavaScript file.
04:37I'm going to replace this line where I output the user's education with the new version
04:42of it that just outputs the name.
04:45So this is not going to look super exciting, but it's going to have the basic structure of our page.
04:50So let's just Refresh, most of the stuff is going to be gone, and we are just going to
04:55see the sidebar content and Hello undefined, right now let's see if we made some sort of mistake here.
05:02I didn't need to put education first name.
05:05So let me fix that, it just needs to be info.first_ name, copy that and back into myscripts, and I will
05:20replace this with first name of the user, back into our app, we'll reload here, and
05:30now it's going well.
05:32It's not exactly exciting, but it's important to pay attention to details like the app icon,
05:39you can see it already appearing right here that looks a lot better than it did before,
05:44and we've set up the base structure for what's going to go on our page.
Collapse this transcript
Adding a JSON video feed with YouTube APIs
00:00Most apps need to communicate with data of some sort, usually some type of database.
00:05If we were using PHP we might hook up to a MySQL database.
00:08Since we are focusing on the JavaScript API, we need to stick to what we can do with JavaScript.
00:14Thankfully, we can use JSON formatted data to add some dynamic content to our page.
00:20In this video, I am going to show you how to display a feed from a channel in YouTube.
00:23So I am going to switchover to BBEdit and also Transmit, open up, make sure I have my
00:29facebook folder open here.
00:32And the first thing I need to do is modify the HTML to add the placeholder where I am
00:37going to put on my video.
00:38So I'll open up the index file, and that's going to go in the sidebar.
00:43So here's my sidebar, and I'll delete the thing that says Sidebar content because we
00:47are adding some content, and indent it a little bit.
00:50So now we are ready to put some content in this video group section.
00:55So back into codesnippets, the next thing I need to do is add the YouTube JSON call.
01:00So let me just copy that, that's going to go at the bottom of our page just before the
01:06closing bodytag right there, and I'll indent it like this.
01:12You can see that this is querying a URL from YouTube, looking for feeds of a specific type.
01:18So in this case, I am going to look for a channel called lynda, and look for the uploads
01:24of that channel, make sure that I am in a JSON format, and then it's asking YouTube
01:32to return to a function called populateVideos.
01:36So I'm going to have to create this function called populateVideos in my JavaScript.
01:41And I'm asking for seven results, and the category is going to be my last name.
01:46So if you actually take this and copy the URL, so I am just going to copy this URL and
01:52put it on a browser, let me open a new tab here, you're going to get a feed of JSON data like this.
01:59This is really hard to read, and this is normally what happens with JSON, it's really tough
02:05for human beings to read it when it's minified like this.
02:08But thankfully, there is a place called the JSONLint which is a validator, you can just
02:14paste that in here, and hit Return or hit Validate, and it'll pull up the file in a
02:21much more human readable format.
02:23So you could see that we can get to different parts of what we need from YouTube, and mainly
02:29let's see if we can find the list of videos.
02:31So category, go to the key words here thumbnail, title and all kinds of other user data.
02:39It's still a little bit tough to read but if you parse this out you'll be able to figure
02:43out how to get to the data that you want.
02:45So I am going to go back in to BBEdit, and so right here I've got this script that is
02:52going to load the videos, and it's calling a function when it's done that we need to create.
02:58So I'll go back into my code snippets, and I need to add this populateVideos function.
03:03So I am going to grab the code from here.
03:06So what this does is it reads the data that it got from YouTube, always remember the other
03:13call in the index file is sending us to this function.
03:17So it takes the entire YouTube feed and puts it in the entries variable here.
03:21So what this function does is it grabs all the data from YouTube, and it feeds it into
03:25a variable called entries, and then we start by preparing an output variable that we are
03:31going to use to output these things later on into our page, you can see that it goes
03:36to getElementById, finds the videogroup, and it displays the output variable.
03:41And what we do is we want to go through each one of those entries in the YouTube feed.
03:46And to get that, I am going to go to the data which is the entire feed, and then find the
03:51feed section, find the entry section, and then find out how many items are in that entry section.
03:58So if you looked at the JSONLint, you'd be able to find something called feed entry.
04:03So let's say we can find it feed, and there's the feed right here, and we can do another
04:09search for entry and here's an entry.
04:12And there should be all the entries following from here.
04:16So let's say, here is the entry let's see an entry is a array, and you can see the first
04:24entry is right there and all the different pieces of data are going to be right there.
04:30So back into BBEdit, that's what it is doing, and it's going through all the entries one
04:35by one, and then it's kind of parsing what I need from each entry.
04:39I need to get the ID from one of these entries.
04:44And what I need to do is find this thing called $t, it's one of these things right here, see
04:51the $t, right that appears in different places, here it is again.
04:56So I'm finding one of those from the ID section, so back in here, I look for ID.
05:02You can see that right underneath ID there is a $t, and it's going to appear in several places.
05:09So here's a normal entry.
05:11In the ID, if I look at the $t variable, it gives me a link to the YouTube video.
05:17I don't need this entire YouTube link, I only need these last few letters right here, so
05:23I can get rid of all this other stuff and just grab that last part.
05:26And that's what I'm doing in this section.
05:28I am taking the substring of the last few letters and putting them into the entries
05:34ID, which I can use to get a number of different things.
05:37So, next I do the same thing and just find the title of the entry, the description of
05:43each entry, and the thumbnails for each entry.
05:47So you could see where all those are in this JSLint structure.
05:51And then I just output or at least, I feed the output variable a list item for the entry
05:58title here, and then I use that number that I got from doing a substring, feeding it into
06:06this entriesID variable, and I put that entriesID into a link that I'm sending to YouTube, specifying
06:14that the video that I want has that entriesID.
06:16Now let me scroll over to the right, and then I just create an image and the image is going
06:22to have the Thumbnail name here, and as the Alt section is going to use the entriesTitle.
06:28So this is going to get the videogroup and place the output inside that tag.
06:33So we need to put this into the JavaScript section, so we are going to have to open Transmit
06:39and open up our script file and go back into codesnippets, grab the populateVideos function here.
06:48And I do need to save this one, then go back into myscript.js and just place this anywhere.
06:54It doesn't really need to be part of the initialization, because as soon as YouTube finishes loading
07:01the data, it's going to feed, or it's going to transfer to this function which is going
07:07to populate our videogroup area.
07:09So I am going to save that, and I want to go into our application and do a refresh here.
07:17And, as you can see, we're already getting a list of the latest videos.
07:22This is cool because I don't have to worry about updating this page, it'll automatically
07:26be updated as I load, or as new videos are loaded into that section.
07:31So this is kind of a cool piece of script.
07:35If you want to, you can modify this to be your own videos.
07:38Notice that when I'm asking for that feed, I'm specifying that I want to use the channel
07:43called lynda, and that's what I put right here, users/lynda.
07:47You could use your own if you want to right there.
07:49Just put your own username.
07:51And I want to make sure that I only grab items with a category of my last name.
07:56Now this category can also be keywords or tags, so it doesn't have to be a category,
08:02it can be actually a tag in the video.
08:05So you could request a feed of not just the channel, but a set of videos from your channel.
08:11Let me show you the videos that I am talking about.
08:14So if you go over here, we have all the videos that belong to me from lynda.com.
08:20I did a search on YouTube just for my name and lynda.com.
08:25If I just clicked on lyndapodcast, you'd be able to see every single video that lynda.com
08:31publishes for free on YouTube, which is really cool.
08:34But it's kind of hard to get to my videos since there are so many videos that we publish.
08:39In that section, mine are just a simple subset of that.
08:43So by going to YouTube with those specifics I can get only the videos that belong to me.
08:50Let's go back to BBEdit, take a look at our codesnippets.
08:55And that's really cool but I really would like to have a playable video.
08:59One more thing let me show you, I am going to go back into Safari and go back into the
09:03app and show you that these are actually just images.
09:06And if we click on these, right here, it's going to launch and let you play that video on YouTube.
09:13So I want to have a version of the video that sits at the very top that's a playable version
09:18of the latest video in that channel.
09:21So I have a slightly different version of the populateVideos function, I am going to
09:26grab that, and I'll paste it into myscript.js just replace the entire function.
09:34And the difference here is that I am asking this function, if it's the first time that
09:41we run this function, then I want you output something slightly different right here.
09:46If it's not the first time then go ahead and output the small thumbnails with the descriptions.
09:53So this is slightly different version, I'll save that and switch back to my app and refresh.
09:59And now at the very top, we have a playable version of one of the videos.
10:04So that's pretty cool, it doesn't look particularly great, so we need to style this a little bit better.
10:09I am going to go back into BBEdit, and I am going to open up my css folder on my server, open that up.
10:18I need to add some styles into this section.
10:20So back into codesnippets, we created a label that goes at the top of our video feed, so
10:24I am going to style that first.
10:26I'll grab that and switch back to mystyle, at that label.
10:32We're going to end up using that label a lot.
10:34Let's see how it looks, back into my app, and I'll refresh, and the label looks like
10:41this, looks really nice. It's 50% of the size of the container.
10:44As a blue background, I tried to match the bar right here, and it uses one of the custom
10:49fonts that we loaded from Google.
10:51So let's go back, so let's take a look 55%, Normal font, here's the custom font that we
10:56used from Google, color background like Facebook, etcetera, etcetera, pretty simple CSS.
11:03So let's go back into codesnippets and grab the stuff for our normal video feed here.
11:09This is the style for the first element of our video feed.
11:13Go back into mystyle.css, paste that, and for each one of the first elements I want
11:18to give the headline a slightly different look.
11:21And then go ahead and control the font size of the paragraph here and also control the
11:27size of the frame that plays our movies.
11:30So I'll save that, and I'll go back into our app and refresh, and you can see there's a
11:36slightly different look for our styles right here.
11:40That's not bad, back in here, and we need to obviously style the rest of the list, which
11:45is our video feed, which is this code right here.
11:49Pretty simple just controlling the headlines and the paragraph within the videogroups,
11:55and also creating styles for the list item, clearing out the margins, getting rid of the bullets.
12:02Then I size each one of those list items to be 50% of the width of the column.
12:09Let me go ahead actually and copy this, and it will actually make more sense when you're
12:13seeing it, rather than me talking about what I am doing here first.
12:17So down here, I am going to paste this, save it, go back to Safari and refresh.
12:26And there's my sidebar, and you could see that my videos are now here at the bottom,
12:31and I have a pop-up version of the content, the description or the title for these videos
12:38that appears when I roll over these little thumbnails.
12:41So that's pretty cool, the way they I did that, now hopefully it will make more sense
12:46when I show you the CSS.
12:48So the way that I did that is I created some styles for the video, headline and paragraph
12:54right here, some basic styles for the unordered list, hiding the margins, hiding the bullets,
13:01and then made each one of the list items half as wide as the column.
13:06Floated each item to the left, so they appear side by side, and took out any border and
13:13positioned them relatively, so that when I hover over an item, that hovered item is going
13:21to be positioned absolute, so that it appears right next to the position of each one of these list items.
13:29The iframes, the width right here's a 100%, but because each list item is 50% it's going
13:36to be 100% of 50%, which is 50% as well.
13:40And I need to specify a height, otherwise the default height for the iframes is a little bit too small.
13:45You can play around just take that out, and you'll see what happens.
13:48And then, I need to create a style for my entriesTitle, so that the title that now
13:54appears when I roll over is in the right position.
13:58So I move it to the left a 110%, so they appear left of the thumbnail.
14:04And I originally have them hidden, that's what this means right here, display: none,
14:08top: 0, it's just the position is right at the top of the thumbnail.
14:13And next to that when I hover over each one of these list items, I display them, or I show them.
14:20The width is still the same.
14:21Change the color or actually color the font white and add a little bit of padding so it
14:27has a little bit of an area around, you know I have the opacity at 90% that's why you can
14:31see a little bit of this black right here.
14:33So it's a pretty good looking stuff for our page for this section, which just shows a
14:38few YouTube videos. And it was really easy to do.
14:41All we did is just go to YouTube's version of their APIs and grabbed some of the code,
14:48inserted it into our JavaScript file, and it's working pretty well.
14:53And don't forget that you can create your own version of your data that you want to
14:56put on your web sites.
14:58You could do it by using some sort of JSON variable or some service JSON feeds that allows
15:03you to pull from an existing web site.
Collapse this transcript
Debugging cross-browser security issues
00:00So I wanted to show you an issue that happens when we start calling in feeds and other data from external sites.
00:06It's something that you have to watch out for and program around.
00:08So let's take a look.
00:10Our video feed from YouTube is doing really well, but if you go to the developer console--
00:14I am going to go to Develop and say Web Inspector.
00:19Right now, we're already on the developer console tab.
00:22Sometimes this thing doesn't come up perfectly, so I'm just going to click on this icon and
00:26then click on it again, and there the window shows up like it should.
00:30So you could see that we're getting a lot of warnings, and the warnings are all related
00:35to insecure content coming in from somewhere else.
00:38Essentially from our Google Feed, the problem is that Facebook requires you to use an SSL
00:43certificate on your site.
00:45And that allows the transfer of information between your app and Facebook to be more secure.
00:49But it means that we have to be careful when we code the pages or when we import code from other places.
00:55When working on a site with the SSL certificate on, if we link the data with a regular http://
01:00you will see this warning.
01:03It's not got to be problem in a lot of browsers, but we should fix it.
01:07So to do that, I'm going to modify the code from Google and use https links.
01:11Now, it's going to be a little weird in some places because some of this data is being
01:15read directly from Google's Feed. So let's take a look.
01:19I'm going to switchover to BBEdit--and there's my codesnippets file--and open up transmit,
01:24open up my facebook folder, and in here I need to pull the JavaScript file as well as
01:31the index.php file. So we need to fix a couple of problem lines.
01:35Essentially, these two lines that right now in the code will say http need to be changed to https.
01:41Let me go ahead and open these files.
01:45There's the myscript. Looks like I un-clicked on the index file, so I'll open that as well.
01:50So here's the call to the gdata from YouTube.
01:52I'm going to go ahead and put an S right here and save the file.
01:57You can copy them from code snippets, but it's really just easier to type the esses in here.
02:02And somewhere in here is the call to our video, and here's another one right here. Http needs
02:08to be changed to https, and this one is well.
02:12I'm going to save this, and what you could do is you could do a find and replace in most
02:15programs just do a search for http and make sure there are none other ones there.
02:21So, http:// right now. I don't have any more, so that's perfect.
02:26And once I save those, I'm going to switch back into my app and refresh it.
02:35And now you can see that there's a lot less errors, but there's still this error right
02:38here, which is because I'm using a link that the Google Data Feed provides.
02:46You can see these images down here that are the thumbnails for the videos.
02:50Those are being read from the Google Feed.
02:52So we're going to need to fix those in a slightly different way.
02:55Let me go ahead and see if I can make this a little bigger. No, that's about as far as it'll go.
03:00So let me go back, and I'll go into my codesnippets, and I will grab this piece of code because
03:05it's a little bit gnarly.
03:06So I'll grab this last piece right here and look for the same piece of code on the populateVideos.
03:14Here's where I'm reading the thumbnail for the files from the Google Feed.
03:19Now I'm going to paste this and show you what I am going to do differently.
03:23If I read this URL, it's just going to get me a URL that looks like this: http://i. something ytimg, et cetera, et cetera.
03:32So if you look at this link, it's pretty much a link where this middle piece right here
03:38is the ID of the video that we want to see.
03:41So we could grab this entry right here and remove the ID and insert an https instead of an http.
03:49But it's really just simpler to type the link in and just use the slash and entrance ID variable
03:55that we've already created up here when we took part of the string that we get from Google
04:01and took a little piece of it so that we have the ID by itself.
04:06So this is just going to take the proper name of the image with the https at the end, and
04:11I want to make sure that I save this.
04:13Go back here, refresh, and now our app should look perfectly clean.
04:20We were outputting the object here, so that's still there.
04:22And you can see that all the errors are fixed.
04:25So as you work on your site, you'll occasionally want to perform this type of change.
04:29Go into the developer tools and take a look at the Errors and Warnings section, and see
04:34there's anything that you have to fix.
04:36It's best not to have any warnings, even though on some browsers it is not going to be a
04:39problem just because you want to make sure things are as clean as possible.
Collapse this transcript
Installing the WordPress JSON plug-in
00:00It would be nice to have a feed of stories from the web site associated with this app.
00:04That happens to be a web site called iviewsource.com. This is a companion site to my weekly series
00:10on lynda.com called iviewsource.
00:12So I write bunch of stories and blog posts right here, and I want to have that feed of
00:16content on my web site.
00:18It's WordPress site, and you might be tempted to just query the SQL database but in keeping
00:23with the JavaScript theme we need a JSON solution.
00:26Thankfully, there's a great plug-in you can install on a WordPress site that will generate
00:30a JSON feed of your data.
00:32The plug-in is called JSON API, and this is the URL for it.
00:36You just download from right here.
00:38But it's easier to just log into your admin center, so I am going to type in wp-admin
00:44and get into my admin screen and just install it directly from here.
00:48So I'm going to click on plug-ins. To find the JSON API plug-in, all we need to do is
00:52hit this Add New button and then type in json api on the search, click on Search Plugins,
01:02and it should find the plug-in right there.
01:04So we could just hit this Install Now button Are you sure you want to install this plug-in?
01:11Yes, it will take a second. It's successfully installed.
01:14I am going to go ahead and click on the Activate Plugin button, and now that plug-in has been activated.
01:20So let's just check it out. There it is. It's been successfully activated, and it can be that easy.
01:26If interested in learning more about WordPress and Plug-ins, make sure you checkout WordPress
01:30Essential Training on the lynda.com online training library.
01:34So let's take a look at what you can do with the plug-in. The documentation for the plug-in
01:38is right here at this URL, and you could see that it has a lot of descriptions about what you can do.
01:45And the basic example of how to call the plug-in is right here, the Implicit mode example.
01:49Really, all you have to do it is find a page for a location on your web site that you want
01:55to get a JSON feed of, and then add the ?json=1 at the end.
02:01So let me go back to my site, and I'll just click on this link up here.
02:05Go back to View Source and all we have to do is just add ?json=1 that is going to generate
02:12a feed of our site.
02:14This is a little hard to read, and this is where JSON kind of becomes a little troublesome
02:18to actually parse visually.
02:20So what I want to do is copy this URL, so I want to go to jsonlint.com and put the URL
02:26right here. You can also paste to JSON directly into this window.
02:29I'm going to click on Validate, and it's going to show me the JSON feed in a more human readable
02:37form, so you could see that here we have posts, and every post is going to have a different object entry here.
02:45So here's another post, there's a comma separated part, and we can get into any of the data
02:49from the post by digging into this stuff.
02:52So you could also just go to any page on the web site, so let me back up, and I'll just
02:57go to this first article on using JSON, and I'm going to take that link right there, and I'll just add a question mark
03:08and do json=1, and now we get a feed of only this article.
03:13So if we want to get all text of the article, you could that.
03:15You can also get the most recent posts, just like this: json=get_recent_posts.
03:26There's the latest posts, and if you want a specific amount of posts, all you have to
03:31do is add count= whatever number you want here, so I'll say 2, and this way, you will
03:38only get the most recent two posts, and then hit Validate. And now we can see here is the
03:45posts variable here with the array, and the array will have two posts because that's what we specified.
03:52So if you're not using WordPress, you will need to create your own feeds. The JSON format
03:56is not particularly tough to create and understand, but having a plug-in like this sure makes your life easier.
Collapse this transcript
Adding a JSON post feed onto a page
00:00Since we've installed the JSON plug-in into our WordPress site, we have access to a feed
00:04of information, and we can pull that data into our Facebook application.
00:08We've already done something similar when we pulled in our YouTube feed.
00:12So let's take a look.
00:13So I am going to go into BBEdit and make sure my codesnippets file is already open.
00:17And I am also going to go into Transmit and open up our Facebook folder.
00:22I need to open the JavaScript file, so I will open up this _ folder, the js folder, and click
00:28on the myscript.js, and I will also need to open the index.php file.
00:32I will select both of those and open them.
00:36The first thing I need to do is add a div to hold my content. It needs to go after this
00:40welcome div, so I am just going to open this up right here.
00:43And I am going to go into codesnippets and grab this placeholder for our blog content.
00:49So all the blog content is going to be inserted into this div, back into index.php and just
00:54paste it right there, and we will just indent it with everything else.
00:58Now that the JSON is there, we need to pull our JSON feed from the WordPress site.
01:03So I will render a little bit of JavaScript code to handle that.
01:05Let's take a look, go back into codesnippets, and here is the code that we need to insert
01:11into our JavaScript file. It's very similar to the script that retrieves the YouTube feed,
01:16and we worked on that on the movie on adding a JSON video feed with YouTube the APIs.
01:21So if we take a look at this, it's doing pretty much the same thing, setting up a couple of
01:24variables, and then setting up an output that starts with a heading.
01:28Then it's going to go through each of the posts that the JSON feed returns, and it's
01:33going to store that information into the output variables, such as reading the data from the
01:38JSON plug-in that we installed in WordPress, and then it's outputting in a variable that
01:43we can then insert by calling this getElementById and looking for the blog div that we placed into our HTML.
01:51And then setting .html to the output variable that we are going through and setting to the posts each time.
01:56So I am going to copy this. I am going to go into myscript.js.
02:02And that needs to be at the same level as our populateVideos function, which is right here.
02:07So I am going to place that right on top of that, so I will paste that right there.
02:11So now that we've created this function, we also need to call the function.
02:15So this code is not going to execute unless it gets called from somewhere, we want it
02:19to be called after the initialization for the Facebook API is complete, so that would go right here.
02:25So let's go back in the codesnippets, and just grab this code, we are going to put that
02:29into this line right here, and that's going to call our function.
02:35Indent it out a little bit, and I need to make sure that I save both of these files.
02:38By placing it here, we assure that after the Facebook API has been loaded it's going to
02:44execute that function.
02:45We then need to do that with the populatedVideos because that was automatically called by YouTube's
02:51API, and that was done, so if we go back into our index page, you could see that this code
02:57that we inserted from YouTube has a callback right here that targets our function called
03:04populateVideos as soon as the information has been loaded from the YouTube API.
03:10So we then need to put this function into our JavaScript file, because it's already
03:16been called by that YouTube API.
03:18So we will insert that there, and now we are ready for this information to come in.
03:23So let's go ahead and make sure that they're saved.
03:25And I want to switch back in to Facebook, I want to refresh this page.
03:31So now our feed of information is coming in, it doesn't look very good, but we will fix
03:34that with some CSS later on.
03:36I do want to show you one thing that is not working here.
03:38If we click on this link right here, its going to open up a new window and take you to that
03:42article on view source.
03:45Now if we click on this link right here, it's going to actually open that link up in the
03:50Facebook iFrame, and that's not good.
03:53So I am going to click on this Back button.
03:55If it asks you, if you want to send the form again, just hit Send. It's not going to really make a difference.
04:00So the problem is happening if we click on these links. These links are coming directly
04:06from the feed that comes from our WordPress site.
04:09So they're already in there. We can't modify those links. We have already got a link to
04:13the content up here, so I could just get rid of these continue reading links, and then
04:18our feed will look just fine.
04:19So I am going to do that. I am going to back into codesnippets with a little bit of jQuery.
04:25So back into codesnippets, and I am going to grab the next piece of code, this piece
04:29of code is going to remove the links from the feed that comes in from our WordPress plug-in.
04:34So I am going to copy this and go back into myscript.js. You need to scroll down while
04:40it's going through each of these articles.
04:42I want to make sure that I insert that function right there.
04:47So as that information is being read from the WordPress plug-in, we are going to create
04:52a temporary variable and feed the excerpt into that temporary variable and then use
04:58this little bit of jQuery to remove all the anchor tags from that temporary variable.
05:04Then we'll feed that back into a variable called excerpt.
05:07So that excerpt variable will have the data from the YouTube feed that is the excerpt
05:12without any of the anchor tags.
05:14So we will need to modify our code a little bit more.
05:17Change this right here so that it doesn't output the excerpt but it outputs this new
05:21version of the excerpt which gets rid of the anchor tags.
05:25Back into codesnippets really quick. We will grab this next line of code.
05:28This just makes sure that we have the new version of the excerpt, so we will replace
05:33this right here and save that.
05:37And now we'll go back into our Facebook application, hit Refresh.
05:43These links the anchor tags that use to be there are now gone, which is great.
05:47So all we have to do now is make this look great, go back into codesnippets, and I have
05:52prepared some CSS that's going to style the blog posts.
05:55So let me grab this code right here, and I'll paste it into our CSS file.
05:59Looks like we didn't open that, so I am going to go back into Transmit, look for the CSS, open this up.
06:08So I will put this at the very end of the file, I'll save it, and then I'll go back
06:12into Facebook and Refresh.
06:15And let's take a look at--it looks a lot nicer.
06:18Let's go ahead and pull this over to the side so I can go over the CSS and show you what I've done here.
06:23It's really simple CSS--let me make this one a little bit smaller so you could see them both side by side.
06:28So, see just setting up a little bit of a margin, changing the font to this XO font that
06:32we've imported from Google fonts.
06:35And then just making because some basic changes to the way that everything looks on our Facebook page.
06:41There's nothing too complicated on this page. The only thing that you may see that's a little
06:46bit different is we are changing the cursor here so that when you go over these links
06:51it gives you that little pointer cursor that's a hand.
06:54So our feed is looking great. Content on your app can be data you get from other sources
06:59like web sites, YouTube feeds, or any other kind of feed.
07:03JSON with a little bit of jQuery gives you the ability to easily parse and feed data into our application.
Collapse this transcript
5. Adding Social Plug-Ins to Your Application
Introducing social plug-ins in your app
00:00Social plug-ins are way to add features from Facebook into your content.
00:05Normally, these are features you can add to web sites by copying and pasting some code from Facebook.
00:10You're probably familiar with things like Comments, the Send and the Like button, et cetera.
00:14So let's take a look at how you can integrate them into an app.
00:17So, when you first log in to the developer site, you see this sort of division between
00:22building things for web sites and building for mobile and building apps on Facebook.
00:27We have been focusing on the building apps on Facebook section, and if we click on Build
00:32Apps on Facebook, and we go to the SDK Reference, go to the javaScript SDK, you'll notice that
00:38there's no mention of social plug-ins. That's really because social plug-ins are part of
00:44the building for web sites.
00:45And we can find them here by going to Core Concepts and then hitting Social Plugins.
00:51So here's all the plug-ins that Facebook provides. There's are quite a few of them, the Like
00:54button, the Send button, Subscribe, Comments, Activity Feed, and other things.
00:59If you click on the Like button, you'll see that when it talks about adding this to your
01:05sites, it assumes that you're going add it to a web site, not an app.
01:09So we'll have to watch out because the code is a little bit different.
01:12So if we scroll down, we'll notice that there is a code generator right here. It's pretty cool.
01:16You can put in a URL that you want to like.
01:18We can type in, for example, our app URL, and then we can customize the button by clicking
01:27on different things, changing the width, showing faces if there's anybody that's already liking
01:34this app, Color Scheme, et cetera, et cetera.
01:37And when you hit Get Code, it's going to give you three options here to use the code that's HTML5.
01:44This is really what we want.
01:45XFBML is something that you don't want to use or IFRAMES in this particular case.
01:50So if you look at the code that it gives you, the first thing is it wants you to use the
01:55fb-root div, and we've already added that to our site.
01:58So we definitely don't to copy that.
02:00Next you see a script, and that script is essentially the JavaScript SDK being loaded.
02:05There's little bit of a pop-up here, so it can populate some of these fields for your particular app.
02:11If you have more than one app then you would see more than one right here.
02:14And this piece of script right here is obviously loading the JavaScript SDK.
02:19We already have that code on our site, so we don't need that.
02:21The only thing that we need is this right here.
02:24So when you're reading these social plug-ins, it's kind of important to understand which
02:29parts of the code that it's telling you, you need and which parts you don't.
02:31So I'm going to copy that and click OK.
02:34And if you scroll down, you'll see the different attributes that you can add, so you can do
02:40these manually, because we just copy the code. You can see a section here about getting Open Graph Tags.
02:45We don't need to do that, the app is already connected to Facebook, and through the JavaScript
02:51SDK, the web site already has all this information.
02:54So on a normal web site, you would need to grab all this code. In working with apps,
02:59you don't need any of that at all.
03:01And let's see what else.
03:02There are few other options here, but all we need is to go back into BBEdit.
03:08And here is my codesnippets, I'm going to open up also Transmit, and I'll need to open up the Index file.
03:18So I'm going to go to codesnippets. I already have a version of what we need here.
03:22I'll first grab just this headline and div for all our social plug-ins.
03:27So it is just a normal div with a h2 for the label that goes at the top, and it has something
03:33that says Help us share View Source by liking this app.
03:36So I'm going to copy this, go back into our index page, and I want this on the sidebar
03:41on the top of the video group.
03:43So I'll put it right there, indent it a little bit.
03:47And when we go back to our app, it should just at least have the headline and be ready
03:52for the Like button. There it is.
03:56So switch back over to BBEdit, next thing is we are going to grab that code that we
04:00got from the code generator and just copy that, and we'll put it right underneath this right here.
04:10And really a lot of the social plug-ins, all you do is you grab a piece of code, you got
04:14to know which part to grab.
04:16And it'll display whatever you want on the site, including stuff on this app.
04:20So you could see there's the Like button looks pretty simple.
04:23Now one problem is that it's way too wide. So, we really want to fix that.
04:27I'm going to go back into BBEdit and back into my codesnippets and grab a much simpler
04:32version of this Like button.
04:35So I don't really need the data-href again. The JavaScript SDK already knows what page
04:40we are on, so you don't really need that.
04:42Also, we don't really need any of this other stuff, and the data width is too wide.
04:46So I'm setting it here to 200, and I don't need to show the faces of the people when
04:51they log in, so this is a much simpler version of the code.
04:54Back into my Index file, paste that piece of code right there, save it.
05:00And we have just the Like button, fits a little bit better, and we can click on it.
05:06We can type in a little message. This is going to go on our timeline.
05:14So, and I'll post it to Facebook, and that's it.
05:18I'm going to Command-click on this Facebook link right here to open up Facebook on a new
05:24tab, and there it is.
05:25There is Like that we did, and that's pretty cool.
05:29It's easy to add some of the social plug-ins to your apps, but one of the things that you
05:33really have to watch out for is for making sure you understand which parts of the code
05:37on that page, because those particular social plug-ins are designed to normally just be
05:43things that you put on web sites, not on apps.
Collapse this transcript
Adding the Facepile and Comments social plug-ins
00:00In the previous movie we learned how to add the Like button our app.
00:04So let's go and explore some of the other options that we have in social plug-ins, and
00:08I'm going to add a couple of them.
00:09So I'm going to click over here to go the social plug-ins, and you can see that there
00:14are a number of plug-ins available to you, including Comments--which is what I want to
00:18add right now--and also something called Facepile.
00:21Facepile is a picture of other friends who are users of this app.
00:26So I am going to go to the Comments section first and look at the code for that.
00:31So you could see that there's another code generator here, and if we get the code, it's
00:35going to pretty much tell us what we saw on the Like button--a bunch of code that we don't
00:40need, which is this div that's fb-root.
00:43We don't really need that, we already have that in our app.
00:46The other thing here is loading of the JavaScript SDK, again that's something that we already
00:51do in our apps, so we don't need that.
00:53Really the only thing you need is this piece of code right here, but I've got that in my
00:58snippets file, so I'm going to switch over to BBEdit and switch over to Transmit and
01:02open up my index page from the site and copy this fb-comments div, and I want that to happen
01:13right underneath the blog section.
01:15So what is it doing?
01:17I need to make sure I set the data width right here, because the default width is a little bit short.
01:23We also want to make sure we add a data-href.
01:26Unlike our previous button, we need to make sure we tell Facebook that these comments
01:31are all going to be from this URL.
01:33When Facebook receives comments from different web sites, this is how it identifies all the
01:38comments as belonging to this particular application, and then we can put the number of posts, if
01:44you want to right there. So I'll save that.
01:47I'll switch over to my app, refresh, scroll down to the bottom, and I should see the Comments
01:55section right here.
01:57So if I want to, I can add a comment.
01:58I'm also a moderator because I'm the creator of this app or this user is a creator of the app.
02:03So I can go in to Moderator View, something I wouldn't see normally, and I'll hit Comment, and there you go.
02:10Showing you how to put a comment there, pretty cool.
02:13You can do things that you normally do with comments like Like or Reply.
02:18Let me go ahead and go back to Facebook Developers, and back to Social Plugins and scroll down to the Facepile.
02:30Another really easy to do Social Plugins, you can also get the code from here and customize it.
02:36Remember, you don't need any of this just get this part right there and paste it onto your app.
02:41So, I already have that and the code snippets with a little headline that says who loves
02:47us, here's a punch of your friends will also like this app.
02:49So I'm going to paste that into the social plug-ins section right underneath our Like button.
02:55Let's put that over, save it, and go back to our app and refresh.
03:08So now we can see a few or our friends who've already added this app.
03:12So it is just that simple. You can add a number of social plug-ins into your app.
03:17It's a pretty similar process to how you add things to regular web sites.
03:20Just keep in mind that you don't need to initialize the SDK or insert the Facebook div, and there
03:26might be some slight difference in the code between the different items that we add.
Collapse this transcript
Creating a live event chat
00:00So there is one more social plug-in, I want to show you, not because it's any different
00:04than the other ones, but just because it's pretty awesome, it's the Live Stream social
00:08plug-in that lets users chat in real time on your app. It's just about as easy to
00:13use as the rest of the plug-ins, so let's check it out.
00:15I am going to go to the social plug-in section of the Facebook developers area and scroll
00:20down until I see the Live Stream right here, so I'll click on that and the Live Stream
00:28lets your users share activity, this is really cool when you have stuff like concerts, speeches, webcasts, and stuff.
00:35Everything is pretty similar except it is going to want an app ID, and if you have multiple
00:42chats going on, you want to make sure that you specify a number or some sort of unique
00:45ID right here to differentiate them.
00:48So, I don't need to get the code. I already have it in my snippets file, so I will switch
00:52to BBEdit as well as Transmit.
00:55Here's our code, see it's just as simple as the other one, and I'm going to open up the index.php file.
01:01This plug-in also has a minimum width, so is that going to fit on the sidebar where
01:06the rest of my social plug-ins are, so I need to put it in my main area, I'll grab it, and
01:11I'll put it on top of the blog stuff right now.
01:14So, here is my blog, and I'll paste that right there, Save it, and I'm going to go back to
01:19Safari and go back to my app, Reload. And there I have an actual chat, and I can discuss
01:30things with users, so let's go ahead and put something in here, so we're going to hit Share.
01:36Notice that it goes into the event in real-time.
01:38Now this is kind of boring, because I don't have another user using this app right now,
01:42but I'm going to switch over to Google Chrome, and I am going to refresh my app here as well,
01:47because I'm logged in as another user. Let's scroll to the top.
01:50There I can see my other user and just type in a comment and hit Share and the two users
01:57can kind of communicate with each other in real time, so I'm going to switch back to Safari.
02:02Now eventually this should be updating on the fly, so let's just go ahead and type something
02:06in, and this is also going to post to my Timeline if I just leave it like this, so if I click
02:11that off then it won't post, so I think there is a little bit of a lag, because I'm using
02:16two browsers, and it's not showing me the result of the other person here, but let me
02:21refresh--maybe I can see the whole stream of conversations.
02:25And once again because I am the administrator of this app, I can ban this user if they're
02:29doing something crazy.
02:31So this is a really sweet plug-in. I'd love to maybe add it to a web site, and let's go
02:35ahead and add a little bit more description to our site here, so we'll just had a little
02:44label and a description of what this is.
02:48Then I'll go back into one of my browsers just refresh just to get a nice little headline
02:56at the very top that identifies this, so like I said this is one of the really cool plug-ins
03:02I can imagine that Facebook will be improving them as they go along and maybe adding some
03:07new ones, so this is a great way of adding a lot of functionality to your app without a lot of code.
Collapse this transcript
6. Creating Feedback with Dialogs
Posting to a user's timeline with the Feed dialog
00:00One way an app can communicate with the user and their friends is by posting on their timelines.
00:05You can do this through the FB.ui method of the JavaScript SDK.
00:09So let's take a look.
00:11So the FB.ui method--which is right here--is one of the core methods.
00:14Let me click on that, allows you to trigger Facebook dialogs that can do things like
00:19publish stories, prompt user to add friends, et cetera.
00:23It's really easy to use.
00:24You simply issue a call with certain parameters and the method pops up a dialog.
00:28So I am going to use it to add a post to our feed option that lets our users share the
00:33article posts from the blog.
00:34So I'm going to switch over to BBEdit and also pull up transmit.
00:38I am only going to need the JavaScript files.
00:41So let me open that up.
00:48So here's JavaScript. What I want to do is add a link when we populate the stories
00:52to the end of our stories to allow people to post something about that particular article to their feed.
00:59So right here I want to go to my codesnippets and just grab something that creates a link.
01:05So we just want to add a link, and that link is pointing to a function, and that function
01:10gets three parameters here, the title, the link itself that we're clicking on, and also
01:14little bit of the excerpt, because we can use that in the dialog to display some feedback to the user.
01:20So I'm copy this, go back into myscript.js, and right here I'll just paste it--go ahead and indent it.
01:29And I'll just hit Save.
01:30So all this does is just create a link that says post a feed at the end of each article.
01:35Let me go back into Safari and Refresh this page.
01:43So here come our stories, and at the end you'll see this little link right here.
01:47So let me go back because now we really have to make it work.
01:50So back to be VB Edit, and now that we've got that function call, we need to create the function.
01:56So here's a function, it begins right there, and it ends over here, and all this function
02:01does is actually make a call to the FB.ui method, and that method gets a JSON object.
02:08Here's the JSON object that it gets, and that JSON object essentially tells it what kind
02:12of dialog this needs to be, and this happens to be a feed dialog here.
02:17Then it needs to get things like the link, the picture if you want to--so here I'm sending
02:22a picture of the monogram--the title of the page, a caption, and a description.
02:27Once you see the actual dialog come up, you'll see all these elements appear.
02:31Once the user interacts with the dialog box, whether they choose to post to their feed
02:35or not, they're going to either get a message that says thank you this has been posted, or
02:39the post is not published.
02:41So let me copy this, and I'll go back to myscript.js, and this can really go anywhere, so I'm going
02:48to put it on top of populateVideos and save this.
02:52Come back to my app, refresh, and scroll down to get to our story.
03:01So here's a story about an article on the JSON Tutorial.
03:03I am going to click Post to Feed, and so here's what the dialog looks like.
03:10And I want to point out some of the items from the JSON method here.
03:13So the JSON object will have the type of dialog that this is going to be, this is a feed,
03:18then a link which we are feeding from the anchor tag that we created.
03:21And that happens to be just a link to the article.
03:24All right, which when you see the actual post on the user's timeline, you'll see that you
03:28can click to the article to read it.
03:30Then the name is going to be the title of the story.
03:33Here's the title of the story.
03:35The caption, it's going to say this is from the view source blog, so that goes right there.
03:39And the description is the description from the article summary.
03:42We got that by using that JSON API plug-in from WordPress.
03:47So if the user clicks on Share, that gets shared onto the timeline.
03:51You can see that this has been posted onto your timeline, that's the message that we get.
03:56So that's pretty cool.
03:58And if we go to the user's timeline, I'm going to Command-click on this Facebook link here
04:03to open this up in a new tab.
04:05We should see that added to our timeline.
04:07So, it might be a little slow. Let's go ahead and refresh this page. I'll click on his name,
04:16see if it's being added somewhere else.
04:17So there it is. It's actually on his page right now, and maybe it's just taking a little while
04:21to get to this actual main timeline.
04:23So there it is. There's our post. You can see that it got added via the app and it has
04:26all that text and people can click to our story now.
04:29So this is an easy way of adding some Facebook-like functionality to your apps.
04:34In this case, we're using a dialog. It's really easy to use.
04:37All you need is just a JSON object to push into the dialog box.
04:42When the user clicks the post or not, they are going to get an ID as a response or your
04:46function will receive an ID as a response, and you can use that response to either give
04:52the user some feedback, whether or not they click on a post.
Collapse this transcript
Sending a message to a friend
00:00There's another dialog that you can use where you can message a friend instead of posting
00:04something to their timeline, and I wanted to show it to you because it's a little bit
00:07tough to get to, and it kind of illustrates some of the issues with JavaScript SDK web site.
00:12So from here I'm going to go to FB.ui, because that is the method we're using, and if you
00:19notice, there's nothing that says send a message to a friend. There's nothing even down here
00:24on the left-hand side navigation.
00:25But if we click on prompting the user to share link, you'll see that on the left-hand side
00:32there's a bunch of dialogs that you can set.
00:34So these are sample pages for coding these different dialogs.
00:38There it is at the bottom of the page, so I'm going to click on that, and here is the
00:42example for that one.
00:44It's pretty much the same thing as what we did before, it's just sort of in a weird place
00:49and sometimes these things for whatever reason aren't exactly where they should be.
00:53So I already have this set up, and it's very similar to posting to a timeline, so I want
00:59to switch over to BBEdit and also open up Transmit, and I should have the Facebook folder open.
01:07And I need to open, of course, the JavaScript file because it's what I'll be working with.
01:12So I'll open that up and the codesnippets are going to be super similar, we're just
01:16going to add a link to message to a friend, which is going to be a function we are going
01:20to define in a minute. So I'll grab it.
01:22It sends pretty much same info as the posting to a timeline.
01:26Let's go into our JavaScript function and locate the populateStory section where we
01:31put all the stories.
01:32Here's where we add the postTofeed link, and right underneath that I'll add the Message
01:37a Friend, save that, and I'm going to go over to my code snippets now and get the second
01:43part, which is this function for messaging to a friend.
01:46And this is exactly the same as the other function, but obviously I modified things,
01:51like the message instead of the post. Everything else is pretty much the same.
01:56Obviously the method is different.
01:58That's how it works with most of those dialog boxes. You're just going to be changing the
02:01method and everything else is going to be the same.
02:04So here is postTofeed. It really doesn't matter where I put it, so I'll put it down here.
02:08I'll save this and switch over to Safari, refresh my page, and I'll scroll down here,
02:16and there is the Message a Friend.
02:17So I am going to click on this to send a message to a friend. I am going to send it to Darren.
02:22There's Darren. And so with this particular type of dialog box, I can choose the friends
02:27that I send this to. And this will pre-populate this if you want to, with a specific user.
02:32So I'll send Darren a message, and I'm going to hit the Send button here, and you'll get
02:39a little messages just like before. Notice that it actually created a chat with him,
02:45with this automatically placed in there, and we'll switch to another browser, so Google Chrome.
02:50We have Darren's account, and eventually we should see that message--actually that's probably
02:56what we were seeing before, so there is the message that we're getting.
03:00So there are a lot of different dialogs that you can use, but the code is almost identical,
03:05you're just changing the method.
03:06Still, these things are something you should take a look at, because a lot of them can
03:10be useful ways of communicating with people through your app.
Collapse this transcript
Sending invitations with the Request dialog
00:00If you've been on Facebook for any length of time, you've probably received requests
00:04to join other users and apps.
00:06Perhaps, you have received some sort of animal request from FarmVille.
00:09Now we can do the same thing in our app through dialog boxes much like the ones we've already been doing.
00:15So let's a look at the javaScript SDK and how to do requests in apps.
00:20So I'm going to click over here to the javaScript SDK, and I'm going to go to the FB.ui.
00:26And in there, I'm going to click on Prompting the user to send an application request to a friend.
00:30So here's what that looks like.
00:32It's pretty simple, much like what we've done before, but a little bit different. It's a little
00:36bit simpler request function here.
00:39We see that we have a message, we don't need all that other stuff.
00:42And we also have a to: attribute. This to: attribute allows us to send a request to a
00:47specific person as long as we know the user ID of that person.
00:51Now if I want to send a request to more than one user, you can include them in the to:
00:58attribute here by separating the values of the user IDs with commas.
01:02If you want the users to pick their own friends from a list of friends, all you do is Remove
01:06the to: attribute that we used to up here, so you don't see it up here, and then they
01:10can take any number friends that they want.
01:14If you have Frictionless Requests turned on-- and that's one of the things that I added
01:18to this application when we were setting up the FBAsyncInit is to add this functionality to it.
01:25What will happen is if they've approved some friends to already be able to receive requests,
01:30then they won't have to see a confirmation dialog every time they send a new request.
01:35That's kind of a nice feature, and it's something you should definitely turn on.
01:38There're a lot of other options in any of this documentation with a lot of functions
01:43or methods like the FB.ui method. You can send some of the same requests through JavaScript
01:50or through a URL, and this is what this is telling you how to do here.
01:54You can send a request dialog for your app with a URL so that it becomes something that
01:59perhaps you click on a link to get.
02:02You do it pretty much the same way. You set up the URL that you're going to send this
02:07to, which is facebook.com/dialog/apprequests, and you pass some variables like what you do in JavaScript.
02:14You pass the app ID, which you can get from this Apps link right here.
02:19You can also send a message, and you can also send a redirect_uri.
02:23So here's the property so you can send to that. You can send it to and any number of
02:27filters and other things.
02:29So it's always a good idea to check out the documentation.
02:32Let's go ahead and do one. I'm going to hide this.
02:35We're going to go over to our codesnippets.
02:38So we are going to actually need to open up the index file, the JavaScript and the CSS
02:43file, because we're going to make changes all those.
02:45So I'll open those up.
02:48We're going to add a picture that's going to be on our side bar.
02:52It's going to invite our friends to come into our app and join us in the app.
02:57So I'm going to hide this, and I'm going to import the image.
03:01There it is right there, it is just a little box that tells people to come join me in the app.
03:06So I'll drag that into the Images folder, and then I'm going to switch back to BBEdit,
03:11and I'm going to go to my codesnippets and grab the HTML.
03:15This is just a regular image link that is going to click over to the JavaScript function
03:21we're going to create.
03:22So I'll copy this into my index.php. It can go right after the social plug-ins.
03:28So I'll paste it right there. I'll save that, back into codesnippets.
03:32Now I am going to grab a little bit of CSS just to make sure. Let me show you what that
03:36looks like right now.
03:37My image is a little bit too big, and that's okay.
03:41Click over here, refresh.
03:43But right now it's a little too wide for our sidebar.
03:45So you can only see part of the image.
03:48So I'm going to go to my BBEdit and get a little bit of a style to override that.
03:54Let's put that at the very end, save that.
03:58Let's take a look at it now.
03:59So that looks a lot better, it even gives us a little bit of a margin at the top, and
04:05now we need to make it work.
04:06So back in here, to our codesnippets, and we're just going to wrap this script.
04:11It's a lot shorter than before.
04:13So just the method, title and the message, copy that, go over to our script file, and
04:19it can go pretty much anywhere.
04:21So I'll put it right here before the populateVideos, paste, save it, and I'll come back to my App,
04:29reload, and now I'll click on this dialog box here.
04:34And it lets you send a request to whomever you want, so you can choose multiple friends
04:39to send a request to.
04:41And if we go down here, you'll notice that because we have that additional functionality,
04:45we don't need to ask friends before sending your requests.
04:48So I'm going to hit Send Requests.
04:50And I'm going to switch to another browser to show you the request that comes up here,
04:56we can see it Stuart has sent you a requests and Source Foo.
05:00So very cool! You can send all kinds of things with the JavaScript API, including messages,
05:06all kinds of dialog boxes, and requests.
05:09That's really a good functionality to add to any app.
Collapse this transcript
7. Creating Achievements with the Gaming API
Creating achievements
00:00A great way to engage and keep users coming back your application is by rewarding them
00:04for performing certain tasks.
00:07The SDKs provide a simple way to do this through achievements.
00:09In this video I'm going to show you how to create and set up achievements for your app.
00:14Achievements are tied to Facebook games.
00:16So in order to do this, you must have specified that your app is a game when you set up your application.
00:21If you have any questions about how to set up your application, make sure you watch the
00:24movie on creating a container for your application.
00:26So I'm going to check to see that this application is a game.
00:29I'm going to switch over to the Facebook Developer page, and this is my App page.
00:34I'm going to go to Edit App, and I can see that my Category is defined as Games, so this is perfect.
00:39Now you also want to make sure that you've asked for the publish actions permission.
00:43And you do that in JavaScript. Let me switch back to BBEdit, and I'll show you the line
00:47that you have to look for.
00:49So if you look at this line right here, this is where I'm asking the scope of my application,
00:53and I want to make sure that I include this particular permission right here, the publish_actions
00:57permission, whenever I'm working with games.
00:59So I'm going to go ahead and close this, and I'll go to the Facebook Developer site and
01:03in here, I'm already in the JavaScript SDK, and the Achievements and Scores and Game section
01:09is a little bit hard to find from here.
01:11It's not down here anywhere, so what you have to do is go back to the Facebook Developer site.
01:16I'll just click on this right here, and then I am going to go to Build Apps on Facebook,
01:20and I'll scroll all the way down, and you can see right here that we have a Reference
01:24area for Requests, Achievements, and Scores.
01:27The section we want is Achievements, so we'll click on that.
01:30So this is the section of the developer site that discusses achievements.
01:33There are some rules for achievements.
01:36Every achievement has points associated with it.
01:38Even though we're not using them in this course, you can assign different points to go with each achievement.
01:44As you can see, every game gets a total of a thousand points across all achievements
01:48and a maximum of 1000 achievements.
01:50And one thing to know it is that a user can only get a particular achievement one time.
01:54So if you scroll down to this part right here, you can see that a Facebook achievement definition
01:59is nothing more than a simple web page.
02:02Now this page is going to have some metatags and the metatags will describe the Achievements,
02:06Title, Description, the Location of the achievement as well as an Image for the achievement, which
02:11it says it should be 50 x 50 pixels, but I like to make them bigger than that.
02:15So I usually make them 200 x 200 pixels.
02:17And you can include multiple image tags, if you want multiple images associated with the achievement.
02:23Finally, you have a section on Points and of course the application ID that administers this page.
02:29So I'm going to go ahead and close this, and I'm going to open my working folder and in
02:34my working folder I've already prepared some graphics, as well as some pages.
02:38So if you take a look at the graphics, they are just 200 x 200 pixel graphics, PNGs are
02:43fine, or JPEGs or whatever you're used to using for graphics.
02:46And you could see some of these other items are the pages that we have for our application.
02:51If you go to the Newbie page, you will see that this is a simple HTML page just with
02:55a bunch of metatags, and the metatags have descriptions of what the achievement is supposed to be like.
03:00When you create the URLs on these pages, you want to make sure that you include the https:// protocol
03:06right in front of every URL.
03:07So don't just use the http, or you will have a bunch of warnings come up in your JavaScript
03:12in the developer tools.
03:14So the URL is where this page is going to be on our web site, and it's going to be on
03:18our web site/facebook, and this one is going to be called achievement-newbie.php.
03:23Also you need the location of the image file.
03:25It's in the same place, I'll put it in the images folder, and then this is going to be
03:28called achievement-newbie, and I'm going to give this 10 points.
03:31And this is something that we're going to have to put in the app_ID, it's going to change
03:35depending on your app.
03:37So we'll have to type that in before we upload it.
03:39So what I'll do is I'll grab these three pages, and I'll bring them into BBEdit, and I need
03:44to make sure that I type in the app ID for each of these.
03:47So I'll go back into Safari, and I'm going to go into my Developer page and here's my App ID.
03:53You can also get it if you go to the App section and see that it says App ID/APIKey.
03:59So I'll copy that, back into BBEdit, I want to make sure that I paste that here, save
04:05it, paste it in all of the pages, save it, paste that in this page, save it.
04:11So now all these pages have the correct App ID.
04:14Everything else about the pages is fine.
04:16So I'm going to go ahead and close these out, and I'll close these windows out here.
04:23So we want the PHP pages, and you also want to make sure that you use PHP whenever possible
04:29when working with Facebook.
04:30I've found that in some instances, using the HTML extension causes problems.
04:34So I'm going to drag these over to just a regular Facebook folder, and my images are
04:40going to go into my images folder.
04:45You can put these wherever you want, but you want to make sure that in the pages themselves
04:48you have the link to the correct place.
04:50Now it looks like it's done.
04:51I'll open up the images folder just to make sure they're there, and you also probably
04:55want to pull up a browser and just make sure that your images are showing up when you go
04:59directly to the page--or at least that there are no errors.
05:01So I'm going to go to my page on the web, iviewsource.com/facebook/achievements-newbie.php,
05:11make sure that the page is there.
05:13So if it looks like this, and you see the title of the page, you should be fine.
05:17So if we look at the page code--so we'll go to the View menu and select View Source,
05:21you can see that all the links and all the page information is fine, and I want to just
05:26type in the other ones as well.
05:27So sharing is the other one, okay, we can see the title came up, we can look at the source code.
05:32I'll hit Command+Option+U just look at the Source Code, the same thing is View Source,
05:37and I'll type in the last one Kung Foo, Kung Foo title.
05:42The source looks good, so we are good to go.
05:45That's pretty much all it takes to create the Achievements.
05:47There are a couple of steps to get them included into your application, and we will be taking
05:52to look at those things next.
05:54So the first step to give your application the ability to handout Achievements is to
05:57create them by creating a simple HTML page.
06:01Make sure you specify an https:// URL so you won't run into a lot of security warnings.
Collapse this transcript
Debugging with the Debugger
00:00So the next step in creating our Achievements is to debug them, that's to the make sure
00:04we make no mistakes before we register them.
00:07Debugging them is done with a really nice tool Facebook provides called the Facebook Debugger tool.
00:11So let's take a look.
00:12So the Facebook Debugger tool is accessible through the Facebook Developer web site.
00:17If you look on the left of the navigation, you'll see a tools menu. I am going to click
00:21on that, and there's a bunch of tools that are pretty interesting, and you might want
00:24to take a look at all of them.
00:25What we want to work with right now is this Debugger tool.
00:28The debugger tool allows you to type in any URL Access Token or Open Graph Action ID.
00:32A lot of times when you're looking at the documentation, and it asks you to create a
00:37post or create get, you can test those things by using this debugger tool.
00:42You can also test pages like this. Let's take a look at the sourcefoo page right now.
00:47So now that your sourcefoo page looks like it is generating a warning, it's not exactly
00:51an error, so it's not a big deal, and I understand what's happening here.
00:55It says that the images that are being referenced by the page must be at least 200 pixels in both dimensions.
01:00Now must be is a little bit relative, because it shouldn't have thrown up a warning if they
01:04have to be 200 pixels, they should have thrown up an error.
01:07But I understand that this is happening because of the way Facebook is being retooled for
01:12iPad high-resolution screens.
01:14So although on some pages in Facebook you may see that the graphics have to be at least
01:1850 x 50 pixels, in other pages you're going to see some times that they need to be 200 pixels.
01:23So I know that the graphic that's in question is this monogram, and I probably should go
01:28ahead and upgrade the graphic to have a little more resolution, but it's just a warning,
01:32so I'm not to worry about it.
01:34You could see how Facebook sees your page and what metatags is able to gather from the
01:39information on your app.
01:41So this is great, and what we want to do is make sure we test out the achievement pages
01:45that we've created using this debugger tool just to make sure everything is okay.
01:49So I'm going to type in a slash, and I'll type in the URL for each of the pages.
01:53So I'm going to do the Newbie page first, I'm going to hit Enter.
01:57The Newbie page pass with flying colors, there is no errors or warnings.
02:01And I want to make sure that when I roll over this graphic down here, I see that it's an
02:06https link, see down there it just says https and a link to the graphic. That's great.
02:12The graphic is even showing up, so that's also good.
02:14I can also check down here to make sure that the graphic is going to be read from an https
02:20address or that's going to cause some security features or warnings on your browser later on.
02:25So everything checks out for this one. Let me try out the other two.
02:28So sharing, hit Enter, and that one passes perfectly as well, looks like all the data
02:33is there, my image is linking to an https link, which is great.
02:37So the last one is the kungfoo achievement and hit Enter, and that one passes as well.
02:42Check the image, here is the https link, perfect.
02:46So this is looking really good, no errors, and now we're ready to go ahead and register the achievements.
02:52The Facebook debugger tool is not just a tool for previewing achievements, you can use it
02:56to debug any type of Open Graph call.
Collapse this transcript
Getting an app token with JavaScript and PHP
00:00Whenever Facebook interacts with users or applications, certain security measures have
00:05to be put in place to make sure that communication with Facebook is secure.
00:08So far we've seen that the JavaScript SDK can easily generate an access token for users.
00:14However, for security reasons, the JavaScript SDK is unable to generate an app token.
00:19You'll need that to be able to do certain things, and in this video, I'm going to show you how to do it.
00:23The app access token is used to make sure our app has permission to perform certain functions.
00:28Registering an achievement is one of those functions.
00:31You can't create it with JavaScript because you get an app token by using a POST command
00:35with your application ID, which is public, and your app secret, which is not.
00:40You can find those by going to your app page and looking at App ID right here and the App Secret.
00:46If you somehow have your app secret in JavaScript, it would no longer be a secret because anyone
00:51could view source for your application and copy that app secret.
00:55Thankfully, PHP allows you to get the app token without revealing your app secret, so let's take a look.
01:00So I'm going to minimize Safari, I have my codesnippets.txt file already open, and I
01:06have my facebook folder open as well.
01:07So I'm going to make a new file here, right-click and create new file.
01:12I'll call this one register_achievement.php, and I'm going to open that up.
01:21So the first thing I need is just a basic HTML structure, which I have on this first
01:25part of this codesnippets. And I'll just paste that right there.
01:28It's just a basic HTML page.
01:30Next, I need to go ahead and call the JavaScript SDK, which requires me to have a div for Facebook.
01:36So I'll go to the codesnippets file and get this div of fb-root which is part of Facebook.
01:42It's how Facebook places certain messages on your page.
01:45I also have a div for my messages just in case I need some place to put stuff.
01:50So back into codesnippets, so next I need to load the JavaScript SDK.
01:54These are pretty standard functions you should have seen already.
01:57This one right here loads the SDK, and this one executes once the SDK has loaded.
02:02But I need to put in appId right there, so I'll copy this, come in here, just needs to
02:08go somewhere in the head section, and I need to switch back to Facebook and get my App ID from here.
02:15So I'll copy that and paste it right there, save that.
02:19So next, I need to come over here and actually perform the PHP part.
02:24So this is actually going to be a part-JavaScript part-PHP function here.
02:28So we are creating a variable in JavaScript called appToken, and that variable is going
02:33to get a string--you can see the little quote right there--and that string is going to be generated by PHP.
02:39Notice that it ends right there with the end of the string.
02:42So PHP is everything between this <?php, and this ?> right here, so all the stuff in between is PHP code.
02:53So let me go ahead and copy it and put it somewhere in my app.
02:55It doesn't really matter where I put it in here, so I'll put it right after these load
03:00JavaScript functions.
03:02And looks like I forgot to copy this alert, so let me go ahead and grab that and paste
03:07it right there, so that's set back to JavaScript right here.
03:09So what we have here is with PHP, we are getting the APPLICATION_ID and APPLICATION_SECRET.
03:15Let me go ahead and do that from my developer page. I'll get the App Secret from here, and
03:22we'll paste that right here.
03:23We'll need the APPLICATION_ID. We don't need to go back because we've already used that
03:27up here, so I'll just copy and paste it from there.
03:30Yours are going to be different than mine, so make sure you get yours.
03:32Here we're calling the Graph API with a URL, and that URL is going to pass along some variables
03:37which is going to be mainly my APPLICATION_ID and my APPLICATION_SECRET.
03:42Once it returns, it's going to be fed into this variable called result.
03:45Now I'm getting the data from the URL by using a function called file_get_contents.
03:50That is the method in PHP that allows you to get a file from any URL and feed it into a variable.
03:56We're getting the file from the URL that we generated up here, feeding it into the result variable.
04:02Next, what we're going to get back from PHP is actually a string that says something like
04:07appToken= and then the actual app token.
04:11So we need to just get the actual token part, not the appToken= part.
04:16So I'm using this other method in PHP called explode that will divide a string into parts
04:21and feed it into an array.
04:23So here I tell that the divisors are going to be the Equals Sign, and everything is
04:28going to go into this pieces variables.
04:31So what I'm going to do next is I'll put the second piece which will be whatever was to
04:36the right of that Equals Sign, which will be our app token.
04:40Then afterwards, I'm going to go ahead and print it out just by using an alert.
04:43So I'm going to save that, and I'm going to switch over to Safari.
04:48I'll open a new tab, and I'll just type in the URL for this page.
04:53So that's iviewsource/facebook, and I think I used an Underscore here, register_achievement. Let's see.
05:00Yeah, that is the right page.
05:02It's not showing anything, I didn't get an alert, so let's see what's happening.
05:05I want to go to the Develop menu, show the Web Inspector, and if I scroll down, I'll
05:11take a look at this warning here.
05:12Now it's not showing up because it's actually inside the JavaScript text, so this is not
05:16going to be showing to the users also in the head section, which doesn't necessarily get displayed.
05:20But I'm getting a warning from PHP.
05:23It says there's something wrong with file_get_contents.
05:25So if a look over here, it says URL file- access is disabled in the server configuration for
05:31that particular server.
05:32So that means that our server does not support to get-file-contents function, and we can
05:37fix that by creating another file called php.ini.
05:43So I'm going to make a new file here, call it php.ini, and what that file does is it
05:49will override anything in PHP that I want to be different on this folder.
05:56So it allows me to execute commands that I normally wouldn't by putting in some setup
06:01information in this file.
06:03And let's go ahead to that. I'll go ahead and open this up back into my codesnippets,
06:08and what I want to do is put in this line.
06:11This will allow the file-get-contents to work in this folder.
06:15So I'll put that there, I'll save it, and I'll switch back over to our page and hit
06:20Refresh, and it looks like it's given us the app token just fine, which is perfect.
06:26This is great! We're getting a little error.
06:28It's because we don't have the channel file--we're not calling the channel file.
06:32If you remember, when we're setting up the original document, it gives you this error
06:35if you don't set up a channel file.
06:37But we don't really need to worry about that.
06:39We're only going to use this page to register our achievements, so that will work fine.
06:44So this appToken function that we created is going to be actually quite handy.
06:48We're going to need it in other places.
06:50PHP can't by default run inside JavaScript files, so we do have a .php page in order
06:56to get the app secret.
06:57Other than that, getting an app token with PHP is almost as easy as getting anything with JavaScript.
Collapse this transcript
Registering an achievement
00:00Now that we are able to get an app token, it should be easy to register an achievement.
00:05Let's finish up our register achievement page and go ahead and register our achievements with Facebook.
00:10So here's the Achievements page on the developer site, and you could see if you scroll down
00:15a little bit that in order to create an achievement, we have to issue an HTTP POST to APP_ID/achievements
00:24with an app access_token, which we now have, and the following parameters, two parameters.
00:29Number one is the achievement which will be the URL to the achievement, and then the second
00:34one is the display_order which is the order that the achievement will display whenever
00:40it comes up on the app.
00:42Achievements with a lower display order will be displayed before achievements with a higher display order.
00:47So let's go ahead and open up our register_achievement page which we worked on in the last movie.
00:53So I'm going to go ahead and open that up, and first thing I need to do is get our registerAchievement function.
01:00This is exactly what JavaScript API requires. It sends a post command, which is right there,
01:06to the url which will need our APP_ID instead of this YOUR_APP_ID obviously, /achievements
01:12and its going to send along a couple of variables, display_order, and then when it gets a response
01:18from the server. If the response is an error, it'll print that out. If it's not an error,
01:23then it means everything went fine, and it will display a message into our div that we created before.
01:29So I'll copy this, come over here, and I don't really need to worry about where I've put
01:34that because it's going to be executed when I click on a button.
01:37So there's the registerAchievement function, and I'm going to go over here into our Facebook
01:44Developer page and get the APP_ID.
01:49I'll paste it right there, I'll Save this, and then I just need to have a button somewhere
01:56on my screen that allows people to register the achievement when they go it so underneath
02:02my div for message, I'll paste that, save that, and then I'm going to go to my register
02:10app page--and let's see, hold on. I'm probably missing something else.
02:14So what I want to do is I've got my app ID here.
02:17I want to make sure that I update the URL that this is going to register every time.
02:24So when I click on this button, it' going to pass along a URL. The URL that it will pass
02:31will be right here.
02:32So I'll need to typing something instead of achievementName and type in URLs for one of
02:37my achievements, iviewsource.com/facebook/achievement-newbie.php.
02:49Now that one, I want to make sure that a register with the lowest display_order, so you do want
02:54to make sure you update this whenever you change it to a new one.
02:58Just to make sure I'm going to copy this URL and put it in a browser. Make sure it's pointing
03:04to the right place. Looks like my Newbie Achievement title came up so I know that that's a good one.
03:08Let's see what else.
03:10I don't need to alert this anymore, so I'm going to delete that right there, save this
03:15and pull-up our register_achievement.php page.
03:19So I should see the button for register, and if I click on this, if I did everything correctly, it'll say Thanks.
03:29This achievement has been registered with Facebook.
03:31So now what I need to do is just modify this. I will change the display_order to be 2 and
03:38put in the address for sharing.php, save this, go back into Safari, refresh my page at the
03:47Register button, wait a second, looks like it got registered just fine, back into BBEdit,
03:53last one is the kungfoo, and I'll make a display_order of 3, save it again, come back in to Safari,
04:04Refresh, Register, wait for the message, and there it is.
04:08Not terribly exciting but if everything went as planned, we're now ready to display our
04:12achievements in our application.
Collapse this transcript
Showing available achievements on your page
00:00So now that we've registered our achievements, it's time to place them on our page.
00:04What we want is to put a list of the available achievements into our app.
00:09So let's take a look.
00:10So if you go to the Achievements page in the Developer web site for Facebook, you'll see
00:15a lot of information about how to work with achievements.
00:18So we've already learned how to create achievements first by creating the HTML necessary for Facebook
00:23to understand what they are. Then we learned how to register them.
00:27Now it's time to learn how to read achievements, which is in this section.
00:30Notice it says that you can get all achievements for an app by issuing an HTTP GET request
00:35to /APP_ID/achievements.
00:38So what I'm going to do is switch over to the Graph API Explorer which normally shows
00:43the information about a user, and I'm going to switch right here to Source Foo which is
00:48the application, and I will now need the APP_ID/achievements.
00:54So I'm going to go to my Apps page and do that by going to this link right here, and
00:59I'll copy the App ID, I'll switch over to the Graph API Explorer again, and I'll paste
01:05that, and I'll put in /achievements.
01:09And remember, it said that it wanted a GET request.
01:12You can also issue any other type of GET, POST, or DELETE request right there.
01:17So I'm going to hit the Enter key, and now you can see a list of achievements in JSON
01:21format for my page.
01:23So this Graph API Explorer, it's quite useful for a lot of different things.
01:26And this is one of them you can issue commands directly from here, or you can read information
01:31that you want to preview for your app.
01:33This will tell you how to get to specific things.
01:36So I could see that registering my achievement worked because there's my Newbie achievement
01:40and next is my sharing achievement, and I can see the data for the Kung Foo Achievement.
01:47Awesome! So this looks like it's ready to go.
01:49Let's switch over to BBEdit, I'm going to minimize this.
01:52Here's my codesnippets.txt file.
01:53I'm going to need to open the JavaScript file as well as the CSS document and the index file.
02:03So let me open those.
02:04All right! So obviously, the first thing I need is to have a placeholder for my information.
02:08So I'll go to codesnippets and get this placeholder set of divs right here.
02:12So it's going to have a div with an ID of achievements, and then it's going to have
02:15a section for appachievements and later on we'll have a section for userachievements.
02:20So I'm going to go over to my index file, and I'll put it right in the social plug-ins.
02:27It's not technically a social plug-in, but it doesn't really matter where these go.
02:31They could go outside the social plug-ins.
02:33So let me save that.
02:34All right! So next we need to get the App Token.
02:38Now if you watched the previous movie, you saw that we registered the App Token by creating
02:43a document that allowed you to register these.
02:46One of the things that we had to do was combine a little bit of PHP with JavaScript, and this
02:51is the function that does that.
02:53So I need to make sure that this is on an HTML page--or a PHP page rather--so that this
02:59executes before it gets sent back to the server.
03:02I want to put this right here before I call myscript.js to make sure that this appToken
03:08variable is created with the proper information.
03:11So I pasted that in there.
03:12All this does is goes to this URL and requests an app ID by feeding in the APPLICATION_ID
03:21and the APPLICATION_SECRET.
03:22Obviously, I need to get my APP_ID and my APP_SECRET, so let me go ahead and switch over.
03:28We'll get the App Secret back to BBEdit, there's the App Secret, and back to Safari, put the
03:34App Key, and minimize this, come over here, get the APP_ID.
03:40So we should be good to go.
03:42This should be fed into the appToken variable before it calls the script.
03:47Save that, and next of course, we need to add the script for getting the App Achievements.
03:53So this is exactly what we were doing in the Graph API.
03:57We set a call to the Facebook API method with the APP_ID and the achievements, and it's
04:04a get method, and we also need to make sure that we feed it in appToken which we have
04:10just received by creating this right here.
04:13Then after that, it's going to receive a response from the server if that was successful, and
04:19that response is going to be fed into a variable.
04:21So that data will be fed into this appAchievements variable.
04:25And one of them right now is just outputting it to the console to make sure that it works just fine.
04:30So I'm going to grab this code, I'll go back into my JavaScript document, and I want to
04:35put this somewhere after it initializes things.
04:39So here's where it populates the stories, here's where it finds out if a user is logged
04:44in, and right here after I insert the welcome with the user's name, I'm going to paste that.
04:51So after it initializes, we verify that this is a user from Facebook and they're logged
04:56in, then I'm going to call the Facebook API.
04:58I need to make sure I update this APP_ID, it should be somewhere in here.
05:02There it is up there, so I'll just copy it and paste it right there.
05:05I'll save this, and let's switch back over to our app, and I'll refresh.
05:10Okay, so we shouldn't really see anything, but we should be able to go to the console
05:15and take a look at the object that it received back.
05:18So I'm going to show Web Inspector here, and I'm going to go to the console, and we'll see an error.
05:23So you could see that it actually received three objects. I must have another console
05:28output here, so I can delete that if I want to.
05:30But I see the three different objects right there.
05:32And if I open them up, I notice that this is an object and the description is about
05:37the Newbie achievement here, here's the title.
05:39So here's all the data for the Newbie achievement.
05:43I'll close this one out.
05:44Here must be the data for the next achievement which is the sharing, and sure enough, there's
05:48all the data, and finally, the Kung Foo Achievement.
05:51So all my information came in just fine.
05:53It's just matter of accessing the data, so I know that I can--if I understand the JSON
06:00format, I can output whichever parts of these different objects I need.
06:05So I'll switch back over to BBEdit and go back to codesnippets, and here I have a script
06:10just a replacement script that gets the App Achievements.
06:13So it's pretty much the same as this one up here, but after App Achievements, I have this
06:19section right there, and then I output everything.
06:22So this section right here creates a variable called output.
06:24It feeds it the label of that section, then a little bit of a description, and then here
06:31it goes through and gets the Available Achievements.
06:34So it's going to go--this is jQuery that goes through each one of the achievements, and
06:39it generates output for each one of those.
06:42So I don't want to copy this entire thing and have to reput the APP_ID, so I'll just
06:45copy this section, go back into my JavaScript, and in the place where I'm just outputting
06:52stuff to the console, I'll paste that and let me indent it a little bit so it looks a little better.
07:00So this should output all of the achievements into the appachievements div that I created as a placeholder.
07:07So let's save that, switch over to Safari, let's close this out, and refresh.
07:13Okay, so it looks like it is publishing the achievements. There's the Achievements heading
07:18that I created, here is the paragraph, and after that it's printing out the Available
07:23Achievements--here's the heading for that, here's the achievements.
07:26Right now, the achievements are 200 by 200 pixels, but I can see that there's the Newbie
07:30Achievement with the description, there's the graphic for the Sharing Achievement, and
07:35finally the Kung Foo Achievement.
07:37Obviously, this is really annoying that it's so large, so we need to fix that, and we'll do that with CSS.
07:43So we'll get the last bit of code from the codesnippets, and we have some styles here
07:47for the achievements.
07:49So I'll scroll all the way down, copy that, paste it at the very bottom of my CSS file,
07:56save that, let me switch over and just show you how it looks, and then I'll go through
08:00and explain really quick how I did the CSS.
08:03See it's loading the achievements, and now they look really nice right next to each other.
08:07If you scroll over each of these achievements, you'll see that it has a little bit of a pop-up.
08:12I did that all with CSS.
08:13So let's take a look a look at the CSS and see how we achieved that.
08:16I'll see if I can open that and leave it to the site because it makes sense when we're
08:20looking at the code and what it's doing side by side.
08:23So achievements, the CSS starts right here just a little bit of padding to the bottom,
08:29so it has a little bit of room at the bottom.
08:31The headline level 3 is going to be formatted in this manner just a slight color here. Nothing--nothing major.
08:38So that's this Available Achievements, I believe.
08:41And then the h4s are going to be a little bit smaller, so the h4s will be these ones
08:46that pop up when I roll over these elements.
08:50Then we keep on going, and we have a paragraph.
08:52This makes the paragraphs right here a little bit smaller. And the images, it sets the width
08:57of the images to 50 pixels.
08:58Remember that Facebook now requires 200 by 200 pixel images.
09:02I think it's because of the iPad's high-resolution screen, so it just resizes it to be 50 pixels wide.
09:09Next, we go through and add some other styles for paragraphs, let's see, article.
09:15So each one of these--actually, so h3 and h4 are the achievements for here, and then
09:21later on when I do user achievements, you'll actually see an h4.
09:25And inside the articles for the achievements, you'll see an h3 which is actually what happens when I roll over.
09:31The paragraph is right underneath that.
09:33And then the trick that I do to pop them over is right here within these two styles.
09:39So I set up the article itself, so each one of these groups of data has an article div.
09:44So I set the position of that to relative.
09:46That allows me to then set the text inside them to be positioned absolute, so the text
09:52will be positioned relative to the last relative container which is its parent, which is this
09:59article thing right here.
10:01And because it's absolutely positioned, then I can say okay, how far away from the original
10:05element do I want to position the rollover text.
10:09And that's going to be positioned 80 pixels to the right, -20 pixels from the top, and
10:13the width of that group of text is going to be 200.
10:16So that's what you see when you roll over like this.
10:19The text is aligned to the right, as you can see, opacity 90%, so it shows a little bit
10:24of the background through, and the Z index 100 so that it stays on top of everything else.
10:29And of course, this is not displaying when you see the screen first, but it will display
10:35whenever you roll over. That's what this part of it does.
10:38It says, when I roll over each article, which is each one of the elements, then the text
10:44element will display as block, which means it's just visible like that.
10:48So pretty cool! I think you're getting the hang of how to access different parts of the
10:52Facebook API, first by previewing them through the Graph API Explorer, but then by just calling
10:58different things with the JavaScript SDK and accessing anything you want from Facebook's Open Graph.
Collapse this transcript
Assigning and displaying achievements
00:00We can see the achievements our app offers users. Let's take a look at how you can assign
00:05an achievement to a user and how to display the achievements that a user has received.
00:10So let's go to the Developer web site and the Achievement section, and at the very top
00:15of this window, you could see how you can do things with Achievements for the app itself.
00:20So how to create App Achievements, how to read them, and how to delete them.
00:23You need to scroll down here to this section called Managing achievements for a user, if
00:28you want to know how to do things for users.
00:31So of course we can read user achievements by issuing a get request to /USER_ID/achievements,
00:37we need an users access_token and publish_actions.
00:40Notice that it gives you a JSON object with some data. One of things you should see here
00:45is that it doesn't have data about the description of the achievement or the image of the achievement,
00:51so it only sends back the ID URL type and title, it's not going to send the description or the image.
00:56So if want to display those, we need to find a way of doing that.
00:59So also if you want to create an achievement, you need to issue a post to /USER_ID/achievements
01:05with an app access_token.
01:07So this is not a user access_token, it's an app access_token.
01:10And you can of course delete an Achievement if you want to.
01:13So I am going to go over to my codesnippets file, and I'm going to open the JavaScript and the CSS.
01:20So the first thing I need to do is figure out how to do achievements or how to list
01:23achievements from a user.
01:25So I will go to codesnippets, and I will get the achievements from this user, so I will
01:31copy that, I will paste it right before this response.status section right here, and I
01:35will indent a little bit.
01:37So this queries the Facebook API. Feeding it the user ID and /achievements issues a get request.
01:45And if it's gets a response, then it's going to tell you that it's going to either actually
01:49output to the console the results of user achievements, or it's is going to tell you,
01:54you haven't earned any achievements.
01:56Since we haven't earned any achievements, we are probably going to get this message.
01:59So I'll save this, and I'll go back on to Facebook and Refresh this right here.
02:05So I should see a message that says, sorry you have not earned any achievements yet.
02:09And that's pretty good, but now we need to figure out how to give a user an achievement.
02:13I want to give everybody that comes to the site a Newbie achievement, so I will go to
02:17codesnippets and just copy this code right here.
02:20So this is going to create a post to the achievements, to userid/achievements with an access_token
02:26that is the app_token that we already have in storage in this variable called app_token.
02:32And it's going to post the achievement name which is the URL of the achievement we want to add.
02:37So I will copy that, go back into myscript.js. I want to put that right here, right after
02:42it realizes that a user has not earned any achievements.
02:45Let me go ahead and indent that a little bit.
02:48And I am going to go ahead and save it.
02:50So now, if the user hasn't earned any achievements then it's going to tell me, but then immediately
02:55give me the Newbie achievement.
02:58So I'll refresh this page, it should still tell me sorry you haven't earn any achievements
03:02but now the user should actually have the Newbie achievement.
03:06So how do we know that?
03:07We will go to the Graph API Explorer, so here's the user ID already.
03:11We need to just type in achievements and--oops! Before we do that we need to make sure we
03:17are in the right app.
03:19So I will do that /achievements, hit Refresh or hit Return or Enter, and you could see
03:25that the achievement has been assigned, so if I go back into the application, and I load
03:30it up once again, I will see a rather to see this thing that says undefined which is fine right now.
03:35The user doesn't really have any achievements and haven't really done anything to display the achievement.
03:42So I am going to go back into the Graph API Explorer, and I'll show you how to delete
03:46an achievement just in case you want to reset things, you need to go here and issue a delete
03:53Command to /achievements, and then you need to give it the achievement= and just copy
04:00the achievement that you want to delete, the URL is right here.
04:03So I could just do that copy, paste, hit Submit, and it should come back with a value of true.
04:09So that means that this user no longer has achievements, so we could test that by going
04:14to get--just go to /achievements here, hit Return, and you should see there's no data here, so perfect.
04:23So let's go back into the app, and we will Refresh.
04:27So I should say, once again sorry you haven't earned any achievements, and if we Refresh
04:33it should just give us that blank undefined.
04:36The user has been given an achievement now, so that's great.
04:39Okay, so let's see what else we could do here. Let's go back to the codesnippets.
04:42So here is how you would check for a specific achievement.
04:45Once a user has an achievement, you can test for that achievement by going through each
04:51one of the user achievements and then doing something with the achievement.
04:55So let me grab this code right here, and I'll paste it, I will come over here.
04:59So if the user has achievements, then what I want to do is execute this bunch of stuff
05:07and what I'm doing here it is I'm going to check the user achievements that I received
05:12from the server, from Facebook.
05:15I am going to the check to see if we have a variable here to see if it has that specific achievement
05:19and then go through the list of user achievements here.
05:22If the user achievement title comes back to be Newbie achievement.
05:27Then it's going to set this variable has newbie to true, and it's going to stop going through
05:32all the different options.
05:34So when I get over here, I say okay so if this user doesn't have a Newbie achievement,
05:38then I want to go ahead and apply it to him so the same thing is down here.
05:42Now this is not really practical for maybe the Newbie achievement, but this is exactly how
05:47you would deal with checking for other achievements.
05:50So you would check to see if they have a specific achievement, if they have it then you could
05:55set a variable, and then if they don't have it you can give it to them.
05:58So not exactly what you would do here, practically, but it's the process that you would go through
06:03for checking different achievements.
06:06So I will save that, and I will go back to my codesnippets.
06:09So the next thing is to actually get the achievements or actually print the achievements for the user.
06:15So I am going to grab this piece of code right here, and all it's going to do is just output
06:19the user achievements to the Console, so we kind of preview them.
06:23So I will come back over here, and I'll put that right in here.
06:28So I'm copying too much here. All I need to do is--this is already up here so I don't need that.
06:35So all I need to do is output if they do have achievements, output to the console just the
06:42object with all the achievements.
06:44So let's save that and go back into our app here.
06:47Okay, so we really shouldn't see any achievements because we see the title right now, your achievements,
06:52but if we go to the Console.
06:54So we'll go to Develop > Show Web Inspector, and here we see the object. The object has
06:59the achievement right here which is the Newbie achievement.
07:02So I know that this is working, and this is also one of things you can do to test to see
07:08there's any achievements, and this gives you the structure of how to get to them.
07:12So using the Console is really handy for this type of functions.
07:16Now that we know that we have an achievement we have verified it by looking at the console,
07:21then we can actually go through the process of outputting the achievement.
07:24So let's go back into our codesnippets, remember you don't really need this line, you just
07:28go from this output line, find this line and replace it.
07:32So here's a tricky part with outputting the Achievements.
07:34I want to output the achievements with the icon as well as a description of the achievements,
07:40but the information that I get back from the console--if you remember--doesn't have that in it.
07:45So if I were to pull up the console here, Show Web Inspector and look at this object
07:50I am going to see that I get the achievement back, and I get the title of the achievement,
07:55I get the URL of the achievement, but I don't get the name or description of the achievement.
07:59So if I want to do that because I really do I want to tell the users hey you've got this
08:04achievement, here's what it's about.
08:05If I want to do that I am going to need to do something else.
08:08So what I'm doing here is I am going through each application achievement and getting information
08:14from each one of those, and then checking to see if the current user achievement title
08:19matches this app achievement title.
08:22So I am going to go through each one of the app achievements, and seeing of the title of
08:27the achievement that I just got back from the list of user achievement matches any of
08:32the titles in the achievement that this app lets users have.
08:36If it does, then it's going to output everything about the achievement, so the image URL of
08:43this appObject which is the app achievement list, and the title as well as the description.
08:50So this'll work really nice, we will grab this and go back into myscript.
08:55So instead of outputting them to the Console, then I can just paste them in here, and let's
09:04take a look at this little bit further.
09:06So I have the user achievements, I need the title, and it's not just the title because
09:12it does get me the title.
09:13But I need the description and the location of the image.
09:16I can only get that by looking at the app achievements.
09:20Now up here I was able to get all of the app achievements, and then I put them in this appAchievements variable.
09:25So down here looking at that JSON data and going through each one of those items in order
09:30to print out all the information that I need from the achievements.
09:34So let me go ahead and save that, go back into our application and Refresh.
09:40So we should now be able to see the achievement that this user has, which is really just the Newbie achievement.
09:46And this looks great. I would like it to format it a little the better.
09:49I like how this is kind of you know stacked up next to the image, so I am going to move
09:53these over here, and I need to do that with some CSS which should be pretty simple CSS.
09:59So let's go back into our codesnippets, here's the Styles. Because I created so many styles
10:03for the apps, the styles for the userAchievements are going to be just a slight modification margin bottom.
10:10And then I am bringing this display of the images inline.
10:14So I will copy that and just add that to the very end of the CSS, Save it, refresh my page,
10:20scroll down a little bit.
10:22And there is the Newbie Achievement.
10:24Looking good, and if I had any other achievements they would just go underneath that.
10:28And that's how you add Achievements to a specific user.
10:31Really everything in Facebook apps is done with this process, we have taken a look at
10:37how to retrieve data from the Facebook objects, the Facebook API, and then you know issuing
10:42different commands, whether it's a post-command or Delete command. The commands are even going
10:47to be the same with the different types of things that you want to do.
10:50So learning how to manage the Facebook API is going to be really beneficial to you creating awesome apps.
Collapse this transcript
Conclusion
Next steps
00:00So I hope you've enjoyed this course.
00:02If you like Facebook app development, here are some other places you might enjoy.
00:06So you'll definitely be spending a lot of time in the developer web site.
00:09So I wanted to point out a couple of places in the developer site you may want to take a look.
00:13You definitely want to go to the Tools section.
00:15There's a few tools I didn't talk about, one of them is the Test User API.
00:19So here you can create users for testing out your application.
00:23You may want to dig into some of the additional SDKs, we've covered the JavaScript SDK but
00:28there's also a PHP SDK.
00:30We covered in a couple of places how to access the SDK through PHP.
00:34In some places this is a better solution than the JavaScript SDK, especially when working
00:39with something like SQL databases.
00:41There's a couple of other SDKs you may want to checkout, including the iOS and Android SDK as well.
00:46If you need to help with Facebook, the best place to get it it's by going to the Support
00:50section of the web site at this URL... or visiting facebook.stackoverflow.com.
00:55This is the official place where you can ask questions and find specific answers to your questions.
01:00So once again, this is Ray Villalobos, and have fun building the next great Facebook application.
Collapse this transcript


Suggested courses to watch next:

JavaScript Essential Training (5h 31m)
Simon Allardice

XHTML and HTML Essential Training (4h 44m)
Bill Weinman


CSS Fundamentals (3h 14m)
James Williamson


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,718 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked