使用Facebook C#SDK

时间:2015-07-21 09:34:23

标签: c# asp.net facebook-c#-sdk

我想使用Facebook c#SDK获取用户个人资料。我已经做了一些编码,并且能够在我的应用程序中使用Facebook登录。我正在解释下面的代码。

  

immediateHelp.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                    if (Request.Params["code"] != null)
                    {

                        //Facebook.FacebookAPI api = new Facebook.FacebookAPI();
                        FacebookClient client = new Facebook.FacebookClient(GetAccessToken());
                        object me = client.Get("/me");

                       lblTextImage.ImageUrl="need picture here"  ;
                       lblTextEmail.Text="need email here";
                       // JSONObject meFriends = client.Get("/me/friends");

                    }

            }
        }
        private string GetAccessToken()
        {

            if (HttpRuntime.Cache["access_token"] == null)
            {

                Dictionary<string, string> args = GetOauthTokens(Request.Params["code"]);

                HttpRuntime.Cache.Insert("access_token", args["access_token"], null, DateTime.Now.AddMinutes(Convert.ToDouble(args["expires"])), TimeSpan.Zero);

            }



            return HttpRuntime.Cache["access_token"].ToString();

        }
        private Dictionary<string, string> GetOauthTokens(string code)
        {

            Dictionary<string, string> tokens = new Dictionary<string, string>();



            string clientId = "*************";

            string redirectUrl = "http://localhost:3440/immediateHelp.aspx";

            string clientSecret = "************************";

            string scope = "user_photos,email";



            string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}",

                            clientId, redirectUrl, clientSecret, code, scope);



            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {

                StreamReader reader = new StreamReader(response.GetResponseStream());

                string retVal = reader.ReadToEnd();



                foreach (string token in retVal.Split('&'))
                {

                    tokens.Add(token.Substring(0, token.IndexOf("=")),

                        token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));

                }

            }



            return tokens;

        }

我需要在登录后附加图像字段中的图像和文本框中的电子邮件。我还需要使用Facebook c#SDK在销毁会话中注销属性。请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

以下代码可帮助您获取电子邮件和个人资料照片。代码 client.Get(“/ me?fields = picture”))以Json的格式返回图片,以便我们需要提取url,如代码所示:

FacebookClient client = new FacebookClient(access_token);
dynamic myInfo = fb.Get("/me?fields=email");

var picture = (((JsonObject)(JsonObject)client.Get("/me?fields=picture"))["picture"]);
var url = (string)(((JsonObject)(((JsonObject)picture)["data"]))["url"]);


lblTextImage.ImageUrl=url;
lblTextEmail.Text=myInfo.email;

为你开展这项工作。