set user (access token) within FB.init

此生再无相见时 提交于 2019-12-12 09:38:30

问题


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?


回答1:


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



回答2:


I have more knowledge reagrding the PHP SDK, but in PHP you could just replace the "me" with the user id and I suppose it will work for you as well:

FB.api('/'+fbuserid+'/...');

But you should be aware that the access token of the users change, after changing passwords and logging in and out from Facebook.



来源:https://stackoverflow.com/questions/11560306/set-user-access-token-within-fb-init

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!