如何使用C#查询Facebook评论插件评论信息?

时间:2017-02-15 10:03:06

标签: facebook-graph-api c#-4.0 facebook-comments

我正在尝试从this page获取评论信息(向下滚动到** Comments Plugin Code Generator *部分)

我正在使用Facebook Nuget package中的FacebookClient类来获取数据。我的代码如下:

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];

// this is included as a sanity check that the client can fetch data (correct token, proper calls)    
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);

// this is the code the actually should fetch comments content
// this is the data-href value retrieved from inspecting rendered page
var pageUrl = HttpUtility.UrlDecode("https://developers.facebook.com/docs/plugins/comments#configurator"); 
var fbComments = fbClient.Get($"/{pageUrl}/comments");

但是,我只收到这样的JSON结果:

{
  "og_object": {
    "id": "246649445486535",
    "description": "The Comments box lets people comment on content on your site using their Facebook profile and shows this activity to their friends in news feed. It also contains built-in moderation tools and special...",
    "title": "Comments - Social Plugins - Documentation - Facebook for Developers",
    "type": "article",
    "updated_time": "2017-02-09T22:53:10+0000"
  },
  "share": {
    "comment_count": 0,
    "share_count": 4226
  },
  "id": "https:\/\/developers.facebook.com\/docs\/plugins\/comments#configurator\/comments"
}

问题:如何获取实际评论内容?

1 个答案:

答案 0 :(得分:0)

感谢CBroe我已设法正确构建请求并向获取评论信息迈出了一步:

    网址中的
  1. #configurator书签错误(必须删除)

  2. 获取数据的正确代码

    var pageUrl = HttpUtility.UrlDecode("https://developers.facebook.com/docs/plugins/comments");
    var fbComments = fbClient.Get($"https://graph.facebook.com/v2.6/?fields=og_object{{comments}}&id={pageUrl}");
    
  3. 在Chrome中放置pageUrl会收到评论,但在IE11和C#代码中失败。但是,这是另一个问题的另一个问题。