En primer lugar se crea la aplicación y obtener el id de aplicación a continuación, pasa esta identificación a su código como este. Inicializar a nivel mundial
public static final String mAPP_ID = "Your APP ID";
Facebook mFacebook= new Facebook(mAPP_ID);
y setOnClickListener en el botón dentro en Create()
// facebook login button click event
try{
//mFacebook.logout(LoginActivity.this);
((Button)findViewById(R.id.loginPageFaceBookButton)).setOnClickListener(loginButtonListener);
SessionStore.restore(mFacebook,LoginPage.this);
} catch (Exception e) {
Toast.makeText(LoginPage.this,"Exception"+e.toString(), Toast.LENGTH_SHORT).show();
}
// loginButtonListener
//----------------------------------------------
private OnClickListener loginButtonListener = new OnClickListener()
{
public void onClick(View v)
{
if(!mFacebook.isSessionValid())
{
mFacebook.authorize(LoginPage.this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
else
{
try
{
JSONObject json = Util.parseJson(mFacebook.request("me"));
facebookID = json.getString("id");
facebookEmail = json.getString("email");
faceBooklastName=json.getString("last_name");
faceBookFirstName=json.getString("first_name");
}
catch (Exception e)
{
// TODO: handle exception
//Toast.makeText(LoginActivity.this,"Exception FB "+e.toString(), Toast.LENGTH_SHORT).show();
}
catch(FacebookError error)
{
Toast.makeText(LoginPage.this,error.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
//onActivityResult
//***********************************************************
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
// DialogListener CLASS STATRT HERE.
public final class LoginDialogListener implements DialogListener
{
public void onComplete(Bundle values)
{
try
{
JSONObject json = Util.parseJson(mFacebook.request("me"));
facebookID = json.getString("id");
facebookEmail = json.getString("email");
SessionStore.save(mFacebook, LoginPage.this); Toast.makeText(LoginPage.this,"facebookID :"+facebookID+" \n "+"facebookEmail : "+facebookEmail, Toast.LENGTH_SHORT).show();
}
catch(Exception error)
{
Toast.makeText(LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
}
catch(FacebookError error)
{
Toast.makeText(LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText(LoginPage.this, "Something went wrong. Please try again.1"+error.toString(), Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText(LoginPage.this, "Something went wrong. Please try again.2"+error.toString(), Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText(LoginPage.this, "Something went wrong. Please try again.3", Toast.LENGTH_LONG).show();
}
/****** Facebook Login End *******/
}
Alguna información como la dirección de correo electrónico requiere agregar un indicador de permiso específico a la solicitud de autenticación. Ver http://stackoverflow.com/questions/3611682/facebook-graph-api-how-to-get-users-email – larham1