您可以使用Graph API获取公开的Facebook页面的Feed,而不要求用户允许? [英] Can you get a public Facebook page's feed using Graph API without asking a user to allow?
问题描述
如果你像我一样,你的客户不会想要一个标准的Facebook likebox插件,他们会想要所有的风格和自己的方式定制。
你不需要花一整天的官方文档,想知道是否任何这个都适用于你这样的简单的东西,这很简单。出现混乱,因为你会假设所有这些密钥和秘诀,你必须从Facebook页面获得许可或身份验证,你想从中提取Feed - 你不会。所有您需要的是一个有效的应用程序,您可以获取任何公共页面的Feed。
将您的应用程序设置在Facebook中,它将为您提供一个应用程序ID和API键。获取您想要的公共页面Feed的配置文件ID,这就是您需要的。然后,您可以使用以下代码来获取auth令牌,然后使用该代码将Feed数据作为JSON对象返回。
<?php
函数fetchUrl($ url){
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_TIMEOUT,20);
//您可能需要在
// curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false)之下添加行;
$ feedData = curl_exec($ ch);
curl_close($ ch);
return $ feedData;
}
$ profile_id =the_profile_id_of_the_page_you_want;
//应用信息,Auth需要
$ app_id =your_app_id_in_here;
$ app_secret =your_app_secret_in_here;
//检索认证令牌
$ authToken = fetchUrl(https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret = {$ app_secret});
$ json_object = fetchUrl(https://graph.facebook.com/{$profile_id}/feed?{$authToken});
感谢编辑有人建议我认为这段代码来自这里(看起来很熟悉:)),还有一些更多的信息可能有帮助的评论。
然后,您可以解析对象,下面是基于此线程的PHP中的一些代码;
$ feedarray = json_decode($ json_object);
foreach($ feedarray-> data as $ feed_data)
{
echo< h2> {$ feed_data-> name}< / h2> br />;
echo{$ feed_data-> message}< br />< br />;
}
要了解json对象中可用的内容,您可以输出url在浏览器中,将其复制/粘贴到这个有用的json可视化工具中;
http://chris.photobooks.com/json/default.htm
I've never used Facebook's Graph API, or OAuth. I'm simply trying to get a public Facebook page's feed using the Graph API, but it requires an access token. I don't want to hassle the users to login and allow access to get their token. A Facebook app access token could be used to get a public feed, but I'm trying to do this entirely in Javascript, so I can't use the app secret to do so. I read somewhere that a Facebook app access token doesn't ever expire or change unless I manually reset the secret. Is this true? Would it be safe to just hard code in the Access Token? If not, is there some way I could authenticate an app to get the token without having to involve a user? Is there some type of generic app token I could use?
If you're anything like me your clients won't want a standard Facebook likebox plugin, they'll want it all styled and customised their own way.
You don't need to spend all day going round the official documentation wondering if any of it applies to you for something simple like this, it's quite easy. The confusion arises because you'd assume that with all these keys and secret ids you'd have to get permission or authentication from the Facebook page you wanted to pull the feed from - you don't. All you need is a valid app and you can get the feed for any public page.
Set your app up in Facebook and it will give you an app id and an API key. Get the profile id for the public page feed you want, and that's all you need. You can then use the following code to retrieve the auth token and then then use that to return the feed data as a JSON object.
<?php
function fetchUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// You may need to add the line below
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
$profile_id = "the_profile_id_of_the_page_you_want";
//App Info, needed for Auth
$app_id = "your_app_id_in_here";
$app_secret = "your_app_secret_in_here";
//Retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret={$app_secret}");
$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
Thanks to an edit someone suggested I reckon this code came from here (looks familiar anyway :) ) and there's some more info in the comments there that might help.
You can then parse the object, here's some code to do it in PHP based on this thread;
Handling data in a PHP JSON Object
$feedarray = json_decode($json_object);
foreach ( $feedarray->data as $feed_data )
{
echo "<h2>{$feed_data->name}</h2><br />";
echo "{$feed_data->message}<br /><br />";
}
To find out what's available to you in the json object you can output the url in a browser and copy/paste it into this useful json visualisation tool;
http://chris.photobooks.com/json/default.htm
这篇关于您可以使用Graph API获取公开的Facebook页面的Feed,而不要求用户允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!