Reputation: 11999
I have integrated facebook login in my app. It works fine and in the callback method I get
This is my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
findViewById(R.id.sign_in_button).setOnClickListener(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("basic_info","user_friends","email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.e("res",""+loginResult.getAccessToken());
Toast.makeText(LoginActivity.this,""+loginResult.getAccessToken().getUserId(),Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
When I print the access token I get
{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[user_friends, contact_email, email, public_profile, basic_info]}
I was looking for on how to get the user's data from FB PROFILE DATA
The method
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
But it asks for a seesion. From where do I get the session?
Upvotes: 1
Views: 2523
Reputation: 5566
You can use available info, after the login, using Profile class.
Profile.getCurrentProfile() //get current instance of logged profile
Profile.getCurrentProfile().getId() //get current id
Profile.getCurrentProfile().getName() //get current user name
More about it, in documentation.
Upvotes: 1
Reputation: 6899
Here is complete code with status call back and session handling..
public class LoginFragment extends Fragment {
private LoginButton facebookButton;
private UiLifecycleHelper uiHelper;
//use this
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
try {
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
if (pDialog != null) {
onSessionStateChange(session, session.getState(), null);
}
}
uiHelper.onResume();
} catch (Exception e) {
Log.e(Constants.TAG_EXCEPTION, e.toString());
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
public void showLoggedInUserInfo(final Session session) {
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
fbFirstName = user.getFirstName();
fbLastName = user.getLastName();
fbEmail = user.asMap().get("email").toString();
}
}
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
showLoggedInUserInfo(session);
} else if (state.isClosed()) {
// Logged out
}
}
});
request.executeAsync();
}
Upvotes: 0