I am using facebook connect in my android app. It is basically a form. There is a facebook login button on the page. I want the information on the form to be filled in when thee user logs in using Facebook login.
I am trying the following code:
public void loginFB() {
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
@Override
public void onComplete(Bundle values) {}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
getlogininfo();
}
public void getlogininfo(){
mAsyncRunner.request("me", new RequestListener(){
@Override
public void onComplete(String response, Object state) {
try{
Log.d("Profile", response.toString());
JSONObject json = Util.parseJson(response);
final String fname1 = json.getString("first_name");
final String lname1 = json.getString("last_name");
final String email = json.getString("email");
MintPaymentsActivity.this.runOnUiThread(new Runnable(){
public void run() {
fname.setText(fname1);
lname.setText(lname1);
emailadd.setText(email);
}
});
}
catch(JSONException e){
Log.w("This", "eror");
}
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
});
}
/*FB --- */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
basically, when the user clicks the button, faceboook asks for login and then collects the JSON data to display the name and email of the logged in user.
It gives me the following error:
05-30 21:52:40.598: E/AndroidRuntime(665): FATAL EXCEPTION: Thread-13
05-30 21:52:40.598: E/AndroidRuntime(665): com.facebook.android.FacebookError: An active access token must be used to query information about the current user.
05-30 21:52:40.598: E/AndroidRuntime(665): at com.facebook.android.Util.parseJson(Util.java:279)
05-30 21:52:40.598: E/AndroidRuntime(665): at com.Myapp.Myapp$2.onComplete(Myapp.java:117)
05-30 21:52:40.598: E/AndroidRuntime(665): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)
When I put an extra button to fill in the details, so that the getlogininfo() is implemented on a button click (after logging in) it works fine. But i want it to be filled in as soon as the user clicks the facebook login button.
It should be:
You want to execute
getLogininfoonly after the authentication has completed.Also, I suggest that you handle the errors as well, at least log it so you’ll know when developing.