//Getting AccessToken,FacebookMessages,FacebookFeeds and UserProfile for  Authenticated user.

        private void GetAccessToken()
        {
            User   user = new SocioBoard.Domain.User();
            string code = Request.QueryString["code"];

            if (code == null)
            {
                Response.Redirect("Home.aspx");
            }
            if (Session["login"] != null)
            {
                if (Session["login"].ToString() == "googleplus")
                {
                    user = (User)Session["LoggedUser"];
                }
                else
                {
                    user = null;
                }
            }
            if (Session["login"] != null)
            {
                if (Session["login"].ToString() == "facebook")
                {
                    user = new User();

                    user.CreateDate    = DateTime.Now;
                    user.ExpiryDate    = DateTime.Now.AddDays(30);
                    user.Id            = Guid.NewGuid();
                    user.PaymentStatus = "unpaid";
                }
            }
            else
            {
                /*User class in SocioBoard.Domain to check authenticated user*/
                user = (User)Session["LoggedUser"];
            }
            /*Replacing Code With AccessToken*/
            // Facebook.dll using for
            FacebookHelper fbhelper = new FacebookHelper();


            FacebookClient fb        = new FacebookClient();
            string         profileId = string.Empty;
            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters.Add("client_id", ConfigurationManager.AppSettings["ClientId"]);
            parameters.Add("redirect_uri", ConfigurationManager.AppSettings["RedirectUrl"]);
            parameters.Add("client_secret", ConfigurationManager.AppSettings["ClientSecretKey"]);
            parameters.Add("code", code);

            JsonObject result      = (JsonObject)fb.Get("/oauth/access_token", parameters);
            string     accessToken = result["access_token"].ToString();

            fb.AccessToken = accessToken;

            // This code is used for posting Begin

            ManageReferrals(fb);

            // This code is used for posting End

            // For long Term Fb access_token

            // GET /oauth/access_token?
            //grant_type=fb_exchange_token&
            //client_id={app-id}&
            //client_secret={app-secret}&
            //fb_exchange_token={short-lived-token}


            parameters.Clear();

            parameters.Add("grant_type", "fb_exchange_token");
            parameters.Add("client_id", ConfigurationManager.AppSettings["ClientId"]);
            parameters.Add("client_secret", ConfigurationManager.AppSettings["ClientSecretKey"]);
            parameters.Add("fb_exchange_token", accessToken);

            result         = (JsonObject)fb.Get("/oauth/access_token", parameters);
            accessToken    = result["access_token"].ToString();
            fb.AccessToken = accessToken;

            dynamic profile = fb.Get("me");

            int  res            = UpdateFbToken(profile["id"], accessToken);
            bool isfbemailexist = false;

            if (Session["login"] != null)
            {
                if (Session["login"].ToString() == "facebook")
                {
                    try
                    {
                        user.EmailId = profile["email"].ToString();
                    }
                    catch (Exception ex)
                    {
                        isfbemailexist = true;
                        logger.Error(ex.StackTrace);
                        Console.WriteLine(ex.StackTrace);
                    }
                    if (isfbemailexist)
                    {
                        Session["isemailexist"] = "emailnotexist";
                        Response.Redirect("Default.aspx");
                    }
                    try
                    {
                        user.UserName = profile["name"].ToString();
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.StackTrace);
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        user.ProfileUrl = "https://graph.facebook.com/" + profile["id"] + "/picture?type=small";
                        profileId       = profile["id"];
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.StackTrace);
                        Console.WriteLine(ex.StackTrace);
                    }
                    user.UserStatus = 1;

                    UserRepository userrepo = new UserRepository();

                    if (userrepo.IsUserExist(user.EmailId))
                    {
                        string emailid = user.EmailId;
                        user = null;

                        user = userrepo.getUserInfoByEmail(emailid);
                    }
                    else
                    {
                        UserRepository.Add(user);
                    }

                    Session["LoggedUser"] = user;
                }
            }



            var feeds = fb.Get("/me/feed");

            var home     = fb.Get("/me/home");
            var messages = fb.Get("/me/inbox");

            long friendscount = 0;

            try
            {
                fbhelper.getInboxMessages(messages, profile, user.Id);
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }

            try
            {
                dynamic friedscount = fb.Get("fql", new { q = "SELECT friend_count FROM user WHERE uid=me()" });

                foreach (var friend in friedscount.data)
                {
                    friendscount = friend.friend_count;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }


            try
            {
                fbhelper.getFacebookUserProfile(profile, accessToken, friendscount, user.Id);
            }
            catch (Exception exx)
            {
                logger.Error(exx.StackTrace);
                Console.WriteLine(exx.StackTrace);
            }

            try
            {
                fbhelper.getFacebookUserFeeds(feeds, profile);
            }
            catch (Exception exxx)
            {
                logger.Error(exxx.StackTrace);
                Console.WriteLine(exxx.StackTrace);
            }

            try
            {
                fbhelper.getFacebookUserHome(home, profile);
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }

            try
            {
                var friendsgenderstats = fb.Get("me/friends?fields=gender");
                fbhelper.getfbFriendsGenderStats(friendsgenderstats, profile, user.Id);
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }
            try
            {
                FacebookInsightStatsHelper fbiHelper = new FacebookInsightStatsHelper();
                fbiHelper.getPageImpresion(profile["id"], user.Id, 15);
                fbiHelper.getFanPageLikesByGenderAge(profile["id"], user.Id, 15);
                fbiHelper.getLocation(profile["id"], user.Id, 15);
                //  fbiHelper.getFanPost("459630637383010", user.Id, 10);
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
            }
        }
예제 #2
0
        public void GetData(object userId)
        {
            Guid UserId = (Guid)userId;
            FacebookAccountRepository objFbRepo = new FacebookAccountRepository();
            FacebookHelper            fbhelper  = new FacebookHelper();
            ArrayList arrFbAcc = objFbRepo.getAllFacebookAccountsOfUser(UserId);

            foreach (FacebookAccount itemFb in arrFbAcc)
            {
                FacebookHelper             objFbHelper = new FacebookHelper();
                FacebookInsightStatsHelper fbiHelper   = new FacebookInsightStatsHelper();
                try
                {
                    FacebookClient fb = new FacebookClient();
                    fb.AccessToken = itemFb.AccessToken;

                    var feeds   = fb.Get("/me/feed");
                    var home    = fb.Get("me/home");
                    var profile = fb.Get("me");
                    getFacebookUserHome(home, profile, UserId);
                    getFacebookUserFeeds(feeds, profile, UserId);
                    getFacebookProfile(profile, UserId);
                    FacebookAccountRepository fbAccRepo = new FacebookAccountRepository();
                    try
                    {
                        int     fancountacc = 0;
                        dynamic fanacccount = fb.Get("fql", new { q = "SELECT friend_count FROM user WHERE uid=" + itemFb.FbUserId });
                        foreach (var friend in fanacccount.data)
                        {
                            fancountacc = Convert.ToInt32(friend.friend_count);
                        }

                        fbAccRepo.updateFriendsCount(itemFb.FbUserId, itemFb.UserId, fancountacc);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (itemFb.Type == "page")
                    {
                        try
                        {
                            fbiHelper.getFanPageLikesByGenderAge(itemFb.FbUserId, UserId, 10);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                        try
                        {
                            fbiHelper.getPageImpresion(itemFb.FbUserId, UserId, 10);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        try
                        {
                            fbiHelper.getStories(itemFb.FbUserId, UserId, 10);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        try
                        {
                            fbiHelper.getLocation(itemFb.FbUserId, UserId, 10);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        try
                        {
                            fbiHelper.getFanPost(itemFb.FbUserId, UserId, 10);
                        }
                        catch (Exception ex)
                        {
                        }

                        try
                        {
                            int     fancountPage = 0;
                            dynamic fancount     = fb.Get("fql", new { q = " SELECT fan_count FROM page WHERE page_id =" + itemFb.FbUserId });
                            foreach (var friend in fancount.data)
                            {
                                fancountPage = Convert.ToInt32(friend.fan_count);
                            }
                            // FacebookAccountRepository fbAccRepo = new FacebookAccountRepository();
                            fbAccRepo.updateFansCount(itemFb.FbUserId, itemFb.UserId, fancountPage);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    //  var friendsgenderstats = fb.Get("me/friends?fields=gender");
                    // fbhelper.getfbFriendsGenderStats(friendsgenderstats, profile, UserId);
                }
                catch (Exception Err)
                {
                    Console.Write(Err.Message);
                }
            }
        }