zl程序教程

您现在的位置是:首页 >  前端

当前栏目

在网页中获取 facebook page 的内容

网页 获取 内容 Facebook Page
2023-09-27 14:23:58 时间

参考 : http://www.ibm.com/developerworks/cn/opensource/os-cn-facebookapi/

 

1.首先你要有 facebook page, 内容要公开, 登入admin-> setting -> general -> page visibility = Page Published

2.你要有个 facebook app , 需要 App ID 和 App Secret .

3.然后你需要一个 access_token 

GET https://graph.facebook.com/oauth/access_token?client_id={App ID}&client_secret={App Secret}&grant_type=client_credentials" 

用这个 url 就可以获取了.

然后你就可以访问页面内容了 

GET https://graph.facebook.com/{Page_ID or Page_Name}/feed?access_token={3.access_token}"

返回的是json, 之后你就可以自己看看了。

 

c# 

protected void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(async (t) =>
    {
        using (var client = new HttpClient())
        {
            string APP_ID = "xx";
            string APP_SECRET = "xx";
            client.BaseAddress = new Uri("https://graph.facebook.com");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                             
            HttpResponseMessage response = await client.GetAsync("oauth/access_token?client_id=" + APP_ID + "&client_secret=" + APP_SECRET + "&grant_type=client_credentials");
            if (response.IsSuccessStatusCode)
            {
                string text = await response.Content.ReadAsStringAsync(); //return text
                int ipos = text.IndexOf("=");
                string accessToken = text.Substring(ipos + 1);

                response = await client.GetAsync("OnlineStoreStooges/feed?access_token=" + accessToken);
                if (response.IsSuccessStatusCode)
                {
                    string json = await response.Content.ReadAsStringAsync(); //return json
                }
            }
        }
    }));
}

 

js with angularjs :

var app = angular.module("app", []);
app.controller("ctrl", ["$scope", "$http", function ($scope, $http) {
    var APP_ID = "xx";
    var APP_SECRET = "xx";
    $http({
        method: "get",
        url: "https://graph.facebook.com/oauth/access_token?client_id=" + APP_ID + "&client_secret=" + APP_SECRET + "&grant_type=client_credentials"
    }).success(function (data) {
        var ipos = data.indexOf("=");
        var accessToken = data.substring(ipos + 1);
        $http({
            method: "get",
            url: "https://graph.facebook.com/OnlineStoreStooges/feed?access_token=" + accessToken
        }).success(function (data) {
            log(data);  
        });
    });
}]);