Facebook Graph API Development with Flash
上QQ阅读APP看书,第一时间看更新

Time for action - loading photos from an album

However, as we've established, each object inside data is a Graph Object in its own right. Let's take a look at the Packt Books album, with id 307932939458, by browsing to https://graph.facebook.com/307932939458?metadata=1:

{
"id": "307932939458",
"from": {
"name": "Packt Publishing",
"category": "Products_other",
"id": "204603129458"
},
"name": "Books",
"description": "Packt Books",
"link": "http://www.facebook.com/ album.php?aid=180619&id=204603129458",
"count": 32,
"type": "album",
"created_time": "2010-02-04T12:32:17+0000",
"updated_time": "2010-03-18T16:08:42+0000",
"metadata": {
"connections": {
"photos": "https://graph.facebook.com/307932939458/photos",
"likes": "https://graph.facebook.com/307932939458/likes",
"comments": "https://graph.facebook.com/307932939458/ comments"
},
}
}

This time, the metadata gives us the information we need. The photos are linked to the Album Graph Object through a connection called "photos".

Run your SWF and load the albums connection again. In the Renderer, scroll to the Graph Object whose name is Books, and click on Pop Out. Then, expand the Connections box of the Books Renderer, and click on photos.

Time for action - loading photos from an album

What just happened?

When we were only considering the "posts" connection, our graph was very simple; there was a single connection between the Page and everything related to it:

What just happened?

Now that we've introduced albums, it's more complicated:

What just happened?

We now have to traverse two levels of connection to get from the Page to the objects we're looking for.

The connections don't stop there, though. Both Albums and Photos can be connected to Comments, too:

What just happened?

Plus, every comment has a property called from that connects it to the user that posted it. A user can also be "tagged" as appearing in a photo, which connects the photo and the user, as well:

What just happened?

The diagram is looking more and more complex (and the similarities to the mathematical graph drawn earlier are now clear). Of course, now that people are involved, the number of connections gets ridiculous. Users can be connected to any other object, either by being friends with another User, by being tagged in a Photo, Video, or Note, by posting a Comment, Link or other item, or by clicking Like on any other element in Facebook.

What just happened?

If you start with one Page and keep going through all the objects connected to it, and then all the objects connected to those, and so on, you can cover huge numbers of nodes without having to start again with a new Page.

The power of the graph lies in its flexibility. Every type of Graph Object has the same basic structure as every other type of Graph Object—with the exception of Graph Lists (which contain arrays of Graph Objects). That's why our Graph Object Renderer can easily display any kind of Graph Object.

Also, have you noticed that it's not just comments that have a from property? Albums do, too, and so do individual photos, and pretty much every type of object that isn't a Page or a User. This means you can start with any object, find its creator, and traverse outwards across the graph from there.

The Graph model has implications for privacy. Suppose that, if we were granted access to information about a Page, we were also allowed to access information about any object connected to that Page. Well, then we could go from the Page to:

  • An Album posted by the Page, to
  • A Photo in that Album, to
  • A User tagged in that Photo, to
  • That User's list of wall posts, to
  • A Comment made on a post by a friend of the first User, to
  • The User that posted the Comment, to
  • A TV Show that this User Likes, to
  • A Link posted on the Page for that TV Show, to
  • The User who posted that link

and so on. It's no surprise, then, that Facebook uses a more detailed set of rules to determine both what a user can access, and what an app can access on behalf of a user. We'll look at these rules in the next chapter.