Friday, January 7, 2011

Facebook code for creating test users

Here is the working code for creating facebook test users:

<?php
  // modified version of the code from
  // http://coding.pressbin.com/66/PHP-Create-test-user-accounts-in-Facebook/
 // See link above for deleting test users as well.  Take care to not delete your real account.

  //replace "#######" with your facebook app_id and secret key
define('FACEBOOK_APP_ID', ''######");
define('FACEBOOK_SECRET', '#######');

require 'facebook-php-sdk/src/facebook.php';

// Authenticate as an application to get an access token.
$url = 'https://graph.facebook.com/oauth/access_token';
$params=array(
              "grant_type" => "client_credentials",
              "client_id" => FACEBOOK_APP_ID,
              "client_secret" => FACEBOOK_SECRET);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, '&') );
$data = curl_exec($ch);
curl_close($ch);

$access_token = str_replace('access_token=', '', $data);

print_r($access_token);
echo '<b>';

// Create the test user account
// replace ##### with your app ID.
$url = "https://graph.facebook.com/#####/accounts/test-users?installed=true&permissions=read_stream&access_token=$access_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, '&') );
$data = curl_exec($ch);
curl_close($ch);

$request = json_decode($data,true);

foreach ($request as $key => $value) {
  if ($key == "id")
  echo '<br>Test user ID: '.$request[$key].'<br>';
  elseif ($key == "login_url")
  echo 'Log in as this test user: '.$request[$key].'<br>';
}
?>

I make no guarantees about this code but it works as of Jan 7, 2011.

1 comment:

  1. Thanks!!! All the other code snippets I found did not work.

    ReplyDelete