简体   繁体   English

Facebook桌面应用程序C#

[英]facebook desktop app C#

I am trying to build a desktop app to use facebook api and get data from friends. 我正在尝试构建一个桌面应用程序以使用facebook api并从朋友那里获取数据。 Anyways I am stuck in the log in stage. 无论如何,我一直处于登录阶段。 I have used some advice and made the log in to facebook with WebBrowser. 我使用了一些建议,并使用WebBrowser登录到Facebook。 It works great. 效果很好。 I am stuck at trying to make it give me status = Failed or success 我一直在努力使它成为我的状态=失败或成功

I tried doing it like this at the end of the button_1 method 我尝试在button_1方法末尾这样做

if (!w.DocumentText.Contains(@"<div class=""linkWrap noCount"">Messages</div>"))
    {
        w.Navigate(@"http://www.facebook.com/login.php");
        MessageBox.Show("Login error. Wrong username or password!");
    }

    else
    {
       MessageBox.Show("Logged in successfully");

    }

the < div class=""linkWrap noCount"">Messages< /div> is only shown while logged in so thats why I use it to see if a user is logged in <div class =“” linkWrap noCount“”>消息</ div>仅在登录时显示,所以这就是为什么我用它查看用户是否已登录

but the problem is it always gives me an error (wrong user and pass) becasue it reads it before the browser finishes to navigate to the page. 但是问题是它总是给我一个错误(错误的用户名和密码),因为它在浏览器完成导航到页面之前就读取了它。 I tried threads and thread sleep and even timers but it doesnt seem to work 我尝试了线程和线程睡眠,甚至计时器,但它似乎不起作用

an ideas? 一个想法?

here is the code: 这是代码:

private void button1_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
        thread.Start();

         string email = textBox1.Text;
    string password = textBox2.Text;

    // create a new browser
    WebBrowser w = new WebBrowser();
    w.Dock = DockStyle.Fill;
    this.Controls.Add(w); // you may add the controll to your windows forms if  you want to see what is going on
    // latter you may not chose to add the browser or you can even set it to invisible... 


    // navigate to facebook
    w.Navigate(@"http://www.facebook.com/login.php");

    // wait a little
    for (int i = 0; i < 100; i++)
    {
        System.Threading.Thread.Sleep(10);
        System.Windows.Forms.Application.DoEvents();
    }


    HtmlElement temp=null;

    // while we find an element by id named email
    while (temp == null)
    {
        temp = w.Document.GetElementById("email");
        System.Threading.Thread.Sleep(10);
        System.Windows.Forms.Application.DoEvents();
    }

    // once we find it place the value
    temp.SetAttribute("value", email);


    temp = null;
    // wiat till element with id pass exists
    while (temp == null)
    {
        temp = w.Document.GetElementById("pass");
        System.Threading.Thread.Sleep(10);
        System.Windows.Forms.Application.DoEvents();
    }
    // once it exist set its value equal to passowrd
    temp.SetAttribute("value", password);

    // if you already found the last fields the button should also be there...

    var inputs = w.Document.GetElementsByTagName("input");

    int counter = 0;
    bool enableClick = false;

    // iterate through all the inputs in the document
    foreach (HtmlElement btn in inputs)
    {

        try
        {
            var att = btn.GetAttribute("tabindex");
            var name = btn.GetAttribute("id");

            if (enableClick)// button to submit always has a differnt id. it should be after password textbox
            {
                btn.InvokeMember("click");
                counter++;
            }

            if (name.ToUpper().Contains("PASS") || att=="4") 
            {
                enableClick = true;  // button should be next to the password input                    
            }

            // try a max of 5 times
            if (counter > 5)
            {
                break;

            }
        }
        catch
        {


        }


                }






    }

Checkout the facebook-sharp SDK for Windows forms: 签出Windows窗体的facebook-sharp SDK:

https://github.com/facebook-csharp-sdk/facebook-winforms https://github.com/facebook-csharp-sdk/facebook-winforms

I recommend you use Facebook C# SDK . 我建议您使用Facebook C#SDK It uses the OAuth protocol , for user-authentication . 它使用OAuth协议 进行用户身份验证

Down an code example how to get user friends with Facebook-C#-SDK: 下一个代码示例如何使用Facebook-C#-SDK:吸引用户朋友Facebook-C#-SDK:

using Facebook; //add reference to facebook dll for it work

declare the fields: 声明字段:

private FacebookOAuthResult result;
private FacebookOAuthClient OAuth;

and

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            if (webBrowser1.Url.AbsolutePath == "/login.php")
            {
                     // do login..      
            }

            if (FacebookOAuthResult.TryParse(e.Url, out result))
            {
                if (result.IsSuccess)
                {
                  FacebookClient fbClient = new FacebookClient(result.AccessToken);
                  dynamic friends = fbClient.Get("/me/friends"); //User friends
                  // do something.. 
                }
                else
                {
                    string errorDescription = result.ErrorDescription;
                    string errorReason = result.ErrorReason;
                    string msg = String.Format("{0} ({1})", errorReason, errorDescription);
                    MessageBox.Show(msg, "User-authentication failed!");
                }
            }
        }

and then for start user-authentication: 然后用于启动用户身份验证:

//.. 

    OAuth = new FacebookOAuthClient();
    OAuth.AppId = appId; // see link above,you can find how to get it
    OAuth.AppSecret = appSecret; // see link above,you can find how to get it

    Uri loginUrl = OAuth.GetLoginUrl(paramenters);
    webBrowser1.Navigate(loginUrl.AbsoluteUri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM