CS6320:  SW Engineering of Web Based Systems

 

Facebook: Getting User Social Data +++

Currently this is implemented in Facebook in their "Graph API". You can access to much of this data given the basic information user authentication has been performed and you have an OAuth token for a user.

 

First, some basic concepts.....

SOCIAL Graph =

  • every person a node,
  • every event a node,
  • each piece of content (photo) is a node
  • each facebook "page" is a node
  • connections in graph show relationships between nodes

 

How to access a Node in the graph

OPTION 1 - with ID

  1. Every object in the social graph has a unique ID.
  2. You can access the properties of an object by requesting https://graph.facebook.com/ID

Example

For example, the official page for the Facebook Platform has id 19292868552, so you can fetch the object at https://graph.facebook.com/19292868552 ......this is what is returned:

Output JSON formatted

{
   "name": "Facebook Platform",
   "type": "page",
   "website": "http://developers.facebook.com",
   "username": "platform",
   "founded": "May 2007",
   "company_overview": "Facebook Platform enables anyone to build...",
   "mission": "To make the web more open and social.",
   "products": "Facebook Application Programming Interface (API)...",
   "fan_count": 449921,
   "id": 19292868552,
   "category": "Technology"
}

OPTION 2: Use name (for people and pages)

Examples

 


How to Access "Connections" (social data) about YOU (user = me)

The connections supported for people and pages include: TRY them out and see the output

(NOTE: if the following links don't use it may mean the access tokens have expired ...
go to facebook page http://developers.facebook.com/docs/reference/api/ to try)

 

How to access "Connections" (social data) about a user of your program (user_id)

To get additional information about a user, you must first get their permission via the authentication process discussed before. At a high level, you need to get an access token (OAuth) for the Facebook user. After you obtain the access token for the user, you can perform authorized requests on behalf of that user by including the access token in your Graph API requests:

IMPORTANT: 220439 must be substitued with user_id you get from your signed_request data that is automatically sent to your program (after authentication) and on each call to your program. See php code to get the user_id.that supports authentication if not completed. Also, the access token you got from user authentication must be given.

THIS will get you EVERYTHING you have access to about a user (you can as shown above for me ask for specific info)


https://graph.facebook.com/220439?access_token=...  


Example (not working)
https://graph.facebook.com/220439?access_token=2227470867|2.TDuzsqFuRcDIyKZ_G0fMRg__.3600.1304542800.0-520117706|BRHvQXtPyVyrpPI2XUseiPOLdsg

Here is an example output for a user (Facebook provided):

{
   "id": "220439",
   "name": "Bret Taylor",
   "first_name": "Bret",
   "last_name": "Taylor",
   "link": "http://www.facebook.com/btaylor",
   "username": "btaylor",
   "hometown": {
      "id": "108363292521622",
      "name": "Oakland, California"
   },
   "location": {
      "id": "109650795719651",
      "name": "Los Gatos, California"
   },
   "work": [
      {
         "employer": {
            "id": "20531316728",
            "name": "Facebook"
         },
         "position": {
            "id": "148305368513781",
            "name": "CTO"
         },
         "with": [
            {
               "id": "4",
               "name": "Mark Zuckerberg"
            },
            {
               "id": "1586010043",
               "name": "Zach Rait"
            }
         ],
         "start_date": "2009-08",
         "end_date": "0000-00",
         "projects": [
            {
               "id": "153823678006564",
               "name": "Open Graph",
               "with": [
                  {
                     "id": "4",
                     "name": "Mark Zuckerberg"
                  }
               ],
               "from": {
                  "id": "4",
                  "name": "Mark Zuckerberg"
               }
            }
         ]
      },
      {
         "employer": {
            "id": "99073561945",
            "name": "FriendFeed"
         },
         "location": {
            "id": "108212625870265",
            "name": "Mountain View, California"
         },
         "position": {
            "id": "116320241753504",
            "name": "Founder & CEO"
         },
         "with": [
            {
               "id": "581903346",
               "name": "Goutham Patnaik"
            },
            {
               "id": "15500414",
               "name": "Benjamin Golub"
            },
            {
               "id": "223020",
               "name": "Tudor Bosman"
            },
            {
               "id": "1214835",
               "name": "Dan Hsiao"
            },
            {
               "id": "836701",
               "name": "Casey Maloney Rosales Muller"
            },
            {
               "id": "4809535",
               "name": "Kevin Fox"
            },
            {
               "id": "500039935",
               "name": "Gary Burd"
            },
            {
               "id": "207830",
               "name": "James Norris"
            },
            {
               "id": "580246416",
               "name": "Sanjeev Singh"
            },
            {
               "id": "672745547",
               "name": "Paul Buchheit"
            }
         ],
         "start_date": "2007-10",
         "end_date": "2009-08"
      },
      {
         "employer": {
            "id": "86860316161",
            "name": "Benchmark Capital"
         },
         "location": {
            "id": "104048449631599",
            "name": "Menlo Park, California"
         },
         "position": {
            "id": "104098916294662",
            "name": "Entrepreneur In Residence"
         },
         "with": [
            {
               "id": "210560",
               "name": "Peter Fenton"
            },
            {
               "id": "207830",
               "name": "James Norris"
            }
         ],
         "start_date": "2007-06",
         "end_date": "2007-09"
      },
      {
         "employer": {
            "id": "104958162837",
            "name": "Google"
         },
         "position": {
            "id": "103146903077097",
            "name": "Group Product Manager"
         },
         "start_date": "2003-03",
         "end_date": "2007-06",
         "projects": [
            {
               "id": "162849873739882",
               "name": "Google Maps",
               "with": [
                  {
                     "id": "207830",
                     "name": "James Norris"
                  },
                  {
                     "id": "767560056",
                     "name": "Lars Eilstrup Rasmussen"
                  },
                  {
                     "id": "734236612",
                     "name": "Jens E. Rasmussen"
                  }
               ]
            }
         ]
      }
   ],
   "favorite_teams": [
      {
         "id": "112437965439134",
         "name": "Stanford Cardinal"
      },
      {
         "id": "106533819377582",
         "name": "San Francisco 49ers"
      },
      {
         "id": "109892325707088",
         "name": "A's"
      }
   ],
   "favorite_athletes": [
      {
         "id": "106056612759445",
         "name": "Toby Gerhart"
      },
      {
         "id": "112417838771364",
         "name": "Joe Montana"
      },
      {
         "id": "132355330134939",
         "name": "Andrew Luck"
      }
   ],
   "inspirational_people": [
      {
         "id": "103864656318694",
         "name": "Linus Torvalds"
      },
      {
         "id": "113529011990795",
         "name": "Steve Jobs"
      }
   ],
   "education": [
      {
         "school": {
            "id": "112075895485567",
            "name": "Acalanes High"
         },
         "type": "High School"
      },
      {
         "school": {
            "id": "6192688417",
            "name": "Stanford University"
         },
         "concentration": [
            {
               "id": "192578844099494",
               "name": "Computer Science"
            }
         ],
         "type": "College"
      },
      {
         "school": {
            "id": "6192688417",
            "name": "Stanford University"
         },
         "degree": {
            "id": "193640483997198",
            "name": "MS"
         },
         "concentration": [
            {
               "id": "192578844099494",
               "name": "Computer Science"
            }
         ],
         "type": "Graduate School"
      }
   ],
   "gender": "male",
   "locale": "en_US",
   "languages": [
      {
         "id": "106059522759137",
         "name": "English"
      },
      {
         "id": "110343528993409",
         "name": "Spanish"
      }
   ],
   "updated_time": "2011-05-02T05:06:10+0000"
}

 

How to execute

PHP example

https://graph.facebook.com/220439?access_token=...

<?php       
$app_id = YOUR_APP_ID;   //this you need to change
$app_secret = YOUR_APP_SECRET;   //this you need to change  
$access_token = THE_ACCESS_TOKEN  //this you get after you have done authentication  
$user_id = THE_USER_ID;  //this you get after you have done authentication

//AUTHENTICATION MUST BE DONE FIRST
//see previous code samples............here is an example

     $app_id = "YOUR_APP_ID";
      $canvas_page = "YOUR_CANVAS_PAGE_URL";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($canvas_page);
    $signed_request = $_REQUEST["signed_request"]; // this will be passed automatically (see above) to                                                                                      
                                                      // your app when user invokes
     list($encoded_sig, $payload) = explode('.', $signed_request, 2);
     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
     if (empty($data["user_id"])) { // if no data given about user in signed_request info then ask user                                                   // to authorize your app to get it
              // facebook will return to the canvas_page as a "call back " url              //and pass the new signed_request             echo("<script> top.location.href='" . $auth_url . "'</script>");      } else {            $access_token = $data["oauth_token"]; //grab the oauth token sent      } //ASK FOR INFORMATION ABOUT USER (assumes authentication successful) $user_getinfo_url = "https://graph.facebook.com/".$user_id . "access_token?" .$access_token $user_data = file_get_contents($user_getinfo_url); echo("User Data recieved:", $user_data); //process $user_data as you need....more code here!!! ?>

 

See Graphs API reference on facebook ( ) for more

 

You can also publish to Facebook's Social Graph

Example of Publishing

You can publish to the Facebook graph by issuing HTTP POST requests to the appropriate connection URLs, using an access token.

For example, you can post a new wall post on Arjun's wall by issuing a POST request to https://graph.facebook.com/arjun/feed

in PHP using curl library

curl -F 'access_token=...' \
      -F 'message=Hello, Arjun. I like this new API.' \
      https://graph.facebook.com/arjun/feed

Most write operations require extended permissions for the active user. See the authentication guide for details on how you can request extended permissions from the user during the authentication step.
We support writing the following types of objects:


Method

Description

Arguments

/PROFILE_ID/feed

Publish a new post on the given profile's feed/wall

message, picture, link, name, caption, description, source

/OBJECT_ID/comments

Comment on the given object (if it has a /comments connection)

message

/OBJECT_ID/likes

Like the given object (if it has a /likes connection)

none

/PROFILE_ID/notes

Publish a note on the given profile

message, subject

/PROFILE_ID/links

Publish a link on the given profile

link, message, picture, name, caption, description

/PROFILE_ID/events

Create an event

name, start_time, end_time

/EVENT_ID/attending

RSVP "attending" to the given event

none

/EVENT_ID/maybe

RSVP "maybe" to the given event

none

/EVENT_ID/declined

RSVP "declined" to the given event

none

/PROFILE_ID/albums

Create an album

name, message

/ALBUM_ID/photos

Upload a photo to an album

message, source (multipart/form-data)

/PROFILE_ID/checkins

Create a checkin at a location represented by a Page

coordinates, p

 

© Lynne Grewe