Android FacebookSDK V4.20 Login and Logout with LoginManager
After spending days trying to figure this out, I couldn't think of anything that worked. Using my code, it shows me a permission form that I accept and register, but it doesn't get caught in onSuccess, onError, or onCancel. Every time I press the button it just doesn't do anything. And there are no errors in LogCat. I don't know where I am going wrong.
fb = (Button) findViewById(R.id.fb_button);
fb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("kkkkkk","kkllkl");
}
@Override
public void onCancel() {
Log.d("kkkkkk","kkllkl2");
}
@Override
public void onError(FacebookException error) {
Log.d("kkkkkk","kkllkl4");
}
});
}
});
FacebookSdk.sdkInitialize(this.getApplicationContext());
initialized after super.onCreate(savedInstanceState);
source to share
I finally figured it out. I modified the code from source code a bit:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("kkkkkk","kkllkl");
}
@Override
public void onCancel() {
Log.d("kkkkkk","kkllkl2");
}
@Override
public void onError(FacebookException error) {
Log.d("kkkkkk","kkllkl4");
}
});
and then in the button on the click listener, I just needed to make a login request.
fb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
}
});
also don't forget to add onActivityResult. I made a mistake.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
source to share
Below is the complete code.
public class MainActivity extends ActionBarActivity {
CallbackManager callbackManager;
Button but, but2;
AccessTokenTracker accessTokenTracker;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.button);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
}
@Override
public void onCancel() {
// App code
// savedInstanceState
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends" ));
// }
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
}
};
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getUserData(AccessToken.getCurrentAccessToken());
Log.d("Access Token: ", AccessToken.getCurrentAccessToken().toString());
}
});
}
public void getUserData(AccessToken accessToken){
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
//JSONParser parser=new JSONParser();
// Data data = new Gson().fromJson(json, Data.class);
long ID = 0;
try {
String name = response.getRawResponse();
Toast.makeText(MainActivity.this, "response is: "+object.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "error is: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
}
}
source to share