从Facebook获取我的朋友

时间:2013-08-07 20:20:45

标签: c# facebook facebook-graph-api console-application

我正在通过编写简单的控制台应用程序来学习facebook api。 我正在尝试做一些简单的事情,我在网上找到了代码示例(并在stackoverflow中)。

我正在尝试做的事情是让所有的朋友列出[name,id]。

我运行代码,但得到#2500错误。这是代码:

class Program
{
    static void Main(string[] args)
    {           
        FacebookClient fbClient = new FacebookClient();
        dynamic result = fbClient.Get("oauth/access_token", new
        {
            client_id = <REMOVED APP ID>,
            client_secret = "<REMOVED APP SECRET>",
            grant_type = "client_credentials"
        });
        fbClient.AccessToken = result.access_token;
         var friendListData = fbClient.Get("/me/friends");
        JObject friendListJson = JObject.Parse(friendListData.ToString());
         List<FbUser> fbUsers = new List<FbUser>();
        foreach (var friend in friendListJson["data"].Children())
        {
            FbUser fbUser = new FbUser();
            fbUser.Id = friend["id"].ToString().Replace("\"", "");
            fbUser.Name = friend["name"].ToString().Replace("\"", "");
            fbUsers.Add(fbUser);
        }
         foreach (var item in fbUsers)
        {
            Console.WriteLine(item);
            Console.WriteLine();
        }
    }
}

我的代码中缺少什么?

(对不起我的英文)

1 个答案:

答案 0 :(得分:1)

string myAccessToken = "something";         
FacebookClient client = new FacebookClient(myAccessToken); 

myAccessToken是您可以从此处获取的用户令牌https://developers.facebook.com/tools/access_token/

而不是

FacebookClient fbClient = new FacebookClient();
        dynamic result = fbClient.Get("oauth/access_token", new
        {
            client_id = <REMOVED APP ID>,
            client_secret = "<REMOVED APP SECRET>",
            grant_type = "client_credentials"
        });
        fbClient.AccessToken = result.access_token;`enter code here`