Search code examples
androidfacebookfacebook-stream-story

Android: Facebook SDK - Share Post with description, thumbnail and title


I'm having trouble setting up a simple facebook wall post to the user's wall. I want a facebook dialog box to pop up on clicking a button with a thumbnail, description and title. I have tried the following code but no dialog box pops up:

shareOnFacebookBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                /*
                 * Get existing access_token if any
                 */
                mPrefs ShopDetailActivity.this.getActivity().getPreferences(Context.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);
                }
                if(expires != 0) {
                    facebook.setAccessExpires(expires);
                }

                /*
                 * Only call authorize if the access_token has expired.
                 */
                if(!facebook.isSessionValid()) {    

                facebook.authorize(ShopDetailActivity.this.getActivity(),new String[] { "publish_stream" }, new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {

                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();
                        //facebook.dialog(ShopDetailActivity.this.getActivity(), "feed", new SampleDialogListener());
                        Bundle parameters = new Bundle();
                        parameters.putString("message", "message");// the message to post to the wall
                        facebook.dialog(context, "feed", parameters, this);

                    }

                    @Override
                    public void onFacebookError(FacebookError error) {}

                    @Override
                    public void onError(DialogError e) {}

                    @Override
                    public void onCancel() {}
                });


              } 
            }

        });

The authorize window opens and after clicking allow I'd expect the dialog box to pop up but it just returns to the app. What am I doing wrong?


Solution

  • facebook = new Facebook("your facebook id");
    
                        mAsyncRunner = new AsyncFacebookRunner(facebook);
                        facebook.authorize(this, new String[]
                        { "publish_stream", "offline_access" }, -1,
    
                        new DialogListener()
                        {
                            public void onComplete(Bundle values)
                            {
                                Log.e("tag", "Values returned by Bundle ====> " + values.toString());
                                fbImageSubmit();
                            }
    
                            public void onFacebookError(FacebookError error)
                            {
    
                            }
    
                            public void onError(DialogError e)
                            {
    
                            }
    
                            public void onCancel()
                            {
    
                            }
                        });
    
    //add method into your class
    
        private void fbImageSubmit()
            {
                if (fb != null)
                {
                    if (fb.isSessionValid())
                    {
                        Bundle b = new Bundle();
                      b.putString("picture", your image url);
                        b.putString("caption", title);
                        b
                                .putString(
                                        "description",
                                        "test");
                        b.putString("name", "Hi Friends, I am using the your app name app for Android!");
                        b.putString("link", "https://market.android.com/details?id="+this.getApplication().getPackageName().toString());
                        try
                        {
                            String strRet = "";
                            strRet = fb.request("/me/feed", b, "POST");
                            JSONObject json;
                            try
                            {
                                json = Util.parseJson(strRet);
                                if (!json.isNull("id"))
                                {
                                    Log.i("Facebook", "Image link submitted.");
                                }
                                else
                                {
                                    Log.e("Facebook", "Error: " + strRet);
                                }
                            } catch (FacebookError e)
                            {
                                Log.e("Facebook", "Error: " + e.getMessage());
                            }
                        } catch (Exception e)
                        {
                            Log.e("Facebook", "Error: " + e.getMessage());
                        }
                    }
                }
            }