需要帮助发布到Facebook用户墙

时间:2012-03-02 09:31:35

标签: android facebook post

  

可能重复:
  Android/Java — Post simple text to Facebook wall?

我使用以下代码发布到Facebook用户墙。我在HomeActivity中验证了用户身份。存储SharedPreferences中的accesstoken。

     if (HomeActivity.authenticatedFacebook.isSessionValid()) {
                            Log.v("****session valid **** ", "valid");
                            fbuserpref = getSharedPreferences(FBUSER_PREF, 0);
                            acc_tock = fbuserpref.getString("access_token", " ");
                            Log.v("FB accesstoken  >> In FBShare", acc_tock);


                            if (acc_tock == null && acc_tock == " ") {
                                Log.v("Access token is null in FBShare", "null");
                            } else {
                                AsyncFacebookRunner asyn = new AsyncFacebookRunner(
                                        authenticatedFacebook);                 
                                Bundle parameters = new Bundle();
                                parameters.putString("message", msg.getText()
                                        .toString());
                                        Log.v("target id", HomeActivity.id_json);

                                parameters.putString("target_id", HomeActivity.id_json);

                                parameters.putString("method", "stream.publish");
                                asyn.request("me/feed", parameters, "POST",
                                        new TestRequestListener(), null);


                            }

                        } else {
                            Log.v("session Invalid", "Invalid");
                            Toast.makeText(getApplicationContext(),
                                    "Please Login to Facebook.", Toast.LENGTH_LONG)
                                    .show();
                        }



                            public class TestRequestListener implements RequestListener {

                @Override
                public void onComplete(String response, Object state) {
                    // TODO Auto-generated method stub

                    Log.v("posted successfully", response);
                    finish();
                }

                @Override
                public void onIOException(IOException e, Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e,
                        Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onMalformedURLException(MalformedURLException e,
                        Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onFacebookError(FacebookError e, Object state) {
                    // TODO Auto-generated method stub

                }       


}

我收到了以下回复。

03-03 02:21:05.650: V/posted successfully(26847): {"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

1 个答案:

答案 0 :(得分:0)

试试这个 -

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;

public class FacebookActivity extends Activity implements DialogListener,
    OnClickListener
{

private Facebook facebookClient;
private LinearLayout facebookButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.test);//my layout xml

    facebookButton = (LinearLayout)this.findViewById(R.id.Test_Facebook_Layout);

}

@Override
public void onComplete(Bundle values)
{

    if (values.isEmpty())
    {
        //"skip" clicked ?
        return;
    }

    // if facebookClient.authorize(...) was successful, this runs
    // this also runs after successful post
    // after posting, "post_id" is added to the values bundle
    // I use that to differentiate between a call from
    // faceBook.authorize(...) and a call from a successful post
    // is there a better way of doing this?
    if (!values.containsKey("post_id"))
    {
        try
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", "this is a test");// the message to post to the wall
            facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }
    }
}

@Override
public void onError(DialogError e)
{
    System.out.println("Error: " + e.getMessage());
}

@Override
public void onFacebookError(FacebookError e)
{
    System.out.println("Error: " + e.getMessage());
}

@Override
public void onCancel()
{
}

@Override
public void onClick(View v)
{
    if (v == facebookButton)
    {
        facebookClient = new Facebook();
        // replace APP_API_ID with your own
        facebookClient.authorize(this, APP_API_ID,
            new String[] {"publish_stream", "read_stream", "offline_access"}, this);
    }
}
}

并且,也请查看GitHub示例。