Hi All,
I am for an application which extracts information from facebook search, hence i require to login facebook. i have registered my application in facebook developers site and have got api key and secret key. in my code i am getting an exception when i am trying to login. Here is my code for login to facebook:
import com.facebook.api.FacebookRestClient;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
public class FaceLogin {
public FaceLogin(){
getUserID("username", "password");
}
private static void getUserID(String email, String password)
{
String session = null;
try {
HttpClient http = new HttpClient();
http.setParams(new HttpClientParams()); //http.getHostConfiguration().setHost("http://www.facebook.com/");
http.setState(new HttpState());
String api_key = "****some key****";
String secret = "****some key****";
FacebookRestClient client = new FacebookRestClient(api_key, secret);
client.setIsDesktop(true);
String token = client.auth_createToken();
final String loginId = "http://www.facebook.com/login.php";
GetMethod get = new GetMethod(loginId + "?api_key=" + api_key + "&v=1.0&auth_token=" +token);
System.out.println("Get="+get);
http.executeMethod(get);
PostMethod post = new PostMethod(loginId);
post.addParameter(new NameValuePair("api_key", api_key));
post.addParameter(new NameValuePair("v", "1.0"));
post.addParameter(new NameValuePair("auth_token", token));
post.addParameter(new NameValuePair("fbconnect","true"));
post.addParameter(new NameValuePair("return_session","true"));
post.addParameter(new NameValuePair("session_key_only","true"));
post.addParameter(new NameValuePair("req_perms","read_stream,publish_stream"));
post.addParameter(new NameValuePair("lsd","8HYdi"));
post.addParameter(new NameValuePair("locale","en_US"));
post.addParameter(new NameValuePair("persistent","1"));
post.addParameter(new NameValuePair("email", email));
post.addParameter(new NameValuePair("pass", password));
System.out.println("Token ="+token);
int postStatus = http.executeMethod(post);
System.out.println("Response : " + postStatus);
session = client.auth_getSession(token); // Here I am getting error
System.out.println("Session string: " + session);
long userid = client.users_getLoggedInUser();
System.out.println("User Id is : " + userid);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String k[])
{
FaceLogin facebookLoginObj=new FaceLogin();
}
}
I am getting the following exception:
org.apache.commons.httpclient.HttpMethodBase processResponseHeaders
WARNING: Cookie rejected: "$Version=0; $Domain=deleted; $Path=/; $Domain=.facebook.com". Cookie name may not start with $
Response : 200
Jun 8, 2010 2:07:36 PM org.apache.commons.httpclient.HttpMethodBase processResponseHeaders
WARNING: Cookie rejected: "$Version=0; $Path=deleted; $Path=/; $Domain=.facebook.com".
Cookie name may not start with $
Facebook returns error code 100
com.facebook.api.FacebookException: Invalid parameter
- v - 1.0
at com.facebook.api.FacebookRestClient.callMethod(FacebookRestClient.java:828)
- auth_token - 004e90dc8818d5f0921d1065d24508d3
at com.facebook.api.FacebookRestClient.callMethod(FacebookRestClient.java:606)
- method - facebook.auth.getSession
- call_id - 1275986256796
- api_key - f7cb1e48c383ef599da9021fc4dec322
at com.facebook.api.FacebookRestClient.auth_getSession(FacebookRestClient.java:1891)
at facebookcrawler.FacebookLogin.getUserID(FacebookLogin.java:81)
at facebookcrawler.FacebookLogin.( - sig - 9344ec75b74a0a87bcae645046d45da8
FacebookLogin.java:24)
at facebookcrawler.FaceLogin.main(FaceLogin.java:80)
Here may be the problem is for creating session, i searched for all the solutions on net but could not helped me to get login.
Please help me if you can suggest me some way to resolve this problem. i thanks to all your valuable suggestion.