您能否在不征得用户允许的情况下使用 Graph API 获取公共 Facebook 页面的提要? [英] Can you get a public Facebook page's feed using Graph API without asking a user to allow?

查看:20
本文介绍了您能否在不征得用户允许的情况下使用 Graph API 获取公共 Facebook 页面的提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未使用过 Facebook 的 Graph API 或 OAuth.我只是想使用 Graph API 获取公共 Facebook 页面的提要,但它需要访问令牌.我不想麻烦用户登录并允许访问以获取他们的令牌.Facebook 应用访问令牌可用于获取公共提要,但我正在尝试完全使用 Javascript 执行此操作,因此我无法使用应用机密来执行此操作.我在某处读到 Facebook 应用程序访问令牌不会过期或更改,除非我手动重置密码.这是真的?仅在访问令牌中进行硬编码是否安全?如果没有,是否有某种方法可以验证应用程序以获取令牌而无需涉及用户?我可以使用某种类型的通用应用令牌吗?

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?

推荐答案

如果您和我一样,您的客户不会想要一个标准的 Facebook 点赞插件,他们会希望它以自己的方式设计和定制.

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.

你不需要花一整天的时间去浏览官方文档,想知道它是否适用于你这样简单的事情,这很容易.之所以会出现混淆,是因为您假设使用所有这些密钥和秘密 ID,您必须从要从中提取提要的 Facebook 页面获得许可或身份验证 - 而事实并非如此.您只需要一个有效的应用程序,您就可以获取任何公共页面的提要.

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.

在 Facebook 中设置您的应用,它会为您提供应用 ID 和 API 密钥.获取您想要的公共页面提要的配置文件 ID,这就是您所需要的.然后,您可以使用以下代码检索身份验证令牌,然后使用该代码将提要数据作为 JSON 对象返回.

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.

然后你可以解析这个对象,这里有一些代码可以基于这个线程用PHP来做;

You can then parse the object, here's some code to do it in PHP based on this thread;

处理 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 />";
}

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

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 页面的提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆