I want to retrieve logged in user email. This is what I tied, but not retrieving the email. I could retrieve only id, first name, last name but not the email
LoginManager.getInstance().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) {
Log.v("LoginActivity", response.toString());
// Application code
try {
String userEmail = object.getString("email");
String userId = object.getString("id");
emailEditText.setText(userEmail);
new SignupTask(SignupActivity.this, userId, userEmail).execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email"); // ParĂ¡metros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email", "user_friends"));
What do I have missed....
ohk..try this code its working for me...
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setBackgroundResource(R.drawable.fb);
loginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
loginButton.setReadPermissions(Collections.singletonList("public_profile, email, user_birthday, user_friends"));
callbackManager = CallbackManager.Factory.create();
if (netWorkStatus) {
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
//Store Facebook data to webservice .
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
try {
// String picture = object.getString("picture");
//JSONObject jobj = new JSONObject(picture);
//JSONObject dataObj = jobj.getJSONObject("data");
name = object.getString("name");
userEmail = object.getString("email");
//String url = dataObj.getString("url").replace("\\", "");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,picture");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
CallbackManager callbackManager ;
//in OnCreate initialize it
callbackManager = CallbackManager.Factory.create();
//after that overide onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Related
I'm using the following code for fetching Facebook user details through my Android application.
callbackManager = CallbackManager.Factory.create();
fb = (Button) findViewById(R.id.fb);
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity",
response.toString());
try {
id = object.getString("id");
try {
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
#Override
protected void onActivityResult
(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
Log.i("onActivityResult", "onActivityResult");
}
Here, the log inside the onActivityResult is printed, while the loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() is not at all initiated. None of the logs inside this method is printed.
What is the problem?
I solved the problem by placing
callbackManager = CallbackManager.Factory.create();
above the setContentView();
check if You have initialize Fb SDK, if not use this.
Create a class that extends from Application, then you have to register in Android Manifiest as Name.
public class App extends Application {
public static Context context;
#Override
public void onCreate() {
super.onCreate();
context = this;
FacebookSdk.sdkInitialize(getApplicationContext());
}
public static Context getContext() {
return context;
}
}
Manifiest.
<application
android:name=".App"
.....
.....
.....
Rather than making a callback on the button you need to make a callback with the LoginManager.getInstance() will make a callback to the page where you want to make callback. As LoginManager.getInstance() is a getter for the Login Manager and returns the Login Manager.
In short, Just apply the code:
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>(){
Rather than
loginButton.registerCallback(callbackManager,new FacebookCallback<LoginResult>(){
I want to fetch data of user like name, email and other details. I am able to login using facbook API. But I foound out the onSuccess() method is not executing. however it shows me log in.
public class SignInUpActivity extends AppCompatActivity {
private CallbackManager callbackManager;
LoginButton fbLoginBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//intialize fb sdk
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_sign_in_up);
fbLoginBtn = (LoginButton) findViewById(R.id.ObiNoID_SignIn_SignUpActivity_btn_fb_login);
// setting permission for fb accounts
fbLoginBtn.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
callbackManager = CallbackManager.Factory.create();
fbLoginBtn.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) {
try {
String email = object.getString("email");
String birthday = object.getString("birthday");
Toast.makeText(SignInUpActivity.this,email+birthday,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onCancel() {
Toast.makeText(SignInUpActivity.this, "cancelled", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(SignInUpActivity.this, "error", Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);// will call the call
}
}
I am a begginer please help me solve the problem.
try this,
private void connectToFacebook() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
// Application code
if (response.getError() != null) {
System.out.println("ERROR");
} else {
System.out.println("Success");
String jsonresult = String.valueOf(json);
System.out.println("JSON Result" + jsonresult);
String fbUserId = json.optString("id");
String fbUserFirstName = json.optString("name");
String fbUserEmail = json.optString("email");
String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large";
}
Log.v("LoginActivity", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Log.v("LoginActivity", "cancel");
}
#Override
public void onError(FacebookException exception) {
// App code
// Log.v("LoginActivity", "" + exception);
Toast.makeText(LoginActivity.this, "" + exception, Toast.LENGTH_LONG).show();
}
});
}
I have tried many times with different codes but still i am not getting email from fb login. Please help me trouble shoot the isseu. also I am having 2 emails at my test fb account but still not getting any email.the code is below pasted.
public class fbpage extends Activity {
String get_id, get_name, get_gender, get_email, get_birthday,get_locale, get_location,id;
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager,mCallbackManager;
private static final String TAG = "FacebookConnect";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.fbpagelayout);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
// List<String> permissionNeeds = Arrays.asList("user_photos","email","user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity", response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String name = object.getString("name");
String email = object.getString("email");
String gender = object.getString("gender");
//String birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
}
#Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
}
You need to add code for permission which will be needed.
List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile", "user_location");
mButtonLogin.setReadPermissions(permissionNeeds);
Once you add these two lines you will get permission to access email id.
I have a problem with my app, i'm trying to implement facebook login, I read the guide on developer.facebook.com and i wrote this code
public class LoginActivity extends Activity {
private CallbackManager callbackManager;
private Boolean facebookLogin = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
String userID = loginResult.getAccessToken().getUserId();
String facebookToken = loginResult.getAccessToken().getToken();
facebookLogin = true;
sendLoginData(userID, facebookToken);
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "onCancel", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException e) {
e.printStackTrace();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(), "onActivityResult", Toast.LENGTH_SHORT).show();
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I also modify the manifest like the guide say, but, when i'm running the app, if i click on the facebook login button my app is closed without any apparent error.
I also tryed to execute debug, I placed some breakpoints inside the facebook callback and inside the onActivityResult() but they hasn't been reached.
If i add some permission in loginButton.setReadPermissions() the app open the facebook activity but, when I click on ok, is closed again.
Anyone can help me?
Try to use this code.
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity",
response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
#Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
In my Android app I use Facebook SDK 4.3. I try to login with Facebook using LoginButton. Here is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
LoginButton btnLoginFb = (LoginButton) findViewById(R.id.login_button);
btnLoginFb.setReadPermissions("email", "user_likes", "user_friends");
btnLoginFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult result) {
Log.i("test","success");
}
#Override
public void onCancel() {
Log.i("test","cancel");
}
#Override
public void onError(FacebookException error) {
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
When app runs, after click button Login, it displays Facebook Activity, which requires email and password to login. But then the Callback doesn't run into onSucess. It runs into onCancel. I don't understand what is wrong?
Just use this code with login button.
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity", response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
For more information just look at my answer here