Example #1
0
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.Url.AbsoluteUri.Contains("access_token"))
            {
                //authentcation is successfull
                string url1  = webBrowser1.Url.AbsoluteUri;
                string url2  = url1.Substring(url1.IndexOf("access_token", StringComparison.Ordinal) + 13);
                string token = url2.Substring(0, url2.IndexOf("&", StringComparison.Ordinal));
                AccessToken = token;
                //save to database
                FacebookApiEntities db = new FacebookApiEntities();

                FacebookClient fbclient = new FacebookClient(token);
                dynamic        account  = fbclient.Get("/me?fields=email,name");

                AccountTbl acc = db.AccountTbls.Find(account.id);
                if (acc != null)
                {
                    acc.AccEmail = account.email;
                    acc.AccName  = account.name;
                    acc.AccToken = token;
                }
                else
                {
                    AccountTbl NewAcc = new AccountTbl();
                    NewAcc.AccEmail = account.email;
                    NewAcc.AccName  = account.name;
                    NewAcc.AccToken = token;
                    NewAcc.AccID    = account.id;
                    db.AccountTbls.Add(NewAcc);
                }

                db.SaveChanges();
            }
        }
Example #2
0
        private void BtnUpdateManagedPages_Click(object sender, EventArgs e)
        {
            FacebookApiEntities db       = new FacebookApiEntities();
            FacebookClient      fbclient = new FacebookClient(SelectedToken);
            dynamic             Mypages  = fbclient.Get("/me/accounts?fields=access_token,name,id,fan_count&limit=1000");
            int counter = Convert.ToInt32(Mypages.data.Count);

            //save to database
            for (var i = 0; i < counter; i++)
            {
                PagesTbl pg = db.PagesTbls.Find(Mypages.data[i].id);

                if (pg != null)
                {
                    //page already exists then update
                    pg.PageName  = Mypages.data[i].name;
                    pg.PageToken = Mypages.data[i].access_token;
                    pg.fans      = Mypages.data[i].fan_count;
                }
                else
                {
                    //create a record for the page
                    PagesTbl Newpage = new PagesTbl();
                    Newpage.PageName        = Mypages.data[i].name;
                    Newpage.PageToken       = Mypages.data[i].access_token;
                    Newpage.fans            = Mypages.data[i].fan_count;
                    Newpage.PageID          = Mypages.data[i].id;
                    Newpage.ParentAccountID = SelectedAccID;
                    db.PagesTbls.Add(Newpage);
                }

                db.SaveChanges();

                //load to our listview

                listViewManagedPages.Items.Clear();

                foreach (PagesTbl page in db.PagesTbls.ToList())
                {
                    ListViewItem lsitem = new ListViewItem();
                    lsitem.Text = page.PageName;
                    lsitem.SubItems.Add(page.fans.ToString());
                    lsitem.SubItems.Add(page.PageID);
                    lsitem.SubItems.Add(page.PageToken);
                    listViewManagedPages.Items.Add(lsitem);
                }
            }
        }