From the Facebook documentation:

A test user is a user account associated with an app created for the purpose of testing the functionality of that application. You can use test users for manual or automated testing.

But how to manage these accounts? and more importantly how to make use of these accounts?
These questions will be answered through a series of tutorials about this very important feature.

 

What do you need

Back to top
 

Creating a test user account

To create a new “test” user account you just make the following request:

POST /app_id/accounts/test-users?installed=true&permissions=read_stream

There are two parameters installed and permissions you can attach to the request when creating an account which will give you TWO options:

  • The application will be installed when creating the account. Here installed should be set to true (which is the default option), so then you can specify a set of permissions your application will have as if granted by the new user
  • The application is not installed (installed is set to false). Which is a VERY important option too, where -for example- you can test application invitations (from a test user A that installed the app to a test user B that is yet to install it)

Now to perform this call using PHP-SDK:

$params = array(
	'access_token' => $APP_ACCESS_TOKEN
	'installed' => 'true',
	'permissions' => 'read_stream,publish_stream'
);
$facebook->api("/APP_ID/accounts/test-users", "POST", $params);

Here the $APP_ACCESS_TOKEN was obtained using our previous tutorial.

Back to top
 

Retrieving the test user accounts

You retrieve the test user accounts by making the following request:

GET  /app_id/accounts/test-users

And similarly using the PHP-SDK:

$accounts = $facebook->api("/APP_ID/accounts/test-users?$APP_ACCESS_TOKEN");

Which would return something like:

{
 "data" [
   { 
    "id": "1231....",  
    "access_token":"1223134..." , 
    "login_url":"https://www.facebook.com/platform/test_account.." 
   }
   { 
    "id": "1231....",  
    "login_url":"https://www.facebook.com/platform/test_account.." 
   }
 ]
}
Back to top
 

Deleting a test user account

Finally, deleting a user account can be done by making the following request:

DELETE  /test_user_id

And again with the PHP-SDK:

$facebook->api("/TEST_ACCOUNT_ID?$APP_ACCESS_TOKEN", "DELETE");
Back to top
 

A working test users panel

Facebook test users panelOur Test Users Panel

How about a small working panel to demonstrate all the above:

<?php
// PATH TO YOUR FACEBOOK PHP-SDK
require '../src/facebook.php';

// REPLACE WITH YOUR APPLICATION ID AND SECRET
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

function getAppAccessToken($fb) {
	$access_token_url = "https://graph.facebook.com/oauth/access_token"; 
	$parameters	= "grant_type=client_credentials&client_id=" . $fb->getAppId() . "&client_secret=" . $fb->getApiSecret();
	return file_get_contents($access_token_url . "?" . $parameters);
}

// GET Test Users
function getTestAccounts($fb, $a) {
	$accounts = $fb->api("/{$fb->getAppId()}/accounts/test-users?$a");
	if( isset($accounts['data']) )
		return $accounts;
	else
		return null;
}

// CREATE Test User
function createTestUser($fb, $a) {
	$params = array();
	$a = explode("=",$a);
	$params['access_token'] = $a[1];
	if( isset($_GET['installed']) && $_GET['installed'] == 'false' )
		$params['installed'] = 'false';
	if( isset($_GET['perms']) ) {
		$perms = trim($_GET['perms']);
		$params['permissions'] = $perms;
	}	
	$fb->api("/{$fb->getAppId()}/accounts/test-users", "POST", $params);
}

// DELETE Test User
function deleteTestUser($fb, $id, $a) {
	$fb->api("/$id?$a", "DELETE");
}

/////////////////////// \\\\\\\\\\\\\\\\\\\\\\\
function printTestUsers($accounts) {
	$html = "";
	if(isset($accounts['data']) && count($accounts['data'])) {
		$html .= "<table>";
		$html .= "<tr class=\"head\"><td colspan=\"4\">Test Users Table</td></tr>";
		$html .= "<tr class=\"head\"><td>Test User ID</td><td>Application User</td><td>Login URL</td><td>Delete</td></tr>";
		foreach($accounts['data'] as $arr) {
			$html .= "<tr>";
			$html .= "<td>{$arr['id']}</td>";
			$html .= "<td>" . ((empty($arr['access_token'])) ? "NO":"YES") . "</td>";
			$html .= "<td><a href=\"{$arr['login_url']}\" target=\"_blank\">Test User Login</a></td>";
			$html .= "<td><a href=\"{$_SERVER['PHP_SELF']}?id={$arr['id']}&action=delete\">Delete Test User</a></td>";
			$html .= "</tr>";
		}
		$html .= "</table>";
	} else {
		$html = "No users or something went wrong!";
	}
	return $html;
}

$app_access_token = getAppAccessToken($facebook);

// PROCESS ACTIONS
if( isset($_GET['action']) ) {
	if( isset($_GET['id']) && $_GET['action'] == 'delete' ) {
		deleteTestUser($facebook,$_GET['id'], $app_access_token);
	}
	if( $_GET['action'] == 'create' ) {
		createTestUser($facebook, $app_access_token);
	}
}

$acc = getTestAccounts($facebook, $app_access_token);
?>
<!doctype html>
<html>
<head>
	<title>Test Users</title>
	<link href="style.css" media="screen" type="text/css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
	<div id="header">
		<h1><a href="<?php echo $_SERVER['PHP_SELF'] ?>">Test Users Panel</a></h1>
	</div>
	<div id="content">
		<?php echo printTestUsers($acc); ?>
		<br />
		<div>
			<form class="cmxform" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
				<fieldset>
				<legend>Create test user:</legend>
				<ol>
				
				<input type="hidden" name="action" value="create" />
				<li>
					<label>Permissions:</label>
					<input type="text" name="perms" value="" />
				</li>
				
				<li>
				<label>Installed:</label>
				<input type="radio" name="installed" value="true" checked="checked" /> Yes <input type="radio" name="installed" value="false" /> No
				</li>

				</ol>
				</fieldset>
				<p><input type="submit" value="Create Test User" /></p>
			</form>
		</div>
	</div>
	<div id="footer">
		<p>&copy; 2011 <a href="http://www.masteringapi.com/">MasteringAPI.com</a></p>
	</div>
</div>
</body>
</html>

And it’s available for download from the link below!

Facebook Test Users Accounts Panel (664)

Back to top
 

Notes

  • As you can see our use of the PHP-SDK is minimal, so you may use your own methods
  • The panel included is just for demonstration, the code is far from complete but it should give you a general idea
Back to top
  • Pingback: How To: Get A Facebook Application Access Token | API? Yes Master!

  • james

    it does not work

    • http://www.masteringapi.com Ibrahim Faour

      How about providing what exactly didn’t work? errors? lines? maybe providing your code on pastebin

  • Paolo

    sick! just what I was looking for whilst I am mastering my first app. :) you rock!

  • http://www.masteringapi.com Ibrahim Faour

    What doesn’t work exactly? also this is a demonstration ONLY! it shouldn’t work as is!
    Modify your comment! only constructive comments allowed!

  • http://www.facebook.com/profile.php?id=536656275 Ahmad Jamaludin

    thats  nice, but how i can use these users??

    • http://www.masteringapi.com Ibrahim Faour

       It’s really up to you. You can create events, post to the “testing user” wall….etc

    • Guest

      Or to be more specific, enter the login URL returned for the test user into your browser to make them the ‘active’ Facebook user for the app.