开发者

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?


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.


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');
}
?>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜