簡體   English   中英

使用 C# 從 facebook 獲取 FB 頁面數據

[英]Getting FB Page data from facebook using C#

在我的桌面應用程序中,我想閱讀特定Facebook 頁面的牆貼、消息、喜歡計數等(不適用於 facebook 用戶)

我瀏覽了這篇文章get user data(on stackoverflow) 除了 FB 頁面,我想實現同樣的目標。

我准備創建一個 facebook 應用程序來實現這一點,並讓用戶授予提取數據的權限。

以上請指教。

您需要訪問令牌才能從 Facebook 獲取頁面數據。首先使用以下 URL 和您的 facebook 應用程序參數獲取訪問令牌:

https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={yourappid}&client_secret={yourappscret}

然后你可以用返回的令牌調用 Facebook 圖 API

一般: https://graph.facebook.com/wikipedia?access_token={token}

帖子: https://graph.facebook.com/wikipedia/posts?access_token={token}

一個示例代碼是;

class Program
{
    static void Main(string[] args)
    {
        var client = new WebClient();

        string oauthUrl = string.Format("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={0}&client_secret={1}", "appid", "appsecret");

        string accessToken = client.DownloadString(oauthUrl).Split('=')[1];

        string pageInfo = client.DownloadString(string.Format("https://graph.facebook.com/wikipedia?access_token={0} ", accessToken));
        string pagePosts = client.DownloadString(string.Format("https://graph.facebook.com/wikipedia/posts?access_token={0} ", accessToken));
    }
}

經過研究,我開發了這段代碼

 class Posts
{
    public string PostId { get; set; }
    public string PostStory { get; set; }
    public string PostMessage { get; set; }
    public string PostPictureUri { get; set; }
    public Image PostImage { get; set; }
    public string UserId { get; set; }
    public string UserName { get; set; }

}

    private List<Posts> getFBPosts()
{
     //Facebook.FacebookClient myfacebook = new Facebook.FacebookClient();
     string AppId = "--------";
     string AppSecret = "----------";
    var client = new WebClient();

    string oauthUrl = string.Format("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={0}&client_secret={1}", AppId, AppSecret);

    string accessToken = client.DownloadString(oauthUrl).Split('=')[1];

     FacebookClient myfbclient = new FacebookClient(accessToken);
   string versio= myfbclient.Version;
   var parameters = new Dictionary<string, object>();
   parameters["fields"] = "id,message,picture";
   string myPage="fanPage"; // put your page name
    dynamic result = myfbclient.Get(myPage +"/posts", parameters);

    List<Posts> postsList = new List<Posts>();
    int mycount=result.data.Count;

    for (int i = 0; i < result.data.Count; i++)
    {
        Posts posts = new Posts();

        posts.PostId = result.data[i].id;
        posts.PostPictureUri = result.data[i].picture;
        posts.PostMessage= result.data[i].message;

         var request = WebRequest.Create(posts.PostPictureUri);
        using (var response = request.GetResponse())
        using (var stream = response.GetResponseStream())
        {
                         posts.PostImage  = Bitmap.FromStream(stream);
        }
          postsList.Add(posts);
    }
    return postsList;

}

您還可以使用名為Facebook的 Nuget package 從 Facebook 圖中獲取數據。 此外, Json.NET可幫助您 map 將數據直接轉換為對象:

public class FacebookPageInfo
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class FacebookPost
{
    public string Message { get; set; }
    // ReSharper disable once InconsistentNaming
    public string Created_Time { get; set; }
    public string Id { get; set; }
}

public class FacebookPagingInfo
{
    public string Previous { get; set; }
    public string Next { get; set; }
}

public class FacebookPostData
{
    public List<FacebookPost> Data { get; set; }
    public FacebookPagingInfo Paging { get; set; }
}

public class Friend
{
    public string Id { get; set; }
    public string Name { get; set; }
}

// get access token
string oauthUrl = $"https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={appId}&client_secret={appSecret}";
string accessToken = client.DownloadString(oauthUrl).Split('=')[1];

// get data and deserialize it
var fbClient = new FacebookClient(accessToken);
var fbData = fbClient.Get("/wikipedia/").ToString();
var info = JsonConvert.DeserializeObject<FacebookPageInfo>(fbData);
fbData = fbClient.Get("/wikipedia/posts").ToString();
var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM