bakedproject.com Blog Not just another WordPress weblog

16Jun/120

Update Facebook test user password – making batch request with python

A few weeks ago I wrote a post about how to make a batch API call to Facebook to generate MySQL statements. I am going to do a simple Python script to make a batch call to Facebook and update test user's password to "123123"(I just find it easier to type).

Following script really does not check if the batch call being made is over the 50 calls limit, something I will be updating shortly.

9May/120

Use batch request to grab and save Facebook test user information

Facebook opened up their platform for application developers in 2007 and became one of the most accessed platforms via an API set. Over last few years functional modules were added both to the core API and developers tools offered by the social network giant. One of these amazing developer tool is the test users suite. Other than a few platform limitations and a few reported bugs, it works as expected. The interface for managing test users is not the greatest but it works.

Probably even 2 years ago application developers needed to create a plethora of fake accounts just check usual social sharing and other activities. Facebook deals with fake accounts very strictly now and building and/or testing applications using fake accounts is violation of platform terms.

A Facebook test user can only interact with other test users and application the user is added by the application administrator. A test user can not like a fan page - a lot of the time applications require users to like the fan page it is hosted on. Application developers can bypass the "like gate" for test users by simply matching the incoming user ID with the ones saved in the database as test user.. Also, while building the application, it is better to have data to manipulate. It is a capital mistake to theorize before you have data.

Here is an example using my own jQuery based AJAX call to communicate to Facebook graph API end point, but you can also use Facebook JavaScript SDK is FB.api method. I am using batch API call(or the batch request) to

  1. gather all test users for the application
  2. gather their public information and access token

Initially my script was calling for all test users and after calls the graph API to gather information. The second call was a batch request for all the users. This approach was inefficient for 2 reasosns

  1. making 2 graph API calls
  2. the batch request has a 50 call limit, this approach is useless for an application with more than 50 users. I could however, batch the batch request(will post shortly).

The latest fix for the script did all of it within 1 batch request. I simply specified a dependency  between my requests, my second request is dependent on my first one.

1
2
3
4
5
6
7
8
9
10
11
        args.batch.push({
            method                      : "GET",
            name                        : "get-users",
            relative_url                : "/"+app.id+"/accounts/test-users",
            omit_response_on_success    : false
        });
 
        args.batch.push({
            method        : "GET",
            relative_url  : "?ids={result=get-users:$.data.*.id}"
        });

Specifying omit_response_on_success : false is very important as otherwise the API returns a NULL value for the call. I need the first response as it has the access_token for the users.

Here is the link for the entire script.

I am using Mustache engine to generate and display MySQL statements to the browser window.

26Dec/102

iFrame on Facebook application tab

Using iFrame with Facebook is tricky. The FBML fb:iframe works only on application pages but not on the application tabs. Sometimes it is necessary to build an application that is only installable on a profile tab so it is running from within the fan page instead of a stand alone.

There are 2 main advantages of using an iFrame

  1. The CSS is not cached, so debugging and deployment is much easier
  2. Use of jQuery is POSSIBLE!

Facebook parser will generate an error in case of direct implementation of the iframe FBML on a tab. However there is a way to add the iFrame element via FBJS event. Facebook requires user interaction before any automated method execution on a profile tab. The iFrame that loads the content needs a clickable element, preferably an image of the actual frame output. When the user clicks on it, the frame loads with all it's glory!

1
2
3
4
5
6
<script type="text/javascript"><!--mce:0--></script>
<h2>TEST iFRAME</h2>
<div id="iframe_container">
	<a style="cursor: pointer;" onclick="iframe_container.setInnerFBML(iframe_link);">
		click to load iFrame<!-- the clickable element! -->
	</a></div>

A few important points about this snippet

  1. It is FBML, so this code does not have a body tag.
  2. The JavaScript code on top is not necessary for the profile tab, but if user is viewing the actual application, this will automatically load the iframe component, else they have to click on the clickable element.
  3. It is simply loading the fb:js-string which holds the fb:iframe implementation, inside the ifrmae_container.

29Oct/100

Baked json_encode… write your own for PHP 5.1.x

PHP's json_encode is a very handy function for encoding any associative array to JSON. This function comes bundled with PHP 5.2.x. I been working with a client who has PHP 5.1.6. I would install all the necessary tools, but client was not very willing to make changes and I been wanting to write my own JSON encoder for the longest time!

So here is my baked approach...

1
2
3
4
5
6
7
8
9
function my_json_encode($json_array) {
 $json = "[";
 for($i=0; $i1) $json .= ',';
   $json .= '"'.addslashes($key).'":"'.addslashes($row[$key]).'"';
  $i++;
 }
 $json .= "}";
 return $json;
}// end of my_json_encode

and the call

1
2
3
4
5
while($row = mysql_fetch_assoc($result)) {
 $json_row[] = $row;
} 
 
echo my_json_encode($json_row);

5Jan/100

Building Facebook and Facebook Connect application with Codeigniter – Setup

Developing for Facebook is quite simple and easy if you have the right components.  I used Codeingniter as a base framework for a few Facebook applications and that works pretty well .  I had to do some little tweaking though.

This entry is not about how to build a Facebook application in Codeigniter, its more like the things that I have done to get it going...

I named my controller “fbook”. Very important as the Facebook library is “Facebook” under “application/libraries/”

The “Facebook” PHP client comes with 3 main files,
1. facebook_desktop.php
2. facebookapi_php5_restlib.php
3. facebook.php
We need to create a CI library from these 3 files and the “jsonwrapper”. Create a “facebook-clinet” folder under “application/libraries” folder. Copy paste all the files from the Facebook API library except “facebook.php” to this newly created folder. Rename the “facebook.php” file to “Facebook.php” and place it under “application/libraries” folder. Add the following line to the “Facebook.php” file
include_once 'facebook-client/facebookapi_php5_restlib.php';
This will include the REST library.

This is pretty much the initial setup. You can use the same “Facebook” library to for “Facebook Connect” functionality.

27Dec/092

ISO 3166, ISO 3166-1 & ISO 3166-2

WTF is ISO 3166?

I found a lot of sources for ISO 3166-1 complete MySQL database, but no solid source for ISO 3166-2. So I decided to build my own!

So here is the MySQL dump for these 2 tables.

The "id" field for both table probably is unnecessary as both have ISO 3166-1 alpha-2(I just named it "iso") field.