简体   繁体   中英

Android Facebook SDK doesn't post messages to my wall even permissions are already set

I'm having a trouble on this facebook post for my app. It goes the way I expected but doesn't post to my wall. Well here's my code for post:

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.BaseRequestListener;
import com.facebook.android.Facebook;
import com.facebook.android.SessionStore;

/**
 * Created by pc on 11/19/13.
 */
public class PostToFacebook {

    Facebook mFacebook;
    private static final String APP_ID = "MY_APP_ID";
    private ProgressDialog mProgress;
    private Handler mRunOnUi = new Handler();
    Context context;

    public PostToFacebook(Context baseContext){
        context = baseContext;
    }

    public void postToFacebook() {
        mProgress = new ProgressDialog(context);
        mFacebook = new Facebook(APP_ID);

        try{
            SessionStore.restore(mFacebook, context);
        }catch (Exception e){
        }

        mProgress.setMessage("Posting ...");
        mProgress.show();

        try{
            AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);
            Bundle params = new Bundle();
            params.putString("message", "MESSAGE");
            params.putString("name", "MESSAGE");
            params.putString("caption", "MESSAGE");
            params.putString("link", "URL_HERE");
            params.putString("description", "MESSAGE");
            params.putString("picture", "URL_HERE");

            mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
            Log.v("Been Here", "Done that");
        }catch (Exception ex){
            ex.printStackTrace();
            Log.v("Been Here", "Problem");
        }



    }

    private final class WallPostListener extends BaseRequestListener {
        public void onComplete(final String response) {
            mRunOnUi.post(new Runnable() {
                public void run() {
                    mProgress.cancel();
                    Toast.makeText(context, "Posted to Facebook", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

I'm totally lost on this. Well for the post of the facebookAsync it's this one:

public void request(final String graphPath,
                        final Bundle parameters,
                        final String httpMethod,
                        final RequestListener listener) {
        new Thread() {
            @Override public void run() {
                try {
                    String resp = fb.request(graphPath, parameters, httpMethod);
                    listener.onComplete(resp);
                } catch (FileNotFoundException e) {
                    listener.onFileNotFoundException(e);
                } catch (MalformedURLException e) {
                    listener.onMalformedURLException(e);
                } catch (IOException e) {
                    listener.onIOException(e);
                }
            }
        }.start();
    }

The method for request:

public String request(String graphPath, Bundle params, String httpMethod)
            throws FileNotFoundException, MalformedURLException, IOException {
        params.putString("format", "json");
        if (isSessionValid()) {
            params.putString(TOKEN, getAccessToken());
        }
        String url = (graphPath != null) ? GRAPH_BASE_URL + graphPath
                                         : RESTSERVER_URL;
        return Util.openUrl(url, httpMethod, params);
    }

And the openURL method:

public static String openUrl(String url, String method, Bundle params)
          throws MalformedURLException, IOException {
        // random string as boundary for multi-part http post
        String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
        String endLine = "\r\n";

        OutputStream os;

        if (method.equals("GET")) {
            url = url + "?" + encodeUrl(params);
        }
        Log.d("Facebook-Util", method + " URL: " + url);
        HttpURLConnection conn =
            (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("User-Agent", System.getProperties().
                getProperty("http.agent") + " FacebookAndroidSDK");
        if (!method.equals("GET")) {
            Bundle dataparams = new Bundle();
            for (String key : params.keySet()) {
                if (params.get(key) instanceof byte[]) {
                        dataparams.putByteArray(key, params.getByteArray(key));
                }
            }

            // use method override
            if (!params.containsKey("method")) {
                params.putString("method", method);
            }

            if (params.containsKey("access_token")) {
                String decoded_token =
                    URLDecoder.decode(params.getString("access_token"));
                params.putString("access_token", decoded_token);
            }

            conn.setRequestMethod("POST");
            conn.setRequestProperty(
                    "Content-Type",
                    "multipart/form-data;boundary="+strBoundary);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.connect();
            os = new BufferedOutputStream(conn.getOutputStream());

            os.write(("--" + strBoundary +endLine).getBytes());
            os.write((encodePostBody(params, strBoundary)).getBytes());
            os.write((endLine + "--" + strBoundary + endLine).getBytes());

            if (!dataparams.isEmpty()) {

                for (String key: dataparams.keySet()){
                    os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
                    os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
                    os.write(dataparams.getByteArray(key));
                    os.write((endLine + "--" + strBoundary + endLine).getBytes());

                }
            }
            os.flush();
        }

        String response = "";
        try {
            response = read(conn.getInputStream());
        } catch (FileNotFoundException e) {
            // Error Stream contains JSON that we can parse to a FB error
            response = read(conn.getErrorStream());
        }
        return response;
    }

To add I already added the extended permissions of publish_stream and publish_actions for the app settings.

Already solved. There's just a delay in facebook permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM