Facebook Graph API retrieve Friends with json and C#

前端 未结 2 1145
梦谈多话
梦谈多话 2021-02-06 15:53

I\'m working in C# with the Graph API and have been able to grab Facebook user profile information such as the ID, Name and email and then deserialize the JSON to be able to ass

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 15:59

    Well..I ended up using JSON.Net, and it worked great. Thank you for pointing me in that direction. With the help of another article I found (http://www.mattcashatt.com) and the JSON.net files, I was able to get everything working. Here is some of the code I used.

                #region JSON.Net User Profile
                //Profile URL
                url = "https://graph.facebook.com/me?fields=id,name,email&access_token=" + oAuth.Token;
    
                JObject myProfile = JObject.Parse(requestFBData(url));
                string myID = myProfile["id"].ToString().Replace("\"", "");
                string myName = myProfile["name"].ToString().Replace("\"", "");
                string email = myProfile["email"].ToString().Replace("\"", "");
    
                lblID.Text = myID;
                lblFullName.Text = myName;
                lblEmail.Text = email;
                imgUser.ImageUrl = "https://graph.facebook.com/me/picture?type=large&access_token=" + oAuth.Token;
    
                #endregion
    
    
                #region JSON.Net Friends
    
                //Friends URL
                url = "https://graph.facebook.com/me/friends?access_token=" + oAuth.Token;
    
    
                JObject myFriends = JObject.Parse(requestFBData(url));
    
                string id="";
                string name = "";
    
                //Loop through the returned friends
                foreach (var i in myFriends["data"].Children())
                {
                    id = i["id"].ToString().Replace("\"", "");
                    name = i["name"].ToString().Replace("\"", "");
                    lblFriends.Text = lblFriends.Text + "
    " + "id: " + id + " name: " + name + ""; } #endregion } } } public string requestFBData(string action) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(resp.GetResponseStream()); string results = sr.ReadToEnd(); sr.Close(); return results; }

提交回复
热议问题