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.
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've got this code (which was working in 11/2014) called from a button click:
Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(final GraphUser user, final Response response) {
if (user != null) {
System.out.println(user);
preference.edit().putString("fb_nome",user.getName()).commit();
preference.edit().putString("fb_id",user.getId()).commit();
preference.edit().putString("fb_email",user.asMap().get("email")+"").commit();
if(user.getLocation()!=null) {
preference.edit().putString("fb_citta", user.getLocation().asMap().get("name") + "").commit();
}
Session session = Session.getActiveSession();
preference.edit().putString("fb_token",session.getAccessToken()).commit();
new loginFacebook().execute();
}
}
}).executeAsync();
}
}
});
latest sdk and so on.
the problem is that facebook doesn't give me the email and the city anymore!
i'm sure it was working on 11/2014, but now no.
email is null and also the city...
someone can help me?
thanks
Are you setting proper permissions?
facebookLoginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
plus the way to get the user e-mail is:
String email = (String) user.getProperty("email")
Hope this helps, works as intended in all my apps.
Solved with this (green V) question: how to get email id from facebook sdk in android applications?
basically:
List<String> permissions = new ArrayList<String>();
permissions.add("email");
Session.openActiveSession(this, true,permissions, new Session.StatusCallback() {
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
hey i got problem with facebook login dialog and permission request dialog, overlay of both show with no alpha channel and covers activity
its looks like this under white space around dialog should be visible activity content
i dont know how to delete that overlay and/or change it to overlay with alpha channel
this is there i login to FB
public void facebookLogin() {
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// If the session is open, make an API call to get user data
// and define a new callback to handle the response
//final GraphUser graphUser;
Utils.PrintDebug("session is Opened");
final String accessToken = session.getAccessToken();
Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (user != null) {
Utils.PrintDebug("logged as "+user.getName());
FacebookConnectSpiceRequest request = new FacebookConnectSpiceRequest(accessToken, user.getId(), (int) DataManager.CurrentData.UserID);
getSpiceManager().execute(request, new FacebookConnectListener());
}
}
});
Request.executeBatchAsync(request);
}
};
});
}
How do you have the LoginActivity declared in your manifest? Try something like:
<activity android:name="com.facebook.LoginActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
I had the same issue...
in com.facebook.widgets.WebDialog class, there is a variable:
BACKGROUND_GRAY = 0xCC000000;
just edit to:
BACKGROUND_GRAY = 0x00000000;
and it's fixed.
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