将参数传递给FB Api()方法有问题

时间:2011-01-09 00:24:52

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

我有以下代码来获取我的朋友列表,其中包含一些要返回的特定字段。

public ActionResult Test()
{
    fbApp = new FacebookApp();
    authorizer = new CanvasAuthorizer(fbApp);
    authorizer.Perms = requiredAppPermissions;

    if (fbApp.Session != null)
    {
        dynamic friendsFields = new ExpandoObject();
        friendsFields.fields = "id,name,location,bio,gender,religion,activities";
        JsonObject data = fbApp.Get("/me/friends", friendsFields);

        ViewData["friends"] = data["data"] as JsonArray;

        return View();
    }
}

我要求提供ID,姓名,地点,生物,性别,宗教,活动字段,但结果不会提交所有这些字段。只返回id,name,gender,这使得怀疑有问题...如果我尝试使用浏览器获取相同的数据并传递字段,结果将返回所有请求的字段:

URL: https://graph.facebook.com/me/friends?fields=id,name,bio,gender,picture,religion,activities&access_token=...
   "data": [
      {
         "id": "data_data_data",
         "name": "data_data_data",
         "bio": "data_data_data",
         "gender": "data_data_data",
         "religion": "data_data_data",
         "picture": "data_data_data",
         "activities": {
            "data": [
               {
                  "name": "data_data_data",
                  "category": "data_data_data",
                  "id": "data_data_data",
                  "created_time": "data_data_data"
               }
            ]
         }
      },
      {
         "id": "data_data_data",
         "name": "data_data_data",
         "bio": "data_data_data",
         "gender": "data_data_data",
         "picture": "data_data_data"
      }
   ]
}

任何人都知道这里可能有什么问题吗?可以是API吗?我使用的是最新版本4.1.1

TIA!

1 个答案:

答案 0 :(得分:0)

您必须转换Get方法的结果或仅动态访问它。以下是修正后的代码,并且授权也已修复。

[CanvasAuthorize(Perms="user_friends")]
public ActionResult Test()
{
    var fbApp = new FacebookApp();  
    dynamic friendsFields = new ExpandoObject();
    friendsFields.fields = "id,name,location,bio,gender,religion,activities";
    dynamic result = fbApp.Get("/me/friends", friendsFields);

    var data = result.data as JsonArray;
    ViewData["friends"] = data;

    return View();
}