如何使用 Java 以编程方式登录 Facebook?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2285250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:39:43  来源:igfitidea点击:

How to log into Facebook programmatically using Java?

javahtmlfacebookcookiespost

提问by tree-hacker

I'm trying to write a Java program that can automatically log into Facebook.

我正在尝试编写一个可以自动登录 Facebook 的 Java 程序。

I've got the below code so far that downloads the home html page into a String but don't know how to send the email and password to log into Facebook? Also will the Java program need to handle cookies returned to remain logged in?

到目前为止,我有以下代码将主页 html 页面下载到字符串中,但不知道如何发送电子邮件和密码以登录 Facebook?Java 程序是否还需要处理返回的 cookie 以保持登录状态?

public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.facebook.com/");
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc
                .getInputStream()));
        String inputLine;
        String allInput = "";

        while ((inputLine = in.readLine()) != null) {

            allInput += inputLine + "\r\n";
        }
        System.out.println(allInput);

        in.close();
    }

}

}

Update:

更新:

I've tried the below code using htmlUnit however I get the following exception:

我已经使用 htmlUnit 尝试了以下代码,但是出现以下异常:

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException:     elementName=[form] attributeName=[name] attributeValue=[login_form] at com.gargoylesoftware.htmlunit.html.HtmlPage.getFormByName(HtmlPage.java:588)

Anyone know why this is?

有谁知道这是为什么?

    final WebClient webClient = new WebClient();
    final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
    final HtmlForm form = page1.getFormByName("login_form");

    final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Login").get(0);
    final HtmlTextInput textField = form.getInputByName("email");
    textField.setValueAttribute("[email protected]");
    final HtmlTextInput textField2 = form.getInputByName("pass");
    textField2.setValueAttribute("ahhhh");
    final HtmlPage page2 = button.click();

采纳答案by Jon

You should take a look at HTMLUnit, it'll be much simpler than using the above. The following page and code should guide you:

你应该看看 HTMLUnit,它会比使用上面的简单得多。以下页面和代码应指导您:

final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
final HtmlForm form = page1.getFormByName("login_form");

final HtmlSubmitInput button = form.getInputsByValue("Log in");
final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("[email protected]");
final HtmlTextInput textField = form.getInputByName("pass");
textField.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();

http://htmlunit.sourceforge.net/gettingStarted.html

http://htmlunit.sourceforge.net/gettingStarted.html

回答by Luca

There are some problems in your code

您的代码中存在一些问题

  1. is that login_formis not the form name but the form ID
  2. the submit button value i Log In
  3. type of password field is HtmlPasswordInput
  1. login_form不是表单名称而是表单 ID
  2. 提交按钮值 i Log In
  3. 密码字段的类型是 HtmlPasswordInput

so:

所以:

final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
final HtmlForm form = (HtmlForm) page1.getElementById("login_form");

final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0);
final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("[email protected]");
final HtmlPasswordInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();