Note that there are some explanatory texts on larger screens.

plurals
  1. POPost to wall Facebook API
    primarykey
    data
    text
    <p>I have coded an App were I need Facebook sharing (post to wall).</p> <p>The app seams to work: I get a dialog were I am asked if I want to share on Facebook. After it is supposed to authenticate, and if not, it should give me an error message, which it doesn't.<br> It just gives me the same dialog. </p> <p>Can somebody have a look at my code please?</p> <pre><code> public class ShareOnFacebook extends Activity{ private static final String APP_ID = "138652356232418"; private static final String[] PERMISSIONS = new String[] {"publish_stream"}; private static final String TOKEN = "access_token"; private static final String EXPIRES = "expires_in"; private static final String KEY = "facebook-credentials"; private Facebook facebook; private String messageToPost; public boolean saveCredentials(Facebook facebook) { Editor editor = getApplicationContext().getSharedPreferences(KEY,Context.MODE_PRIVATE).edit(); editor.putString(TOKEN, facebook.getAccessToken()); editor.putLong(EXPIRES, facebook.getAccessExpires()); return editor.commit(); } public boolean restoreCredentials(Facebook facebook) { SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE); facebook.setAccessToken(sharedPreferences.getString(TOKEN, null)); facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0)); return facebook.isSessionValid(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); facebook = new Facebook(APP_ID); restoreCredentials(facebook); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.facebook_dialog); String facebookMessage = getIntent().getStringExtra("facebookMessage"); if (facebookMessage == null){ facebookMessage = "Wir sind auf dem Hamburger Weinachtsmarkt, kommt doch einen Glühwein mit uns trinken!"; } messageToPost = facebookMessage; } public void doNotShare(View button){ finish(); } public void share(View button){ if (! facebook.isSessionValid()) { loginAndPostToWall(); } else { postToWall(messageToPost); } } public void loginAndPostToWall(){ facebook.authorize(this, PERMISSIONS, new LoginDialogListener()); } public void postToWall(String message){ Bundle parameters = new Bundle(); parameters.putString("message", message); facebook.dialog(this, "stream.publish", parameters, new WallPostDialogListener()); } class LoginDialogListener implements DialogListener { public void onComplete(Bundle values) { saveCredentials(facebook); if (messageToPost != null){ postToWall(messageToPost); } } public void onFacebookError(FacebookError error) { showToast("Authentication with Facebook failed!"); finish(); } public void onError(DialogError error) { showToast("Authentication with Facebook failed!"); finish(); } public void onCancel() { showToast("Authentication with Facebook cancelled!"); finish(); } } class WallPostDialogListener implements DialogListener { public void onComplete(Bundle values) { final String postId = values.getString("post_id"); if (postId != null) { showToast("Message posted to your facebook wall!"); } else { showToast("Wall post cancelled!"); } finish(); } public void onFacebookError(FacebookError e) { showToast("Failed to post to wall!"); e.printStackTrace(); finish(); } public void onError(DialogError e) { showToast("Failed to post to wall!"); e.printStackTrace(); finish(); } public void onCancel() { showToast("Wall post cancelled!"); finish(); } } private void showToast(String message){ Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } } </code></pre> <p>There is a button on my main menu which has an intent to the class I've pasted. On my Facebook share button I have an intent:</p> <pre><code>Intent postOnFacebookWallIntent = new Intent(this, ShareOnFacebook.class); postOnFacebookWallIntent.putExtra("facebookMessage", "is integrating stuff again."); startActivity(postOnFacebookWallIntent); </code></pre> <p>OK, I'll make it more clear.</p> <p>This class is the class which takes care of all my Facebook API stuff. When I integrate it with my app or any app, it gives the user a dialog asking if you want to share with Facebook. If you press yes, it should authenticate with Facebook and post the message or if it fails, it should give me an error message, which it doesn't. It try to authenticate and then gives me the same dialog, which it shouldn't. </p> <p>Now my question is what am I doing wrong, why is it not doing what it should. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload