public async Task <ActionResult> AddPagePostAsync(FacebookPagePost post)
 {
     post.Page.ApplicationUser = CurrentUser;
     post.ApplicationUser      = CurrentUser;
     if (post.ApplicationUser.Points - post.Points < 0)
     {
         ModelState.AddModelError("points", "You do not have enough points, Please earn points");
     }
     if (context.FacebookPostPages.Any(p => p.PageId == post.Page.PageId))
     {
         post.Page = context.FacebookPostPages.First(p => p.PageId == post.Page.PageId);
         ModelState["Page.Link"].Errors.Clear();
     }
     post.Page.Points = post.Points;
     ModelState["Page.Points"].Errors.Clear();
     if (ModelState.IsValid)
     {
         context.FacebookPagePosts.Add(post);
         lock (post.ApplicationUser)
         {
             post.ApplicationUser.Points -= post.Points;
         }
         try
         {
             await context.SaveChangesAsync();
         }
         catch (DbEntityValidationException e)
         {
         }
         return(RedirectToAction("PagePosts"));
     }
     return(View(post));
 }
    protected void btnPost_Click(object sender, EventArgs e) //מתודת שליחת פוסט לקיר של הדף בפייסבוק
    {
        FacebookPagePost _post         = new FacebookPagePost();
        string           _postResponse = "";

        _post.message = ((ASP.controls_cmstrtextboxcontrol_ascx)BlogTypeMyForm.FindControl("txt_PostText")).Text;
        using (MySqlConnection con = new MySqlConnection(siteDefaults.ConnStr))
        {
            con.Open();
            MySqlCommand _cmd = new MySqlCommand();
            _cmd.Connection  = con;
            _cmd.CommandText = "select * from facebookpages where FacebookPagesID = 1";
            MySqlDataReader _dr = _cmd.ExecuteReader();
            if (_dr.Read())
            {
                _post.id           = _dr["PageID"].ToString();
                _post.access_token = _dr["AccessToken"].ToString();
            }
            _dr.Close();
            con.Close();
        }
        _post.link = ((ASP.controls_cmstrtextboxcontrol_ascx)BlogTypeMyForm.FindControl("Post_Link")).Text;

        if (_post.access_token != "" && _post.access_token != null) //בדיקה האם יש טוקן במערכת והאם יש קבוצה שניתן "לפרסם" בה
        {
            _postResponse = FacebookCode.PostOnFacebookPage(_post); //ביצוע פוסט + קבלת מספר פוסט בכתשובה
            if (_postResponse != "")                                //בדיקה האם חזר מספר פוסט כתשובה מפייסבוק
            {
                using (MySqlConnection con = new MySqlConnection(siteDefaults.ConnStr))
                {
                    con.Open();
                    MySqlCommand _cmd = new MySqlCommand();
                    _cmd.Connection  = con;
                    _cmd.CommandText = "UPDATE tblpages set FB_PostID='" + _postResponse + "' where idtblpages =" + contatctid;
                    _cmd.ExecuteNonQuery();
                    con.Close();
                }

                ((Label)BlogTypeMyForm.FindControl("lblStatus")).Text = "פוסט נשלח בהצלחה!";
            }
            else
            {
                ((Label)BlogTypeMyForm.FindControl("lblStatus")).Text = "הייתה בעיה בשליחת הפוסט";
            }
        }
        else
        {
            ((Label)BlogTypeMyForm.FindControl("lblStatus")).Text = "לא בוצע התחברות לדף פייסבוק עדיין, אנא התחבר לדף";
        }

        //((Label)BlogTypeMyForm.FindControl("lblStatus")).Text = "פוסט נשלח בהצלחה - אנא לחץ שמור לשמירת הנתונים!";
        //((Label)BlogTypeMyForm.FindControl("lblStatus")).Text = _postResponse;

        hid_PostID.Value = _postResponse;

        //((Button)BlogTypeMyForm.FindControl("btnSavePost")).Visible = true;
    }
Exemple #3
0
    protected void PublishPost_Click(object sender, EventArgs e)
    {
        string token_test   = Session["token"].ToString();
        bool   itemSelected = false;

        fbPages_test = FacebookCode.GetFacebookPages(token_test);
        //FacebookApp app = new FacebookApp();
        string           album_id       = "";
        string           access_Token   = "";
        string           _pageName      = "";
        FacebookPagePost myPost         = new FacebookPagePost();
        bool             sendingSucceed = false;

        foreach (ListItem item in FacebookPagesList.Items)
        {
            if (item.Selected)
            {
                foreach (FacebookPage page in fbPages_test.data)
                {
                    if (item.Value == page.id)
                    {
                        itemSelected = true;
                        //myPost.id = page.id;
                        //myPost.access_token = page.access_token;
                        access_Token = page.access_token;
                        album_id     = page.id;
                        _pageName    = page.name;

                        break;
                    }
                }

                //myPost.message = TextToPost.Text.ToString().Replace("\n", "").Replace("\r", "").Replace("\"", "''");

                //string fbMsgID = FacebookCode.PostOnFacebookPage(myPost);

                sendingSucceed = true;
            }
        }
        if (itemSelected)
        {
            using (MySqlConnection con = new MySqlConnection(siteDefaults.ConnStr))
            {
                con.Open();
                MySqlCommand _cmd = new MySqlCommand();
                _cmd.Connection  = con;
                _cmd.CommandText = string.Format("UPDATE facebookpages set PageName='{0}', PageID='{1}', AccessToken='{2}' where FacebookPagesID=1", _pageName, album_id, access_Token);
                _cmd.ExecuteNonQuery();
                con.Close();
            }
            lbl_status.Text = "דף נשמר בהצלחה";
        }
    }
Exemple #4
0
    public static string PostOnFacebookPage(FacebookPagePost myPost)
    {
        string fbMsgID = "";

        HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create("https://graph.facebook.com/" + myPost.id + "/feed?access_token=" + myPost.access_token + "&message=" + myPost.message + (myPost.link != "" && myPost.link != null ? "&link=" + myPost.link : "")); //setting an httpWebRequest with the URL of the API

        webRequest.Method      = "POST";                                                                                                                                                                                                                                             //the type of method the API returns
        webRequest.Timeout     = 20000;                                                                                                                                                                                                                                              //sets the timeout for thew request
        webRequest.ContentType = "application/x-www-form-urlencoded";                                                                                                                                                                                                                //the content type. most of the times it will be application/x-www-form-urlencoded
        StreamReader MyStream     = new StreamReader(webRequest.GetResponse().GetResponseStream());                                                                                                                                                                                  //creating a stream reader to read the results from the API
        string       responseData = MyStream.ReadToEnd();                                                                                                                                                                                                                            //reading the result from the API into a string

        fbMsgID = responseData.Replace("{\"id\":\"", "").Replace("\"}", "").Substring(fbMsgID.LastIndexOf('_') + 1);

        return(fbMsgID);
    }
        public async Task <JsonResult> HasLikedPagePostAsync(string contentLink,
                                                             bool increasePointsIfLiked = false)
        {
            FacebookPagePost content = context.FacebookPagePosts.SingleOrDefault(c => c.Link == contentLink);
            List <string>    likers  = await content.GetLikersAsync();

            bool hasLiked = likers.Any(liker => liker == CurrentUser.Name);

            if (hasLiked && increasePointsIfLiked)
            {
                lock (CurrentUser)
                {
                    CurrentUser.Points += 10;
                }
                await context.SaveChangesAsync();
            }
            return(Json(hasLiked, JsonRequestBehavior.AllowGet));
        }