Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post on Facebook Fan page through app

I've managed to create a class which posts on my facebook wall. But how do I change the code to share on my facebook fanpage instead? I can't find anything on google or stack overflow...

here is the class which shares on facebook:

package com.celticwolf.blahblah;  <--- changed

import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class ShareOnFacebook extends Activity{

    private static final String APP_ID = "35255389027859";  <--- changed
    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 = "Test wall post";
        }
        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, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
    }

    public void postToWall(String message){ 
        new MyAsyncTask().execute(message);
    } 




class MyAsyncTask extends AsyncTask<String,Void,Boolean>
{
public Boolean doInBackground(String ...message){

    Bundle parameters = new Bundle();
            parameters.putString("message", message[0]);
            parameters.putString("description", "topic share");
            try {
                facebook.request("me");   
        String response = facebook.request("me/feed", parameters, "POST");  <---  I think here is the crucial part
        Log.d("Tests", "got response: " + response);
        if (response == null || response.equals("") ||
                response.equals("false")) {
            return Boolean.FALSE;
        }
        else {
             return Boolean.TRUE;
        }
    } catch (Exception e) {

        e.printStackTrace();
        return Boolean.FALSE;
    }
} 

public void onPostExecute(Boolean result){
        if(result == Boolean.TRUE){
 showToast("posted successfully");
}else{
 showToast("couldn't post to FB.");
}
        finish();
}
}






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

    private void showToast(String message){
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }
}

thank you!

like image 436
LxSwiss Avatar asked Jan 06 '13 20:01

LxSwiss


People also ask

How do I post on Facebook fan page?

Posting on a fan page works the same way as posting on a personal profile: You click in the text boxes available, type what you want or paste a link, then click "Share."

How do I post on Facebook Page app?

Tap. in the top right of Facebook. Search for the Page you'd like to post on, then select it from the dropdown menu. Tap Posts, then tap Write something on the Page.

How do you switch between your personal and admin page on mobile?

If you are on your mobile device visiting your page and would like to switch to your personal account, simply click the bubble icon in the bottom right-hand side of the screen to switch who you are posting as. On your desktop, the same button can be found at the top of your page on the right-hand side.

How do I comment as myself on Facebook mobile App 2022?

Click on the three lines in the right upper corner and go to the page you manage. Find a post you would like to comment on. In the lower right corner below the post or photo is a circle with an arrow. Click on the arrow and select your profile in the “Liking and commenting as” section.


1 Answers

String response = facebook.request("me/feed", parameters, "POST");

me/feed becomes PAGE_ID/feed:

String response = facebook.request("PAGE_ID/feed", parameters, "POST");

Learn how to use the Graph API here: https://developers.facebook.com/docs/reference/api/

like image 142
Stéphane Bruckert Avatar answered Sep 23 '22 05:09

Stéphane Bruckert