Serkan Yersen
Serkan Yersen

Reputation: 1313

How to validate Facebook App ID

I need to check if the given Facebook app id is valid. Also, I need to check which domain and site configurations are set for this app id. It doesn't matter if it's done through PHP or Javascript.

I checked everywhere but couldn't find any information about this. Any ideas?

Upvotes: 5

Views: 16208

Answers (3)

Hamza Elbanna
Hamza Elbanna

Reputation: 1

Use the Graph API:

$fb = new Facebook\Facebook(/* . . . */);

// Send the request to Graph
try {
  $response = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

var_dump($response);
// class Facebook\FacebookResponse . . .

More info:FacebookResponse for the Facebook SDK for PHP

Upvotes: 0

Jimmy Sawczuk
Jimmy Sawczuk

Reputation: 13614

You can validate the ID by going to http://graph.facebook.com/<APP_ID> and seeing if it loads what you expect. For the app information, try using admin.getAppProperties, using properties from this list.

Upvotes: 9

Wouter van Vliet
Wouter van Vliet

Reputation: 1524

Use the Graph API. Simply request:

https://graph.facebook.com/<appid>

It should return you a JSON object that looks like this:

{
  id: "<appid>",
  name: "<appname>",
  category: "<app category>",
  subcategory: "<app subcategory>",
  link: "<applink>",
  type: "application",
}

So, to validate if the specified app_id is indeed the id of an application, look for the type property and check if it says application. If the id is not found at all, it will just return false.

More info: https://developers.facebook.com/docs/reference/api/application/

For example:

<?php
$app_id = 246554168145;
$object = json_decode(file_get_contents('https://graph.facebook.com/'.$app_id));
// the object is supposed to have a type property (according to the FB docs)
// but doesn't, so checking on the link as well. If that gets fixed
// then check on isset($object->type) && $object->type == 'application'
if ($object && isset($object->link) && strstr($object->link, 'http://www.facebook.com/apps/application.php')) {
   print "The name of this app is: {$object->name}";
} else {
   throw new InvalidArgumentException('This is not the id of an application');
}
?>

Upvotes: 4

Related Questions