通过FB API从个人资料中检索Facebook帖子 [英] Retriving Facebook posts from Profile via FB api
问题描述
我已经在Facebook开发人员程序中注册了一个应用程序,可以使用fb api从Facebook页面检索帖子,但是无法从facebook个人资料检索帖子.页面或个人资料都应使用其他访问令牌吗?有没有其他方法可以从Facebook页面和个人资料中检索帖子? 任何帮助将不胜感激!
I have registered an app in facebook developers program and I can retrieve posts from facebook page using fb api, but I cannot retrieve posts from facebook profile. Should I use some different access token for both page or profile? Is there a different way for retrieving posts from facebook page and profile? Any help will be appreciated!
推荐答案
先决条件:-您需要为所有请求提供有效的FB令牌:- 我在回答使用C#语言. 步骤1:-首先,您需要使用FB令牌获取用户的FB ID
Prerequisite:- You need to have valida FB token for all the request:- I am answering using c# language. Step 1:- First you need to get the user's FB id using FB token
我正在使用Facebook SDK 7.0.6进行查询
I am using Facebook SDK 7.0.6 for querying purpose
这是初始化在连续调用中使用的FB服务的方法.
here's how to initialize the FB service which we will use in our consecutive calls.
FacebookService facebookService = new FacebookService(facebookToken);
var _facebookClient = new Facebook.FacebookClient(token);
代码段
public string GetFacebookID(string facebookToken)
{
dynamic result = _facebookClient.Get("me?fields=id");
if (result.ToString().Contains("id"))
{
return result["id"];
}
return string.Empty;
}
之后,您可以执行以下方法,使用FB ID和令牌获取用户的帖子
after that you can execute below method to get user's post using FB ID and token
public List<Post> GetPostsForUser(string facebookID)
{
List<Post> posts = new List<Post>();
dynamic result = _facebookClient.Get(facebookID + "/posts"); //Case Sensitive, Posts doesn´t work
if (result.ToString().Contains("data") && result.data.Count > 0)
{
foreach (var item in result.data)
{
posts.Add(new Post
{
ID = item.id,
Story = item.story,
Message = item.message,
Created_Time = Convert.ToDateTime(item.created_time),
Reactions = GetReactions(item.id)
});
}
result = _facebookClient.Get(GetNextURL(result.ToString()));
}
return posts;
}
这篇关于通过FB API从个人资料中检索Facebook帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!