javascript 如何验证 Facebook 应用 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6650107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:24:18  来源:igfitidea点击:

How to validate Facebook App ID

phpjavascriptapifacebook

提问by Serkan Yersen

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.

我需要检查给定的 Facebook 应用程序 ID 是否有效。此外,我需要检查为此应用程序 ID 设置了哪些域和站点配置。无论是通过 PHP 还是 Javascript 完成都没有关系。

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

我到处检查,但找不到任何有关此的信息。有任何想法吗?

回答by Jimmy Sawczuk

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.

您可以通过转到http://graph.facebook.com/<APP_ID>并查看它是否加载了您期望的内容来验证 ID 。有关应用程序信息,请尝试使用admin.getAppProperties,使用此列表中的属性。

回答by Wouter Van Vliet

Use the Graph API. Simply request:

使用图形 API。简单要求:

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

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

它应该返回一个 JSON 对象,如下所示:

{
  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.

因此,要验证指定的 app_id 是否确实是应用程序的 id,请查找 type 属性并检查它是否为 application.id 。如果根本没有找到 id,它只会返回 false。

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

更多信息: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');
}
?>

回答by Hamza Elbanna

Use the Graph API:

使用图形 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

更多信息:用于 PHP 的 Facebook SDK 的 FacebookResponse