无需登录即可在Facebook页面上阅读Feed

时间:2012-05-03 08:22:40

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

是否可以创建一个网站,并使用JavaScript从Facebook页面读取提要并将其显示给我的网站访问者,而无需登录,甚至可以使用Facebook。

我迷失在Facebook的文件中:(

2 个答案:

答案 0 :(得分:2)

您可以使用PHP在服务器端执行此操作。在Facebook开发人员中心创建一个Facebook应用程序,以获取应用程序密钥和密钥。

$profile_id = "1234567890";     

//App Info, needed for Auth
$app_id = "0001234567890";
$app_secret = "abc123ebf123f3g5g6j";

/* USE THIS LINE INSTEAD OF THE "veryfypeer" LINE BELOW */
Facebook::$CURL_OPTS[CURLOPT_CAINFO] = '/path/to/crt/fb_ca_chain_bundle.crt';

//retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");

$data['feed_data'] = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");

function fetchUrl($url){
         $ch = curl_init();

/* DO NOT USE THE FOLLOWING LINE: I'VE COMMENTED IT OUT HERE */
//      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($ch, CURLOPT_TIMEOUT, 20);

         $retData = curl_exec($ch);
         curl_close($ch); 

         return $retData;
    }

...在Javascript方面我认为这是不可能的,因为必须向公众隐藏FB API秘密。 Information taken from here

答案 1 :(得分:2)

感谢@mch这里的commes版本,该版本使用php作为代理来读取带有JavaScript的Facebook源。

在您的服务器上放置一个名为proxy.php的文件,并添加以下代码:

<?php
// always add this header to ensure the JSON is output in the correct format
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header('Content-Type: application/json; charset=utf-8'); 

$graphUrl = $_POST[graphUrl];
if ($graphUrl == "") {
    $graphUrl = "https://graph.facebook.com/facebook/feed/";
}

//App Info, needed for Auth
$app_id = "1234567890";
$app_secret = "0000011122234334445556aaass";

//retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");

//Echo back json to read client side.
echo fetchUrl("{$graphUrl}?{$authToken}");

function fetchUrl($url){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $retData = curl_exec($ch);
    curl_close($ch); 
    return $retData;
}
?>

将app_id,app_secret更改为您的应用ID。在此处创建应用https://developers.facebook.com/apps/

在代理文件旁边创建一个HTML文件。添加以下代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>FB reader</title>
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            var timeout = 5000,
                load_error;

            load_error = function (jqXHR, textStatus, errorThrown) {
                if (errorThrown === "timeout") {
                    alert('Server bussy');
                } else {
                    alert('error: 404', textStatus + ": " + errorThrown);
                }
            };      

            $(document).ready(function() {
                console.log('Loading from Facebook...\n');

                //Change data: {graphUrl: 'https://graph.facebook.com/iambounty/feed'},  to what ever you want.
                $.ajax({
                    type: 'POST',
                    url: 'proxy.php',
                    data: {graphUrl: 'https://graph.facebook.com/iambounty/feed'}, 
                    timeout:  timeout,
                    error: load_error,
                    success: function (rv) {
                        var data = rv.data,
                            len = data.length,
                            i,
                            out = '';
                        for (i = 0; i < len; i += 1) {
                            if (data[i].description) {
                                out += data[i].description + '\n\n';
                            }
                        }
                        console.log(out);
                    }
                });

            });
        });
    </script>
</body>
</html>
相关问题