使用 Facebook Graph API,如何获取页面内容?

发布于 2024-09-09 09:45:00 字数 763 浏览 3 评论 0原文

例如,这是一个“页面”:

http://www.facebook.com/facebook

该页面有一个 RSS 提要(理想情况下我想使用它),但是 a) 它浏览器嗅探意味着我需要从脚本中伪造用户代理来获取它 - 这感觉真的很脆弱 b) 的质量返回的数据确实很差。

我可以使用图形 API 来获取相同的数据吗?此 URL:

https://graph.facebook.com/facebook/feed

暗示我 < em>可以,并且 json 对我来说很好,尽管我是从 PHP 脚本而不是客户端获取它。但是,当我为实际页面尝试该 URL 时,我得到以下信息:

{
    "error": {
        "type": "OAuthAccessTokenException",
        "message": "An access token is required to request this resource."
    }
}

我不明白为什么我的页面需要访问令牌,而其他页面是“更公开”的 - 这是页面上某处的配置吗?如果没有,获取访问密钥的最佳方法是什么 - 请注意,这不是要求页面所有者进行身份验证的交互式脚本。

For example, here's a 'page':

http://www.facebook.com/facebook

That page has an RSS feed (which I'd like to use, ideally), but a) it browser-sniffs meaning I need to fake the user-agent from a script to fetch it - and that feels really brittle b) the quality of the data returned is really poor.

Can I use the graph api to fetch the same data? This URL:

https://graph.facebook.com/facebook/feed

implies that I can, and json is fine for me, although I'm fetching this from a PHP script rather than client-side. However, when I try that URL for my actual page, I get the following:

{
    "error": {
        "type": "OAuthAccessTokenException",
        "message": "An access token is required to request this resource."
    }
}

I don't understand why an access token is required for my page, whilst other pages are 'more public' - is that a configuration on the page somewhere? If not, what's the best way of obtaining the access key - note that this is not an interactive script asking the page owner to authenticate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间海 2024-09-16 09:45:00

如果我尝试通过 CURL 访问 URL,它在 PHP 中工作正常。

$curlResponse = http('https://graph.facebook.com/facebook/feed');
$facebookFeed = json_decode($curlResponse['data'], true);

var_dump($facebookFeed);

使用这个 php 函数:

function http($url) {
  $timeout = 30;
  $connectTimeout = 30;
  $sslVerifyPeer = false;

  $response = array();
  $ci       = curl_init();

  /* Curl settings */
  curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
  curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
  curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $sslVerifyPeer);    
  curl_setopt($ci, CURLOPT_URL, $url);

  $response['http_code'] = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  $response['api_call']  = $url;
  $response['data']      = curl_exec($ci);

  curl_close ($ci);

  return $response;
}

If I try to access the URL via CURL, it works OK for me in PHP.

$curlResponse = http('https://graph.facebook.com/facebook/feed');
$facebookFeed = json_decode($curlResponse['data'], true);

var_dump($facebookFeed);

Using this php function:

function http($url) {
  $timeout = 30;
  $connectTimeout = 30;
  $sslVerifyPeer = false;

  $response = array();
  $ci       = curl_init();

  /* Curl settings */
  curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
  curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
  curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $sslVerifyPeer);    
  curl_setopt($ci, CURLOPT_URL, $url);

  $response['http_code'] = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  $response['api_call']  = $url;
  $response['data']      = curl_exec($ci);

  curl_close ($ci);

  return $response;
}
滥情空心 2024-09-16 09:45:00

更新:自 2011 年 6 月 3 日起,所有 API 请求都需要有效的应用程序或用户 access_token。

欲了解更多信息:http://developers.facebook.com/blog/post/509/

Update: As of June 3, 2011, a valid app or user access_token is required with all API requests.

For more info: http://developers.facebook.com/blog/post/509/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文