简体   繁体   English

您是否可以使用Graph API获取公共Facebook页面的订阅源而无需要求用户允许?

[英]Can you get a public Facebook page's feed using Graph API without asking a user to allow?

I've never used Facebook's Graph API, or OAuth. 我从未使用Facebook的Graph API或OAuth。 I'm simply trying to get a public Facebook page's feed using the Graph API, but it requires an access token. 我只是想使用Graph API获取公共Facebook页面的源,但它需要访问令牌。 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. Facebook应用程序访问令牌可用于获取公共提要,但我试图完全用Javascript完成此操作,因此我无法使用应用程序密钥来执行此操作。 I read somewhere that a Facebook app access token doesn't ever expire or change unless I manually reset the secret. 我在某处读到Facebook应用程序访问令牌永远不会过期或更改,除非我手动重置密码。 Is this true? 这是真的? Would it be safe to just hard code in the Access Token? 在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? 我可以使用某种类型的通用app令牌吗?

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. 如果你和我一样,你的客户不会想要一个标准的Facebook Likebox插件,他们会希望它们都采用自己的方式设计和定制。

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. 之所以产生混淆,是因为您假设所有这些密钥和秘密ID都必须从Facebook页面获得许可或身份验证,而您需要从中提取 - 您不需要。 All you need is a valid app and you can get the feed for any public page. 您只需要一个有效的应用程序,您就可以获得任何公共页面的Feed。

Set your app up in Facebook and it will give you an app id and an API key. 在Facebook上设置您的应用程序,它将为您提供应用程序ID和API密钥。 Get the profile id for the public page feed you want, and that's all you need. 获取所需公共页面Feed的个人资料ID,这就是您所需要的。 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. 然后,您可以使用以下代码检索身份验证令牌,然后使用该代码将Feed数据作为JSON对象返回。

<?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; 然后你可以解析对象,这里有一些代码可以在PHP中基于这个线程来完成它;

Handling data in a PHP JSON Object 处理PHP JSON对象中的数据

$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; 要找出json对象中可用的内容,您可以在浏览器中输出url并将其复制/粘贴到这个有用的json可视化工具中;

http://chris.photobooks.com/json/default.htm http://chris.photobooks.com/json/default.htm

Facebook app access token doesn't ever expire or change unless I manually reset the secret 除非我手动重置密码,否则Facebook应用访问令牌不会过期或更改

That's correct. 那是对的。

App tokens do not expire unless the App Secret is reset. 除非重置App Secret,否则App令牌不会过期。 App access tokens are unique to each app. 应用访问令牌对每个应用都是唯一的。

Since public feeds take any valid access token, application tokens can be used. 由于公共提要采用任何有效的访问令牌,因此可以使用应用程序令牌。

Go to https://developers.facebook.com/tools/access_token and you would not require a flow. 转到https://developers.facebook.com/tools/access_token ,您不需要流程。 Then you can just hard code it in. The moment you reset the secret this method is void. 然后你可以硬编码。当你重置秘密时,这个方法是无效的。

$access_token = '1111111111|2Cha_1-n5'
$graph_url = "https://graph.facebook.com/Boo/posts?access_token=" 
    . $access_token;
$page_posts = json_decode(file_get_contents($graph_url), true);

Then iterate through the pages 然后遍历页面

foreach($page_posts['data'] as $post){
    $post_link = $post['actions'][0]['link'];
    $page_id = $post['from']['id'];
    $page_name = $post['from']['name'];
    $message = ($post['message']) ? $post['message'] : " ";
    $name = ($post['name']) ? $post['name'] : " ";
    $story = ($post['story']) ? $post['story'] : " ";
    $post_time = $post['updated_time'];
}

Source: http://philippeharewood.com/facebook/how-to-get-facebook-access-tokens-in-php-for-public-posts-the-basic-sauce/ 资料来源: http//philippeharewood.com/facebook/how-to-get-facebook-access-tokens-in-php-for-public-posts-the-basic-sauce/

In response to the message of @Psyked and @James_David_Low, it is not recommended to use FQL because is deprecated. 为了响应@Psyked和@James_David_Low的消息,不建议使用FQL,因为不推荐使用。

"...There are two low-level HTTP APIs that are also used at Facebook to access the graph: FQL and the Legacy REST API. These APIs contain similar and overlapping functionality, but are deprecated." “... Facebook上还有两个低级HTTP API用于访问图表:FQL和Legacy REST API。这些API包含类似且重叠的功能,但已被弃用。”

New features are generally only available in the Graph API . 新功能通常仅在Graph API中提供 To future-proof your app you should be using the Graph API in your app if you can. 为了面向未来的应用,您应该在应用中使用Graph API。

The accepted answer does not provide a dynamic way of retrieving the profileId/pageId or even explain how to retrieve it. 接受的答案不提供检索profileId / pageId的动态方法,甚至不解释如何检索它。 Check out my answer/question . 看看我的回答/问题 From my experience, the accepted answer is also wrong in requiring the curly braces around the app-id and app-secret . 根据我的经验,接受的答案也是错误的,需要围绕app-idapp-secret的花括号。 I got an error when I included those and it worked with them out. 当我包含这些内容时,我收到了一个错误,并且它与它们一起工作。

It doesn't get any simpler than this. 它没有比这更简单。

I think in order to do this you have to use FQL not simply the Graph API. 我认为为了做到这一点,你必须使用FQL而不仅仅是Graph API。 This is what I'm doing (replace PAGE_ID with the ID of your page, keep the quotes): 这就是我正在做的事情(将PAGE_ID替换为您的页面ID,保留引号):

SELECT post_id, created_time, type, message, attachment 
FROM stream 
WHERE source_id = 'PAGE_ID' 
AND actor_id = source_id

https://developers.facebook.com/docs/reference/fql/stream/ https://developers.facebook.com/docs/reference/fql/stream/

If you want to use page feed (like your own page feed) in an app for showing it to others. 如果您想在应用中使用页面Feed(例如您自己的页面Feed)来向其他人展示。 Just use your own access_token to get it. 只需使用您自己的access_token即可获得它。
BUT! 但! as it's not considered as a good practice you can also login with the Page or Application and use their access_token. 因为它不被视为一种好的做法,您也可以使用页面或应用程序登录并使用他们的access_token。

For more information please read the official documentation on authentication 有关更多信息,请阅读有关身份验证官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM