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 = "דף נשמר בהצלחה"; } }
protected void ClickToGetPages_Click(object sender, EventArgs e) { if (Session["token"] != null) { string userId = Session["userId"].ToString(); string token = Session["token"].ToString(); FacebookPages fbPages = FacebookCode.GetFacebookPages(token); foreach (FacebookPage page in fbPages.data) { FacebookPagesList.Items.Add(new ListItem(page.name, page.id)); } } }
/// <summary> /// This method gets facebook access Token and returns FacebookPages Type with user's list of pages /// </summary> /// <param name="accessToken">Identifing string produced by facebook when provided a code</param> /// <returns>FacebookPages Type with user's list of pages</returns> public static FacebookPages GetFacebookPages(string accessToken) { HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create("https://graph.facebook.com/me/accounts?access_token=" + accessToken); //setting an httpWebRequest with the URL of the API webRequest.Method = "GET"; //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 System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); //creating a JSON deserializer JSON. FacebookPages FbUserPages = serializer.Deserialize <FacebookPages>(responseData); //De-serealization of the string into an array of pre-defined objects return(FbUserPages); }