私は Java アプリケーションを作成しています。ログインして自分のウォール (自分のウォールのみ) といくつかのグループにアクセスして、何か新しいものが投稿されているかどうかを確認できるようにしたいと考えています。これは製品コードではありません。これは私のためだけです。問題は、URLConnection を使用しているときにログインする方法がわからないことです。ログインしていない場合は、Facebook のグループを開くことができます。ただし、非公開のグループを表示するにはログインする必要があります。これは Web アプリではありません。これは、接続を確立して html をプルダウンし、それを解析するだけです。
私の友人は、facebook にはこの目的のための OAuth 機能があると言っていますが、インターネットを読んだ後、OAuth はアプリケーションにログインするため、またはクライアントにログインしてそれらを検証し、おそらくそれらのウォールに投稿するためのもののようです。しかし、おそらく私はこれを完全に間違った方法で行っているだけで、oauthとグラフAPIなどを使用する必要があります...
とにかく、URLConnection を作成して facebook にリクエストを送信し、すべての Cookie を保存してから、Facebook にログインするためのすべてのヘッダーとその他のものを使用して新しい接続を作成しようとしましたが、実際のユーザーではなく自動化されたプログラムであり、動作していません。認証を受ける方法を知っている人はいますか?
String cookies = "";
URL myUrl = new URL("https://www.facebook.com/");
URLConnection urlConn = myUrl.openConnection();
urlConn.connect();
String headerName=null;
for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = urlConn.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
cookies = cookies+cookieName+"="+cookieValue+"; ";
cookieNameList.add(cookieValue);
}
}
URL url = new URL("https://www.facebook.com/login.php?login_attempt=1");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Cookie", cookies);
conn.setRequestProperty("accept-encoding", "gzip,deflate,sdch");
conn.setRequestProperty("accept-language", "en-US,en;q=0.8");
conn.setRequestProperty("cache-control", "max-age=0");
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty("accepts-encoding", "gzip,deflate,sdch");
conn.setRequestProperty("accept-encoding", "gzip,deflate,sdch");
conn.setRequestProperty("accept-encoding", "gzip,deflate,sdch");
String query = "lsd=AVpGDi9X&email=xxxxxxxx%40gmail.com&pass=xxxxxxxxx&persistent=1&default_persistent=1&timezone=360&lgnrnd=135940_imAl&lgnjs=1375995581&locale=en_US";
OutputStream output = null;
try {
output = conn.getOutputStream();
output.write(query.getBytes("UTF-8"));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
try{
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
}catch(Exception e){
System.out.print("URL BROKE!");
}