Unable to import Session,Request,Response,GraphUser packages in android - android

I imported latest facebook sdk in my ecllipe.When i am trying to get basic information in my app,it does n't import respective packages of Request,Response,and Session.what i do?to import respective packages.
I tried below code but facing problem at importing respective packages.
LoginActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Session.getActiveSession() == null
|| Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
System.out.println("State= " + state);
if (session.isOpened()) {
System.out.println("Token=" + session.getAccessToken());
Request.executeMeRequestAsync(session,
new GraphUserCallback() {
#Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
System.out.println("User=" + user);
}
if (response != null) {
System.out.println("Response="
+ response);
Toast.makeText(MainActivity.this,
response.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}

It happens with you because session is removed from new android SDK 4. So you cant not use this code for getting user information.
Try to use this code. It's work successfully for me.
First Initialize FacebookSdk in onCreate before inflecting layout.
FacebookSdk.sdkInitialize(this.getApplicationContext());
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());
}
});
Add method.
#Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}

Related

Facebook Login Does not Trigger Callback

I am attempting to implement Facebook login into my Android app following: https://developers.facebook.com/docs/facebook-login/android
Through following their implementation, I can Facebook login steps, successfully login, but afterward, the success callback does nothing:
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.button_facebook_sign_in);
loginButton.setReadPermissions("email");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(LoginActivity.this, loginResult.getAccessToken().toString(), Toast.LENGTH_SHORT).show();
Log.v("KJA onSuccess", "Facebook Login succeeded");
}
#Override
public void onCancel() {
// App code
Log.v("KJA onCancel", "Cancel called");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.v("KJA onError", exception.toString());
}
});
How can I ensure the callback receives the notification of login success?
Before clicking Facebook, check if user is already logged in.
try {
String curr_id = Profile.getCurrentProfile().getId();
Log.w("FB_CURRENT_ID", "Current ID: " + curr_id );
} catch (Exception e) {
Log.w("FB_CURRENT_ID", "Not Logged In!");
}
Try this code for button click:
loginButton.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());
if (object.has("picture")) {
try {
String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
String nameOfUser = object.getString("name");
String id = object.getString("id");
Log.w("facebook_id", id);
Log.w("facebook_name", nameOfUser);
Log.w("facebook_pic", profilePicUrl);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture.type(large)");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
}
);
Add onActivityResult in your class. Here's the code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

I want to fetch data from facebook's profile. I am able to login but its not calling the onSuccess(). in Android studio

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();
}
});
}

Android - login with facebook SDK 4 dont retrieve email

I am working in project that login with facebook , I need to retrieve email address as part of registration
returned json
{Response: responseCode: 200, graphObject: {"name":"Hany Bahaa","id":"218274898510181"}, error: null}
my java code
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.signin);
ButterKnife.bind(this);
LoginButton _loginfb = (LoginButton) findViewById(R.id.fb_login);
_loginfb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fblogin();
}
});
}
private void Fblogin() {
// Set permissions
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("email", "public_profile", "user_friends"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("Success");
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
Log.i(TAG, "ERROR");
} else {
Log.i(TAG, "Success");
Profile _profile = Profile.getCurrentProfile();
String jsonresult = String.valueOf(json);
System.out.println("JSON Result" + jsonresult);
String str_email = json.optString("email");
System.out.println("JSON Result email" + str_email);
String str_id = json.optString("id");
String str_name = json.optString("name");
}
}
}).executeAsync();
}
#Override
public void onCancel() {
Log.d(TAG, "On cancel");
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, error.toString());
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
You need to pass a Bundle to your GraphRequest with the parameters you want to retrieve:
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location");
GraphRequest graph = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
Log.i(TAG, "ERROR");
} else {
Log.i(TAG, "Success");
Profile _profile = Profile.getCurrentProfile();
String jsonresult = String.valueOf(json);
System.out.println("JSON Result" + jsonresult);
String str_email = json.optString("email");
System.out.println("JSON Result email" + str_email);
String str_id = json.optString("id");
String str_name = json.optString("name");
}
}
});
graph.setParameters(parameters);
graph.executeAsync();
You can also pass the Bundle as an argument of your GraphRequest constructor.
https://developers.facebook.com/docs/reference/android/current/class/GraphRequest/

Android login with facebook crash

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);
}

Cannot login Facebook with Android Facebook SDK 4.3

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

Categories

Resources