My app before used this code and it worked but it stopped work about yesterday, dont know why.
I can get name and picture. But email is null.
List<String> permisions = Arrays.asList(
"email");
Session.openActiveSession(getActivity(), true, permisions , new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String name = user.getFirstName();
String email = (String)user.getProperty("email");
System.out.println("Email: "+email);
System.out.println("Name: "+name);
}
}
The issue is that a user may have an email set in their Facebook profile but may set it to private meaning even though they have it setup, you can't retrieve it and in this case, you will get a null value. You might want to check for null first before using the property else the code flow will stop there and nothing will happen, no error or anything though you will get NPE. Check this: checking if facebook-sdk android giving null value for user data before calling getProperty()
Related
I'm working with Facebook's android SDK and I have a question.
So.. in the login flow, in the session callback we get the graph user and response and we can extract appropriate data from these fields.
But I want to fetch the user details every time the app is opened.
What is the correct way to achieve this without having the user to log in every time the app needs to fetch user data.
signInWithFacebookButton.setReadPermissions(Arrays.asList("public_profile", "email"));
signInWithFacebookButton.setSessionStatusCallback(new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session != null && state.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (response != null && user != null) {
new ParseLoginTask(user).execute();
}
facebookProgressDialog.dismiss();
}
}).executeAsync();
} else {
facebookProgressDialog.dismiss();
}
}
});
Login Updates in new Facebook SDK , So dont use session
Session Removed - AccessToken, LoginManager and CallbackManager classes supercede and replace functionality in the Session class.
Access Tokens - You can load AccessToken.getCurrentAccessToken with the SDK from cache or if the app is cold launched from an app bookmark. For instructions, see Facebook Login for Android, Get Current Token.
And use SharedPreferene to save User data .
Reference May Help you:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#reading https://developers.facebook.com/docs/graph-api/reference/user
I have a big problem with android-facebook-sdk 3.19.1. No problem to retrieve public information about user but it is not possible getting email address.
I know my post is very near to the following thread ==> facebook android sdk -user email returns null but I have no solution for now.
So here is my code
In my ConnectionActivity.java I have a button R.id.fb_connect
private List<String> permissions = Arrays.asList("email");
#Click(R.id.fb_connect)
protected void fbConnect() {
ensureOpenSession();
}
private boolean ensureOpenSession() {
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
LOGD(TAG, "Call Session.openActiveSession");
Session.openActiveSession(this, true, permissions, new FbCallback());
return false;
}
return true;
}
public class FbCallback implements Session.StatusCallback {
#Override
public void call(Session session, SessionState sessionState, Exception e) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// user.asMap().get("email");
Toast.makeText(ConnectionActivity.this, "email : " + user.getProperty("email"), Toast.LENGTH_LONG).show();
}
}
}).executeAsync();
}
}
I've also tried the code in the link above but it doesn't work. One precision, with this code, I can see the facebook activity which says "XXX will receive the following info: your public profile and email address" but the toast displays "email : null"
EDIT
I work with android studio and my gradle file has the following line
compile 'com.facebook.android:facebook-android-sdk:3.19.1'
EDIT2
If I use
LOGD(TAG, "isPermissionGranted : " + session.isPermissionGranted(permissions.get(0)));
before call Request.newMeRequest so true is shown. Permission is correctly granted
Thx
Prompt how to correctly use the method openActiveSessionWithAccessToken() in Fasebook API. Authorization pass, get token, and launch a dialogue posting, it appears (I can post). After cleaning token, and a closed session, the method of closeAndClearTokenInformation() I'm trying to open a session already having previously received the token, and the session is opened, there is a dialogue, but alas, writes that an error occurred try again later.
In the dialog box displays:
An error occurred. Please try again later.
and gives an error in the log:
Error Code 110,
Error Description: Invalid user id,
Error Message: Missing user cookie (to validate session user).
Here's the code:
public static void migrateFbTokenToSession() {
AccessToken accessToken = AccessToken.createFromExistingAccessToken(getAccessToken(), new Date(getAccessExpires()), null, AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, Arrays.asList(Constants.FB_APP_PERMISSIONS));
Session.openActiveSessionWithAccessToken(getContext(), accessToken , new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if(session != null && session.isOpened()) {
Session.setActiveSession(session);
}
}
});
}
You are removed preous session so how will you get access. You need to login again. If you want to maintain session then just use UiLifecycleHelper .
I did it with using createFromString method like this:
AccessToken accessToken = AccessToken.createFromString(savedTokenAsString, facebookPermissions, AccessTokenSource.NONE);
Session.openActiveSessionWithAccessToken(context, accessToken, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) { }
});
But you need to make createFromString method public, because its package-private in facebook sdk.
I'm developing an Android application, and I'm using this code to get some data from Facebook:
public void onFBLoginClick(View view)
{
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback()
{
// callback when session changes state
#Override
public void call(final Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback()
{
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
txtUserName.setText(session.getAccessToken());
saveUserData(user.getId(), user.getName(), user.getBirthday(), user.asMap().get("email").toString());
saveAccessToken(session.getAccessToken());
getFacebookUserProfilePicture(session.getAccessToken());
}
}
});
}
}
});
}
But, I can't get the email. Always is null.
I set up email permissions on User & Friend Permissions, on developers.facebook.com, but it doesn't work.
What am I doing wrong?
Use this code to open your session and you are ready to go!
String[] PERMISSION_ARRAY_READ = {"email","user_birthday"};
List<String> PERMISSION_LIST=Arrays.asList(PERMISSION_ARRAY_READ);
session.openForRead(new Session.OpenRequest(getParent()).setPermissions(PERMISSION_LIST).setCallback(statusCallback));
let me know if uyou have any problem regarding this.
I can log in fine the first time. And I have an option where the user can disable facebook within the android application. When this is selected the facebook status goes to CLOSED. When I use the option again, to log back in the API hangs at OPENING within the callback function.
Per the examples I've found the first call uses
session.openForRead(new Session.OpenRequest(activity).setCallback(statusCallback));
and the second uses
Session.openActiveSession(activity, true, statusCallback);
The callback function looks like this:
private static class SessionStatusCallback implements Session.StatusCallback {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
GlobalVars.setUser(user.getName());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GlobalVars.getContext());
Editor editor = prefs.edit();
editor.putBoolean("facebook", true);
editor.commit();
System.out.println("facebook logged in");
}
}
});
}
System.out.println("facebook session state callback " + session.getState().toString());
}
}
I'm not sure if we have the exact problem, but my facebook's session state retains "OPENING" after the Session.openActiveSession(activity, true, statusCallback);. I had overridden the onActivityResult() inside my fragment. What I did to solve this was to override the method on my Activity instead.
Related answer: https://stackoverflow.com/a/13888652/782870