如何才能在Android中将文字发布到我的Facebook墙上?

时间:2014-04-12 06:52:27

标签: android facebook facebook-graph-api post facebook-android-sdk

目前,我使用以下代码:

Bundle params = new Bundle();
//params.putString("message", "I want to post this text only.");
params.putString("name", "Facebook SDK for Android");
params.putString("caption", "Build great social apps and get more installs.");
params.putString("description", "Description");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

WebDialog feedDialog = (
    new WebDialog.FeedDialogBuilder(getActivity(),
        Session.getActiveSession(),
        params))
    .setOnCompleteListener(new OnCompleteListener() {

但是,我不能仅发布消息,因为捆绑包必须包含" link"。

4 个答案:

答案 0 :(得分:1)

这是完整的解决方案。请试试这个,我正在使用此代码并且工作完全正常。 如果您没有登录(如果您是第一次使用),您将直接登录屏幕(否则)并且访问令牌存储在本地数据中。之后您可以将文本共享到Facebook墙。

if (facebook.getAccessToken() != null) {

            postOnWall("msg to share");

        } else {
            loginToFacebook();

            }

public void postOnWall(String msg) {
    Log.d("Tests", "Testing graph API wall post");
    try {
        String response = facebook.request("me");
        Bundle parameters = new Bundle();

        parameters.putString("message", msg);
        parameters.putString("description", "test test test");
        // parameters.putByteArray("message", msg);
        response = facebook.request("me/feed", parameters, "POST");
        Log.d("Tests", "got response: " + response);
        if (response == null || response.equals("")
                || response.equals("false")) {
            Log.v("Error", "Blank response");

            Toast.makeText(getApplicationContext(), "FacebookError",Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {
        e.printStackTrace();

    }
}

public void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);

        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();
                        Toast.makeText(sms_by_id.this,
                                "Successfully Login", Toast.LENGTH_LONG)
                                .show();
                        pwindo.dismiss();

                        // Making Login button invisible

                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
                        pwindo.dismiss();
                        Toast.makeText(sms_by_id.this, "Facebook Error",
                                Toast.LENGTH_LONG).show();
                    }

                });
    }
}

答案 1 :(得分:0)

  

因为捆绑包必须包含“链接”。

此声明不正确。在参数中,只需包含message属性即可!不提供链接/图片/标题/描述等,因为所有这些仅与链接相关。因此,只需使用message参数,它就会仅作为文本发布。

答案 2 :(得分:0)

试试这个

    public class ShareOnFacebook extends Activity{

    private static final String APP_ID = "123456789";
    private static final String[] PERMISSIONS = new String[] {"publish_stream"};

    private static final String TOKEN = "access_token";
        private static final String EXPIRES = "2040";
        private static final String KEY = "gfdfdfdfdfd";

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

          facebook = new Facebook(APP_ID);
          restoreCredentials(facebook);


          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);
            /*Intent backhome = new Intent(ShareOnFacebook.this,Quotes_Tab.class);
            startActivity(backhome);*/
        }
     }

       public void loginAndPostToWall(){
            facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
    }

      public void postToWall(String message){
             Bundle parameters = new Bundle();
                parameters.putString("message", message);
         //       parameters.putString("link", "http://theaterbalcony.com/tag/torpedo-3d-malayalam-cartoon/");

                try {
                    facebook.request("me");
             String response = facebook.request("me/feed", parameters, "POST");
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("") || response.equals("false"))    {
                showToast("Blank response.");
            }
             else {
                showToast("Message posted to your facebook wall!");
            }
            finish();
        } catch (Exception e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            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();
   }

   @Override
   protected void onDestroy() {
       // TODO Auto-generated method stub
       super.onDestroy();
       finish();
    }

    @Override
     public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
        // if menu button is pressed
       if (pKeyCode == KeyEvent.KEYCODE_MENU
               && pEvent.getAction() == KeyEvent.ACTION_DOWN) {

           Intent home = new Intent(ShareOnFacebook.this, Quotes_Tab.class);
           finish();
           startActivity(home);
        } else if (pKeyCode == KeyEvent.KEYCODE_BACK
            && pEvent.getAction() == KeyEvent.ACTION_DOWN) {


           Intent home = new Intent(ShareOnFacebook.this,Quotes_Tab.class);
           finish();
           startActivity(home);
           return super.onKeyDown(pKeyCode, pEvent);
         }
         return super.onKeyDown(pKeyCode, pEvent);
    }
      @Override
      protected void onStop() {
          // TODO Auto-generated method stub
           super.onStop();
        finish();
    }

  }

答案 3 :(得分:0)

您需要做的就是更改用户1632209

动作的Bundle
        Bundle parameters = new Bundle();

        params.putString("message", msg);
        params.putString("description", "test test test");