Facebook C#sdk批量请求方法返回空数据数组

时间:2012-03-26 06:22:50

标签: facebook facebook-graph-api facebook-c#-sdk

我正在创建Facebook应用程序,可以获得针对多个指标的用户页面的洞察力。防爆。对于一个批处理请求中的“page_active_users”和“page_active_users”。 我正在使用Facebook C#SDK。但无法从Facebook洞察力(GraphAPI)获取数据。

我用5种不同的方式获取数据但没有成功。通过在浏览器中使用Graph API方法显示页面的数据,但在批处理请求中,它返回空数据数组。

// TYPE1

var para1 = new FacebookBatchParameter(HttpMethod.Get, "MyPageId/insights/") 
{ 
  Data = new { access_token = aToken, since = "2012-01-01", metric = "page_active_users" } 
};

// 2型

    var para2 = new FacebookBatchParameter(HttpMethod.Get, "fql/", new
    {
      q = new[]{

"SELECT value,end_time FROM insights WHERE object_id=MyPageId AND metric='page_active_users' AND end_time=end_time_date('2012-01-01') AND period=86400"
           }
    }) { Data = new { access_token = aToken } };

//输入3

 var para3 = new FacebookBatchParameter().Query(
                    "SELECT value,end_time FROM insights WHERE object_id=MyPageId AND metric='page_active_users' AND end_time=end_time_date('2012-01-01') AND period=86400");

//输入4

var para4 = new FacebookBatchParameter
            {
                Path = "MyPageId/insights/",
                //Parameters = new {since = "2012-01-01"},
                Data = new { access_token = aToken, since = "2012-01-01", metric = "page_active_users" },
                HttpMethod = HttpMethod.Get
            };

//输入5

var para5 = new FacebookBatchParameter
            {
                Path = "MyPageId/insights/page_active_users?since=2012-01-01",
                //Parameters = new {since = "2012-01-01"},
                Data = new { access_token = aToken },
                HttpMethod = HttpMethod.Get
            };

//Executed all above type by passing it to below method one by one.But always return empty data array while data is exists on Facebook which I tested using Grap API tool.

var result = client.Batch(para1-5);

任何帮助表示赞赏。 提前致谢。 Dharmendra Mistry

1 个答案:

答案 0 :(得分:3)

我自己找到了解决方案。希望这会对某人有所帮助。这是解决方案。

///I created an enum for list of metrics that facebook is providing
public enum FacebookMatricType
{
        page_active_users,
        page_active_users_locale
}

//Created a list of Facebook batch request for each metric using LINQ to Object and //concatenate string using string.format method.

var batchParameters = (from FacebookMatricType matricType in Enum.GetValues(typeof (FacebookMatricType))
                                   select new object[]
                                              {
                                                 pPageAccessToken,"insights",matricType.ToString(),pFromDate.Date.ToString("yyyy-MM-dd"),pPageId
                                              }
                                   into objectParamter
                                   select new FacebookBatchParameter
                                              {
                                                  HttpMethod = HttpMethod.Get,
                                                  Path =
                                                      string.Format(
                                                          "https://graph.facebook.com/{0}/{1}/{2}?since={3}&access_token={4}",
                                                          objectParamter)
                                              }).ToList();

//Once I get the list of request I execute it using facebook web client using C# SDK.

var facebookClient = new FacebookClient(pProfileAccessToken);
var results = facebookClient.Batch(batchParameters.ToArray()); 
//Results are ready to deserialize.

谢谢。 ;) Dharmendra Mistry