huuanh1987 / facebook-java-api

Automatically exported from code.google.com/p/facebook-java-api
0 stars 0 forks source link

How to login facebook from a java application without browser and httpclient? #202

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I set up an application and its “Canvas Callback URL” in facebook.
I can call APIs of facebook-java-api 2.1.0 to access facebook website 
given the APIKEY, and the SECERT.
Now what my problem is login facebook. The latest test 
bed "FacebookSessionTestUtils" gives an example to authenticate a user 
given a username and password without asking the user to open a browser. 
It uses Apache HttpClient to simulate the browser interaction.
But For some reason, I cannot add other third-part libraries (such as 
commons-httpclient, commons-codes and commons-logging) to my project.  I 
tried to simulate the same function using "java.net.HttpURLConnection" to 
login, but I failed.
My steps:
1. After I get a token via auth_createToken(), I do:
URL url = new URL(http://www.facebook.com/login.php?
api_key=MyAPIKey&v=1.0&auth_token=token);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection
();   

2. Analyzing “getRequestHeaders()” after doing http.executeMethod(get), 
and set the same request method to HttpURLConnection:
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 
(compatible; MSIE 7.0; Windows NT 5.1; GTB6)");
httpURLConnection.setRequestProperty("Host", "www.facebook.com");

As a result, I can see “Set-Cookie” via “getResponseHeaders()” after 
facebook response. I save the cookies for POST.

3. Analyzing “getRequestHeaders” after doing http.executeMethod(post), and 
set the same request Method to HttpURLConnection:
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 
(compatible; MSIE 7.0; Windows NT 5.1; GTB6)");
httpURLConnection.setRequestProperty("Host", "www.facebook.com");
httpURLConnection.setRequestProperty("Cookie", xxxxxxxxxxxxxxxxxxxxxxx);
String paramString = 
api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&auth_token=xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxx&email=xxxxxxxx%40gmail.com&pass=xxxxxxx&v=1.0 (which encoded by 
UTF8)
String size = Integer.toString(paramString.length());
httpURLConnection.setRequestProperty("Content-Length", size);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-
form-urlencoded");

Connect and write params:
httpURLConnection.setDoOutput(true);   
httpURLConnection.connect();
httpOutputStream = httpURLConnection.getOutputStream();   
httpOutputStream.write(paramString.toString().getBytes(“UTF-8”));
httpOutputStream.flush();

And then I get the response:
StringBuffer httpResponse = new StringBuffer();         
BufferedReader httpBufferedReader = new BufferedReader(new 
InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
httpResponse.append(this.readBufferedContent(httpBufferedReader));
System.out.println(httpResponse.toString());

What is the expected output? What do you see instead?
I logged in failing, and couldn’t auth_getSessionId(token); 
I saved the “httpResponse” string as html file, and opened it by IE. 
It said that my browser didn’t support cookies, and let me change my 
security setting before continuing.
But I could log in www.facebook.com via my IE successfully. Anyway, I 
reset my IE’s security settings and changed it to low level in order to 
allow all cookies and added "www.facebook.com" as a “Trusted Site”. But 
the result was the same. 

What version of the product are you using? On what operating system?
JDK 1.3 and  Windows XP.

Please provide any additional information below.
Actually, what I really want to do is just to replace the codes by APIs of 
JRE libraries.

HttpClient http = new HttpClient();
http.setParams(new HttpClientParams());
http.setState(new HttpState());

final String LOGIN = "http://www.facebook.com/login.php";

GetMethod get = new GetMethod(LOGIN + "?api_key=" + APIKEY 
+ "&v=1.0&auth_token=" + token );

http.executeMethod(get);

PostMethod post = new PostMethod(LOGIN);
post.addParameter(new NameValuePair("api_key", APIKEY));
post.addParameter(new NameValuePair("v", "1.0"));
post.addParameter(new NameValuePair("auth_token", token));
post.addParameter(new NameValuePair("email", "xxxxx@gmail.com"));
post.addParameter(new NameValuePair("pass", "xxxxxx"));

http.executeMethod(post);

Original issue reported on code.google.com by 771M.F...@gmail.com on 1 May 2009 at 3:04

GoogleCodeExporter commented 8 years ago
Complementarity:
For the step 3, I new the url is http://www.facebook.com/login.php?

Original comment by 771M.F...@gmail.com on 1 May 2009 at 3:37

GoogleCodeExporter commented 8 years ago
Please re-post this on the forum. HttpClient handles redirects / cookies but it
should be straightforward to replace it with HttpURLConnection etc. if you 
really
want to.

Original comment by david.j....@googlemail.com on 1 May 2009 at 8:43

GoogleCodeExporter commented 8 years ago
Thanks. I've resolved the issue.
HTTP GET responses 5 cookies, I should call addRequestProperty(cookie, ...);, 
not 
setRequestProperty(cookie, ...);
Now my project works fine:)

Original comment by 771M.F...@gmail.com on 2 May 2009 at 6:22

GoogleCodeExporter commented 8 years ago
Hi,
I followed you posts here, but I did not understand what values to set for the 
cookies on the second POST request. 
is it httpURLConnection1.addRequestProperty("Cookie", "$Version=0; 
test_cookie=1; 
$Path=/; $Domain=.facebook.com");

I fail getting the sessionSecret after getting the cookies and posting http 
POST 
request to http://www.facebook.com/login.php?

I get the following Exception when calling client.auth_getSession
java.lang.RuntimeException: org.json.JSONException: JSONObject["session_key"] 
not 
found.

Can any one give example.

Thanks

Ohad.

Original comment by ohad.s.p...@gmail.com on 30 May 2010 at 7:56