Purpose
The purpose of this project is to discuss some of the circumstances around using the Graph API. If you would like to see some full-fledged working examples of using and not using the Graph API to retrieve wall posts, see:
Using JSON-based Graph API to get Wall Posts
Using a crawler to get Wall Posts
Using the Javascript SDK to get Facebook User info into Your Webpage
Introduction
In order to retrieve wall posts from Facebook with the JSON-based Graph API, there are a few things that you need to have in place before-hand.
Graph API Access Token
If you perform programmatic interaction with Facebook you'll need an access token which means you first need an "App" and an "App ID". Here's how you do that:
- Login into Facebook with an account
- Create a new "app" at https://developers.facebook.com/apps/
- You will need to provide a unique application name and namespace which can be virtually whatever you want
- Provide a mobile number or credit card; if mobile number you will receive a text with the confirmation code
- You will be issued (1) application ID, (2) application secret
- Proceed to get the access token created (https://developers.facebook.com/docs/authentication/applications/)
- Alternatively enter the following URL substituting "YOUR_APP_ID" and "YOUR_APP_SECRET" with your application ID and application secret
- https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials
- The response should contain the tag "access_token" with the value being the access token
Group ID No Longer Needed
Facebook has performed some updates so you no longer need to provide the ID of the wall you are getting wall posts from; that is, you can use the group name as you see it in the URL. However, the group ID number can still be used. Here's how you do that:
- Method #1: Go to the Graph API Explorer at https://developers.facebook.com/tools/explorer
- Method #1: Under the tab "Graph API" enter the name of the public group wall in the URL bar and submit
- Method #2: Enter the following URL substituting "PUBLIC_WALL_NAME" with the name of the public group wall
- https://developers.facebook.com/tools/explorer?method=GET&path=PUBLIC_WALL_NAME
Get Wall Post JSON Data
The next component that you need to have is a method with which to get wall posts (in JSON format) using your access token. The URL to use, looks like:
https://graph.facebook.com/GROUP-WALL-NAME-OR-ID/posts?access_token=YOUR-ACCESS-TOKEN
Since the example mentioned earlier ("Using JSON-based Graph API to get Wall Posts") uses ASP.NET, I'll use some ASP.NET below which serves as the function you call "jsonFetch()" in order to get the wall posts:
Public Class jsonResponse
Private _data() As data
Public Property data() As data()
Get
Return _data
End Get
Set(ByVal value As data())
_data = value
End Set
End Property
End Class
Public Class data
...
End Class
Public jsonData As New jsonResponse
Public Function jsonFetch(ByVal URL As String, ByVal crawlAgent As String, ByVal crawlMethod As String) As String
Dim buffSize As Integer = 2048
Dim crawlOutput As String = String.Empty
Dim crawlError As String = String.Empty
Try
Dim myRequest As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
myRequest.UserAgent = crawlAgent
myRequest.Method = crawlMethod
Dim myResponse As HttpWebResponse = CType(myRequest.GetResponse(), HttpWebResponse)
Dim streamResponse As Stream = myResponse.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim readBuff(buffSize) As [Char]
Dim lineStep As Integer = streamRead.Read(readBuff, 0, buffSize)
While lineStep > 0
Dim outputData As New [String](readBuff, 0, lineStep)
crawlOutput = crawlOutput & outputData
lineStep = streamRead.Read(readBuff, 0, buffSize)
End While
streamRead.Close()
streamResponse.Close()
myResponse.Close()
Catch ex As Exception
crawlError = "[ERROR 2] " & ex.Message.ToString()
End Try
if crawlError.length = 0 Then
crawlError = "success"
crawlOutput = crawlOutput.Replace(""""":", """unknown"":")
Try
Dim jsonDeserializer As New System.Web.Script.Serialization.JavaScriptSerializer
jsonData = jsonDeserializer.Deserialize(Of jsonResponse)(crawlOutput)
Catch exb As Exception
crawlError = "[ERROR 3] " & exb.Message.ToString()
End Try
End if
Return (crawlError)
End Function
Classes, Strucutures, LINQ, Other?
Once you can successfully get the JSON response from Facebook containing wall posts, there are a variety of ways with which you can parse that data. You could do it manually, use a 3rd party JSON tool, use classes, structures or even LINQ. Since I like classes, I actually use a class-based schema that shows how simple it is to use classes, nested classes (classes inside of classes) with JSON data. This sort of knowledge could make it handy in implimentation with other systems that are JSON-based but not from Facebook.
Ready For a Full-Fledged Example?
If you are ready to check out a full-fledged example of using the JSON-based Graph API with ASP.NET and using ASP.NET classes, in order to get wall posts, click the link below. You'll be able to see how easy the entire process is.
Using JSON-based Graph API to get Wall Posts