Using Facebook with NS Basic/App Studio

It’s easy to get Facebook information into your NS Basic/App Studio program. Facebook has an API that is easy to access. Send a message to it, and it will return the information you requested.

Here’s how to request information on Mark Zuckerberg:

$.getJSON("https://graph.facebook.com/MarkZuckerberg", "callback=?", gotJSON)

A bit of explanation is in order.
The $.getJSON() function is in an external library named JQuery. JQuery is a very popular library for JavaScript developers. Fortunately, it can be called from NS Basic/App Studio quite easily. Simply put the following into your project’s “extraheaders” property:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js">
</script>

(You could also download the file jquery.min.js into your project’s directory and add it to your manifest.)

The getJSON call has 3 parameters: the URL to get information from, any parameters to send to the URL, and the return function. In this case, we want to get information about Mark Zuckerberg.

When it comes back, we want the function gotJSON to be called on our program. This is an asynchronous operation: your program can continue to run while we wait for graph.facebook.com to reply. When it does, our function is called.

Here is what gotJSON looks like:

Function gotJSON(data)
  If data["error"] Then
    txtData.value = data.error.message
  Else
    txtData.value = ""
    For Each index, item in data
      txtData.value = txtData.value & index & ": " & item & vbCRLF
    Next
  End If
End Function

The data is returned as a JSON object, which ) is a lightweight text-based open standard designed for human-readable data interchange. What’s left to do is to display the object. Taking advantage of For Each’s ability to iterate both the names and the values of each member of the object, we output one line for each member. Here is the output:
facebook

For more information on the Facebook API, see http://developers.facebook.com/docs/reference/api/.