简体   繁体   中英

Error: com.facebook.FacebookException: Failed to generate preview for user

I am posting a custom story on user's wall.to do this i am creating a objects and actions in my code.i have done my code from https://developers.facebook.com/docs/android/open-graph/ link.and i am using Facebook dialog for posting the custom stories.

This code works fine when developer done with his Facebook account and post on the timeline.. when developer lo-gin with his Facebook account in this app,this app opens the Facebook dialog for posting a custom story,but another person lo-gins with his/her Facebook account in this app, the Facebook dialog appears and immediately dissapear. and showing the error Error: com.facebook.FacebookException: Failed to generate preview for user.

I want to know is there any problem in my code or any other reason.i have also registered my app on developer account.i using the following code to do this.anyone who knows or who has done this so please tell me how to do this.i will be very thankful to you.and i also want to tell that i am using Facebook sdk 3.7.

public class MainActivity extends Activity {
                    Button button1;
                    private static final String TAG = "MainActivity";
                    private UiLifecycleHelper uiHelper;
                    private static final List<String> PERMISSIONS = Arrays
                            .asList("publish_actions");
                    private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
                    private ProgressDialog progressDialog;
                    private boolean pendingPublishReauthorization = false;

                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);

                        button1 = (Button) findViewById(R.id.button1);

                        uiHelper = new UiLifecycleHelper(this, callback);
                        uiHelper.onCreate(savedInstanceState);
                        Session session = Session.getActiveSession();
                        if (!session.isOpened() && !session.isClosed()) {
                            session.openForRead(new Session.OpenRequest(this).setPermissions(
                                    Arrays.asList("basic_info", "email","user_birthday","user_location","user_hometown","user_about_me","user_relationships")).setCallback(callback));
                            session.openForPublish(new Session.OpenRequest(this).setPermissions(
                                    Arrays.asList("publish_actions")).setCallback(callback));
                        } else {
                            Session.openActiveSession(this, true, callback);
                        }

                        button1.setOnClickListener(new OnClickListener() {

                            @SuppressWarnings("deprecation")
                            @Override
                            public void onClick(View v) {

                                OpenGraphObject meal = OpenGraphObject.Factory
                                        .createForPost("music.song");
                                meal.setProperty("title", "Competent Groove");
                                meal.setProperty("image",
                                        "http://example.com/cooking-app/images/buffalo-tacos.png");
                                meal.setProperty("url",
                                        "https://example.com/cooking-app/meal/Buffalo-Tacos.html");
                                meal.setProperty("description",
                                        "requested a hasher song at pizzahutt via Blureffect");

                                OpenGraphAction action = GraphObject.Factory
                                        .create(OpenGraphAction.class);
                                action.setProperty("song", meal);

                                FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(
                                        MainActivity.this, action, "blureffect_unique:request",
                                        "song").build();

                                uiHelper.trackPendingDialogCall(shareDialog.present());
                            }
                        });

                    }

                    @Override
                    public boolean onCreateOptionsMenu(Menu menu) {
                        // Inflate the menu; this adds items to the action bar if it is present.
                        getMenuInflater().inflate(R.menu.main, menu);
                        return true;
                    }

                    private Session.StatusCallback callback = new Session.StatusCallback() {
                        @Override
                        public void call(Session session, SessionState state,
                                Exception exception) {
                            onSessionStateChange(session, state, exception);

                        }
                    };

                    @Override
                    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                        super.onActivityResult(requestCode, resultCode, data);

                        uiHelper.onActivityResult(requestCode, resultCode, data,
                                new FacebookDialog.Callback() {
                                    @Override
                                    public void onError(FacebookDialog.PendingCall pendingCall,
                                            Exception error, Bundle data) {
                                        Log.e("Activity",
                                                String.format("Error: %s", error.toString()));
                                    }

                                    @Override
                                    public void onComplete(
                                            FacebookDialog.PendingCall pendingCall, Bundle data) {
                                        Log.i("Activity", "Success!");
                                        button1.setEnabled(true);

                                    }
                                });
                    }

                    @SuppressWarnings("deprecation")
                    private void onSessionStateChange(Session session, SessionState state,
                            Exception exception) {
                        if (session != null && session.isOpened()) {
                            Log.d("DEBUG", "facebook session is open ");
                            // make request to the /me API
                            Request.executeMeRequestAsync(session,
                                    new Request.GraphUserCallback() {
                                        // callback after Graph API response with user object

                                        @Override
                                        public void onCompleted(GraphUser user,
                                                Response response) {
                                            if (user != null) {

                                                  button1.setEnabled(true);

                                                  Log.i("Birthday", ""+user.getBirthday());
                                                  Log.i("LastName", ""+user.getLastName());
                                                  Log.i("FirstName", ""+user.getFirstName());
                                                  Log.i("getId", ""+user.getId());
                                                  Log.i("email", ""+user.asMap().get("email"));
                                                  Log.i("gender", ""+user.asMap().get("gender"));
                                                  Log.i("Birthday", ""+user.getBirthday());
                                                  Log.i("city", ""+user.getLocation().getProperty("name").toString());

                                            }

                                        }
                                    });
                        }
                    }

                    @Override
                    protected void onResume() {
                        super.onResume();
                        uiHelper.onResume();
                    }

                    @Override
                    protected void onSaveInstanceState(Bundle outState) {
                        super.onSaveInstanceState(outState);
                        uiHelper.onSaveInstanceState(outState);
                    }

                    @Override
                    public void onPause() {
                        super.onPause();
                        uiHelper.onPause();
                    }

                    @Override
                    public void onDestroy() {
                        super.onDestroy();
                        uiHelper.onDestroy();
                    }

                    /*
                     * Helper method to check a collection for a string.
                     */
                    @SuppressWarnings("unused")
                    private boolean isSubsetOf(Collection<String> subset,
                            Collection<String> superset) {
                        for (String string : subset) {
                            if (!superset.contains(string)) {
                                return false;
                            }
                        }
                        return true;
                    }

                    /*
                     * Helper method to dismiss the progress dialog.
                     */
                    @SuppressWarnings("unused")
                    private void dismissProgressDialog() {
                        // Dismiss the progress dialog
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                    }
                }

在此处输入图片说明 for generate hash key

private PackageInfo INFO = null; //global declaration
    public void generateHashKeyForFacebook(Context context) throws Exception {
            try {
                INFO = context.getPackageManager().getPackageInfo("com.bito1.Shoplu", PackageManager.GET_SIGNATURES);
                if (INFO == null) {
                    Toast.makeText(context.getApplicationContext(), "Invalid Package Name / Package not found", Toast.LENGTH_LONG).show();
                    return;
                }
                for (Signature signature : INFO.signatures) {
                    MessageDigest _md = MessageDigest.getInstance("SHA");
                    _md.update(signature.toByteArray());
                    Log.d("KeyHash: =>", Base64.encodeToString(_md.digest(), Base64.DEFAULT));
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }

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