set user (access token) within FB.init

Mario picture Mario · Jul 19, 2012 · Viewed 9.8k times · Source

i want to publish story to user timeline even when they were not login with facebook. Actually I stored user access token in the database, if the user login in with email, i'll retrieve the access token for her. It works well in php (e.g get friends list after setAccessToken(token_from_database), but when i post the story, it always post to current fb login account's timeline.

I think the problem is caused by fb.init:

window.fbAsyncInit = function() {
        FB.init({
            appId      : 'XXX', // App ID
            status     : true, // check login status
            cookie     : true // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
        });
    };

and later:

FB.api('/me/...

take login account as "me", not the one I set in php through setAcccessToken. How could i post to that account (specify with token_from_database) 's timeline? any idea?

Answer

CBroe picture CBroe · Aug 30, 2012

Try passing the access token you want to use as part of the params object you give to FB.api – it should work the same way as it does when accessing the API server-side.

FB.api(
  '/me/feed',
  'post',
  {
    access_token : 'access_token_for_some_user_fetched_from_your_database',
    additional_parameter_foo : 'bar'
  }
);