I am using this code for access user information in my app. But nothing change.. Accualy when i debug the code, login.registerCallback and below never called. No error and nothing change.
Where is my fault ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
String userID = loginResult.getAccessToken().getUserId();
String profileImgUrl = "https://graph.facebook.com/" + userID + "/picture?type=large";
tv = (TextView) findViewById(R.id.textView);
tv.setText(profile.getName());
try {
bitmap = getBitmapFromUrl(profileImgUrl);
iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
it's because you forgot to add the following block in your activity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Related
I am developing an app that can share an image to Facebook.
Only I can share image, other people cannot share. I have a FAB and calling this activity on its onClick() method. I really don't know what to do, my app is live but when I click on my app name over the shared photo I get
"Misconfigured App
Sorry, MyApp hasn't been approved for display in App Centre."
What is the reason that others cannot share images? I really need the answer. Thanks.
public class ShareActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private LoginManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
List<String> permissionNeeds = Arrays.asList("publish_actions");
//this loginManager helps you eliminate adding a LoginButton to your UI
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(this, permissionNeeds);
manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
sharePhotoToFacebook();
Toast.makeText(ShareActivity.this, "Success", Toast.LENGTH_LONG).show();
finish();
}
#Override
public void onCancel() {
Toast.makeText(ShareActivity.this, "Cancel", Toast.LENGTH_LONG).show();
finish();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(ShareActivity.this, "Error", Toast.LENGTH_LONG).show();
finish();
}
});
}
}
private void sharePhotoToFacebook(){
try {
URL url = new URL("http://burakakyalcin.site/image.png");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption("Hello")
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, null);
} catch(IOException e) {
System.out.println(e);
}
}
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent data)
{
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
}
I was reading the thread: Android Facebook 4.0 SDK How to get Email, Date of Birth and gender of User and I'm using exactly the same code to call a facebook login, but, is always firing the method onCancel, what I'm doing wrong?
public class LoginActivity extends AppCompatActivity {
CoordinatorLayout BackgroundImage;
int[] images = new int[] {R.drawable.bg01, R.drawable.bg02, R.drawable.bg03};
Button BtnIniciar;
TextView BtnRecuperar;
EditText txtCorreo;
EditText txtContrasena;
String androidId;
TextView btnRegistrar;
LoginButton loginButton;
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
BackgroundImage = (CoordinatorLayout)findViewById(R.id.background);
int imageId = (int)(Math.random() * images.length);
BackgroundImage.setBackgroundResource(images[imageId]);
BtnIniciar = (Button) findViewById(R.id.Iniciar);
BtnRecuperar = (TextView) findViewById(R.id.btnRecuperar);
txtCorreo = (EditText) findViewById(R.id.txtCorreo);
txtContrasena = (EditText) findViewById(R.id.txtContrasena);
btnRegistrar = (TextView) findViewById(R.id.btnRegistrar );
androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
Boolean isLogged = new HttpGetPost().IsLogged(androidId);
if(isLogged){
Intent intent = new Intent( LoginActivity.this, MainActivity.class);
startActivity(intent);
}
BtnIniciar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean result = new HttpGetPost().Login(txtCorreo.getText().toString(), txtContrasena.getText().toString(), 2, androidId);
if (result) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(v.getContext(), "El usuario/contraseña no es válido.", Toast.LENGTH_LONG).show();
}
}
});
BtnRecuperar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), RecuperarContrasena.class);
startActivity(intent);
}
});
btnRegistrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), RegistroActivity.class);
startActivity(intent);
}
});
//MultiDex.install(this);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends"));
callbackManager = CallbackManager.Factory.create();
// Callback registration
loginButton.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 object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
// Application code
// String email = object.getString("email");
// String birthday = object.getString("birthday"); // 01/31/1980 format
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,email");
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.getCause().toString());
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onBackPressed() {
return;
}
I found the error, the error was:
I was adding the key_hash into the fb_login_protocol_scheme and should be: fb12322222 because my APP ID is 12322222.
First time with Facebook SDK. I can't get users profile. Its always null. What's wrong?
btnFbWidget = (LoginButton) findViewById(R.id.btnFbWidget);
btnFbWidget.setReadPermissions("public_profile");
// Callback registration
btnFbWidget.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if (profile != null)
Toast.makeText(getApplicationContext(), "LogIN as " + profile.getName(), Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "nulll", Toast.LENGTH_SHORT).show();
}
/*...*/
});
use this method also,.....
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Try with this method.
Initialize facebook SDK firstly.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init facebook sdk and
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
//instantiate callbacks manager
mCallbackManager = CallbackManager.Factory.create();
btnFbWidget=(LoginButton)findViewById(R.id.login_button);
btnFbWidget.registerCallback(mCallbackManager, this);
btnFbWidget.setReadPermissions(Arrays.asList("user_status","email"));
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//manage login result
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
public void onSuccess(LoginResult loginResults) {
Toast.makeText(getApplicationContext(), "Success ",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
}
public void onError(FacebookException e) {
}
And then to get the User Profile use this method.
private static String uid,email;
public void getUserProfile() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
try {
//and fill them here like so.
uid = object.getString("id");
email = object.getString("email")
} catch (JSONException e) {
e.printStackTrace();
}
getGroups();
}
});
// If you want to pass data to other activity.
Bundle parameters = new Bundle();
parameters.putString("userdata", "email");
request.setParameters(parameters);
request.executeAsync();
}
And do call this method getUserProfile() in your on create.
EDIT 1 :
Change in read permissions.
btnFbWidget.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
And accordingly you can get further information like.
String email = object.getString("email");
String birthday = object.getString("birthday");
I have an app in which I implemented facebook login which was working last time I checked. For some reason, now, after I click the button nothing happens.
My onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity());
mCallbackManager = CallbackManager.Factory.create();
}
my loginButton
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager, mFacebookCallback);
my onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("askj","Callback called with requestCode "+requestCode+" + resultCode "+resultCode);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
my mFacebookCallback
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("askj", "onSuccess");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
Log.d("askj", "profile " + profile.toString());
if (profile != null) {
Intent upanel = new Intent(getActivity(), AfterLogin.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(upanel);
}
}
#Override
public void onCancel() {
Log.d("askj", "onCancel");
}
#Override
public void onError(FacebookException e) {
Log.d("askj", "onError " + e);
}
};
This is what is being printed in logCat once the button is clicked:
Callback called with requestCode 129742 + resultCode -1
I don't get anything from mFacebookCallback in the console.
Edit: How can I add a specific requestCode to this callback ?
Edit2: For anyone having this problem, I solved it by removing loginButton.setFragment(this);. I still have this question though How can I add a specific requestCode to this callback ?
Here,I'm getting logged into the app using facebook account using 4.1. But now I want to send message to a friend or post something to a friend's wall but I'm not getting a proper answer for doing it.
onCreate(){
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
.
.
loginButton = (LoginButton) findViewById(R.id.login);
loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends","publish_actions"));
}
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
Log.d(TAG, "Login Canceled");
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "Login Error");
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// manage login results
callbackManager.onActivityResult(requestCode, resultCode, data);
}