簡體   English   中英

使用Facebook C#SDK獲取帶有個人資料圖片和注銷屬性的Facebook用戶個人資料詳細信息

[英]Fetch Facebook user profile details with profile picture and Logout properties using 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使用注銷會話注銷屬性。請幫助我解決此問題。

以下代碼將幫助您獲取電子郵件和個人資料圖片。 代碼client.Get(“ / me?fields = picture”))以Json格式返回圖片,因此我們需要將URL提取為代碼show:

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;

希望您能完成這項工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM