The message for the facebook wall user does not work when the Facebook application is installed on the device / emulator

I created an operation that uses this implementation (see accepted answer) to post a state update on the user's facebook wall.

It works without problems if the emulator / phone does not have the facebook application installed.

If the facebook application is installed on the emulator / phone, the facebook application loads the login screen, but after trying to login, the facebook application just disappears, returning me back to my application.

Has anyone had this experience when the facebook app is installed?

My code is:

public class AchievementActivity extends Activity implements DialogListener, OnClickListener{ private Facebook facebook; Button facebookPostButton; String defaultFacebookPost; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.achievements); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_layout); View achievementDivider = (View)findViewById(R.id.achievementDivider); int[] colors = {0, 0xff00ffff, 0}; achievementDivider.setBackgroundDrawable(new GradientDrawable(Orientation.RIGHT_LEFT, colors)); //get the title of the achievement from the intent that started this activity from the activity StatisticsActivity String achievementTitleString = getIntent().getStringExtra("title"); String achievementTextToDisplay = getAchievementTextToDisplay(achievementTitleString); defaultFacebookPost = getDefaultPost(achievementTitleString); //ImageView achievementActivityAchievementBadgeImageView = (ImageView)findViewById(R.id.achievementActivityAchievementBadgeImageView); TextView achievementActivityBadgeTitleTextView = (TextView)findViewById(R.id.achievementActivityBadgeTitleTextView); achievementActivityBadgeTitleTextView.setText(achievementTitleString); TextView achievementActivityAchievementText = (TextView)findViewById(R.id.achievementActivityAchievementText); achievementActivityAchievementText.setText(achievementTextToDisplay); facebookPostButton = (Button)findViewById(R.id.facebookPostButton); facebookPostButton.setOnClickListener(this); } @Override public void onComplete(Bundle values) { if (values.isEmpty()) { Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT); return; } if (!values.containsKey("post_id")) { try { Bundle parameters = new Bundle(); parameters.putString("message", defaultFacebookPost);// the message to post to the wall facebook.dialog(AchievementActivity.this, "stream.publish", parameters, this);// "stream.publish" is an API call } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } } try { facebook.logout(getApplicationContext()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onFacebookError(FacebookError error) { Toast.makeText(AchievementActivity.this, "onFacebookError", Toast.LENGTH_LONG); } @Override public void onError(DialogError e) { Toast.makeText(AchievementActivity.this, "onError", Toast.LENGTH_LONG); } @Override public void onCancel() { Toast.makeText(AchievementActivity.this, "onCancel", Toast.LENGTH_LONG); } @Override public void onClick(View v) { if (v == facebookPostButton) { facebook = new Facebook("my_facebook_api"); // replace APP_API_ID with your own facebook.authorize(this, new String[] {"publish_stream", "read_stream", "offline_access"}, this); } } private String getDefaultPost(String defaultTitleString) { //do some stuff here to get a string to post to wall return defaultPost; } private String getAchievementTextToDisplay(String achievementTitleString) { String achievementTextToDisplay = "DEFAULT"; //do some stuff here to get text to display in the activity //this has nothing to do with the facebook post... return achievementTextToDisplay; } } 

Logcat tells me the following:

 05-11 13:03:34.076: INFO/ActivityManager(98): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) } 05-11 13:03:34.246: INFO/ActivityManager(98): Displayed activity com.facebook.katana/.ProxyAuth: 158 ms (total 158 ms) 05-11 13:03:35.166: DEBUG/dalvikvm(12390): GC_FOR_MALLOC freed 6729 objects / 418424 bytes in 44ms 05-11 13:03:35.166: DEBUG/webviewglue(12390): nativeDestroy view: 0x527e20 05-11 13:03:35.166: DEBUG/NativeCrypto(12390): Freeing OpenSSL session 

EDIT : After the Android development platform was installed on several computers over the past year, and ALWAYS had a problem with Facebook after creating new development environments, I found that this simple answer could cause Facebook to be implemented on one device and not another ....

The Facebook API secret key (the one you list on developer.facebook.com) will be different for each version of your application that you pack with a different certificate. For example, let's say you have two development machines. Since both of these machines will create your applications using different certificates, you must make sure that you create a Facebook API secret key for each machine and list them on developer.facebook.com.

+4
source share
3 answers

This is because when you are logged in to your facebook account, your login session is created on the device. After completing your task, you should exit Facebook.

+1
source

I found a workaround, but so far this is not the best.

 facebook.authorize(activity, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener()); 

This will force your application not to use SSO if the official facebook application is installed on the device. But there has to be a better solution, because there are applications that use sso with the official facebook application.

+3
source
 private static final String[] PERMISSIONS = new String[] {"publish_stream", "read_stream", "offline_access"}; @Override public void onClick(View v) { if (v == facebookPostButton) { mAsyncRunner = new AsyncFacebookRunner(mFacebook); mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener()); mFacebook.authorize(FacebookImplement.this, PERMISSIONS, new AuthorizeListener()); } } public class AuthorizeListener extends BaseDialogListener { public void onComplete(Bundle values) { Bundle params = new Bundle(); params.putString("message"," Message"); params.putString("description", "Wall Posting Description"); mAsyncRunner.request("me/feed", params, "POST", new UploadListener()); } 

}

 public class UploadListener extends BaseRequestListener { public void onComplete(final String response) { mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener()); } 

}

  public class LogoutListener extends BaseRequestListener { public void onComplete(final String response) { } } 

Maybe this code will help you. If you have any problems, ask the question without any problems.

0
source

Source: https://habr.com/ru/post/904741/


All Articles