Facebook SDK 4+ logout not working correctly - android

I'm using Facebook SDK 4+ and my problem is that when i use
FacebookSdk.sdkInitialize(getApplicationContext());
LoginManager.getInstance().logOut();
to logout from my app it's not working correct. I return to my target activity with the login button. When i press the button i get in the app again without authorization. I have installed the native Facebook app. Without this app erverything is working fine. So what i'm doing wrong or im missing something.
The Api Key where ok and they are working.
my login implementaion is like:
private ImageButton loginButton;
private LoginButton fbbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
}
#Override
protected void onResume() {
super.onResume();
callbackManager= CallbackManager.Factory.create();
fbbtn= (LoginButton)findViewById(R.id.fb_button);
fbbtn.setReadPermissions("public_profile", "email","user_friends");
loginButton = (ImageButton) findViewById(R.id.login_btn);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
fbbtn.performClick();
fbbtn.setPressed(true);
fbbtn.invalidate();
fbbtn.registerCallback(callbackManager, mCallBack);
fbbtn.setPressed(false);
fbbtn.invalidate();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
progressDialog.dismiss();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
app.setUser(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
progressDialog.dismiss();
}
#Override
public void onError(FacebookException e) {
progressDialog.dismiss();
}
};

To change user profile you need logged out from it in your FB app (or in Chrome if you are using Chrome Custom Tabs)

Related

Android Facebook login cannot be loaded due to missing permissions

I tried integrating login with facebook in android with calling registerCallBack from Facebook LoginButton and it worked perfectly. But I wanted to customize my facebook login button with an ImageButton, so I tried performClick() when clicking on my customized ImageButton as below
callbackManager = CallbackManager.Factory.create();
LoginButton btnSignInFacebook = findViewById(R.id.btn_social_sync_facebook);
List<String> permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
btnSignInFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
startActivity(new Intent(SocialAccountsSyncActivity.this, AccountInformationSignUpActivity.class));
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()) {
//my customized imagebutton
case R.id.ib_social_sync_facebook:
btnSignInFacebook.performClick();
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
This kept giving me this error:
Unsupported get request. Object with ID '668803530142270' does not exist, cannot be loaded due to missing permissions, or does not support this operation.
Anyone knows the reason please help me with it. Thanks

Initiate login activity by one click

I am working on a custom button which enable Facebook login. What I am coding gave me a result which I need to tap the button twice only it will start the activity.
I was trying to work on it through adjusting the position of startActivity() function in different way. And I found that I can make it to become initialization through placing the startActivity() function just after the finding of view for button bLogin.
Logically, it seems not right as no user will start to proceed to the next activity before login.
Any solution on this? Thank you in advance.
For your information, here are my MainActivity java code.
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
GraphRequest graphRequest;
com.facebook.login.LoginManager fbLoginManager;
Button bLogin;
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//new activity from login page
public void chatbox (View view){
final Intent intent = new Intent(this, tabviewChatbox.class);
fbLoginManager = com.facebook.login.LoginManager.getInstance();
callbackManager = CallbackManager.Factory.create();
bLogin = (Button) findViewById(R.id.bLogin);
//only start activity if login success
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
graphRequest(loginResult.getAccessToken());
startActivity(intent);
finish();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
bLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
}
});
}
public void graphRequest(AccessToken accessToken) {
graphRequest = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("response", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,birthday,friends");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
//Function calling for integration of font awesome, can directly put icon inside default button by typing name
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(IconicsContextWrapper.wrap(newBase));
}
}

How could i send user to login page in my android application if user changed the facebook password

Im using facebook sdk for login in my android application. i have successfully login with facebook but i have changed the password in facebook then the logged android application should prompt for login again. i have followed the documentation https://developers.facebook.com/docs/facebook-login/android it shows only login procedure .
public class UserLoginActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginButton loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions("public_profile");
// Other app specific specialization
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Toast.makeText(UserLoginActivity.this, "success login", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
I have attached code, please tell me what to do here.
Read out for ProfileTracker Facebook and also check AccessTokenTracker Facebook for more control.
Try:
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
}
} ;
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
}
};

Want FB login functionality with customize button working both when FB App is installed and when not?

I am sick of finding and searching this thing. I want a FB login function working on a Button click. I dont want FB login Button for this, plus which FB SDK to use for it,latest one i.e 4.8 doesn't seems to use Session for login. I have tried FB Login Manager also,but sonetimes not gives authentication when native FB app is installed and if then not providing me email.(I have tested it). Please Please provide me a way which FB SDK I use to make my work done. I have use Session but it gives graph Object null when FB app is installed. thanks and please guide me on this.
Change the code accordingly in the CustomCallback class.
public class MainActivity extends AppCompatActivity {
private Button facebook;
private LoginManager loginManager;
private CustomFacebookCallback callback;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Facebook initialize
FacebookSdk.sdkInitialize(MainActivity.this);
FacebookSdk.addLoggingBehavior(LoggingBehavior.GRAPH_API_DEBUG_INFO);
FacebookSdk.addLoggingBehavior(LoggingBehavior.DEVELOPER_ERRORS);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_RAW_RESPONSES);
setContentView(R.layout.activity_main);
// Init button
facebook = (Button) findViewById(R.id.facebook);
// Instantiate parts and variables
callback = new CustomFacebookCallback();
// Callback Manager initialize
callbackManager = CallbackManager.Factory.create();
//===================
// Facebook Login
//===================
// 1) Permissions
final ArrayList<String> permissions = new ArrayList<String>();
permissions.add("email");
permissions.add("public_profile");
permissions.add("user_friends");
// 2) Set login function to button
facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loginManager = LoginManager.getInstance();
loginManager.logInWithReadPermissions(MainActivity.this, permissions);
loginManager.registerCallback(callbackManager, callback);
}
});
}
private class CustomFacebookCallback implements FacebookCallback<LoginResult> {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.d("Result : " , object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,email");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
Log.d("Facebook", "Canceled!");
}
#Override
public void onError(FacebookException e) {
Log.e("Facebook", "Error!");
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}

Switch to another activity after successful Facebook login in Android App

I am new to Adroid app development and started learning for about a week.
But since 3 days i am struggling with switching the activity after successful facebook login.
My questions :
1) I need to redirect to my another activity once the user is logged in.
2) I need to have their profile details like profile picture, email and name etc on the next activity frame (which will come after login).
Current State : After login, fragment stays as it is and Logout button is displayed.
My code for the fragment here :
public class LoginFragment extends Fragment {
private CallbackManager mCallbackManager;
public AccessTokenTracker mAccessTokenTracker;
public ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accesstoken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
Log.d("get me profile", "Name");
//Log.d("Welcome :", profile.getName());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public LoginFragment(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
AccessTokenTracker mAccessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldtracker, AccessToken newtracker) {
}
};
mAccessTokenTracker.startTracking();
ProfileTracker mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldprofile, Profile newprofile) {
//Log.d("New Name", "data");
}
};
mProfileTracker.startTracking();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.login_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager, mCallback);
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
}
#Override
public void onDestroy() {
super.onDestroy();
mAccessTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Request your kind words guys to make this work.
For Answering your first question
1) In the FacebookCallback:onSuccess method you will can the method which will opens the next Activity needed, and in the onCreate of the Fragment, you will check if the AccessToken is Null, if it is null then do nothing to wait for User to press login, if it is not Null, then call the same method get open the Activity needed as the user is already logged in
2) you have check the Documents and permissions to get the Profile picture and name for the current logged user, and if you have questions, please write the code you use first to get User data

Categories

Resources