How to login user from inside Facebook Page Tab and get access_token on server side to post a carousel to the feed? -


i have page tab facebook app. want post users timeline it.

after login on client side javascript sdk (i use angularjs module ciul/angular-facebook (sorry, cannot post github link here)):

https://gist.github.com/sevavietl/7e363fdfd0e714a12a43

i retrieve access_token on server side , trying post carousel users feed:

https://gist.github.com/sevavietl/cec5fa434837312adfd3

i have 2 problems:

  1. while first call graph returned error: (#210) param id must page.

after browsing, found can caused wrong token usage. not user_access_token. login user on client side.

  1. and next calls graph returned error: authorization code has been used.

again, after browsing, found token can used once in order oauth compliant.

please, advise me how right?

thank in advance.

best regards, vsevolod

after spending time on this, writing solution, not obvious, @ least me. but, actually, all info present in documentation.

starting problem number 2: and next calls graph returned error: authorization code has been used.

as mentioned in comments, indeed confusing access_token authorization code. understand in simplified form, mechanism is:

  1. when login facebook-javascript-sdk writes authorization code cookies.
  2. then on server side can retrieve access_token javascripthelper available in facebook-php-sdk. helper has getaccesstoken() method, retrieves access_token using authorization code cookies. place confused. trying use getaccesstoken() on every request. requests made ajax authorization code not changed, , can use once getting error. the solution pointed in many places on internet, somehow able misunderstand this. here code excerpt uses sessions store access_token, , uses getaccesstoken() method if access_token not set in session.

        $fb = new facebook([         'app_id' => $widget->app_id,         'app_secret' => $widget->app_secret,         'default_graph_version' => 'v2.5',     ]);      $helper = $fb->getjavascripthelper();      if ($request->session()->has('facebook_access_token')) {         $accesstoken = $request->session()->get('facebook_access_token');     } else {         $accesstoken = $helper->getaccesstoken();          // oauth 2.0 client handler         $oauth2client = $fb->getoauth2client();          // exchanges short-lived access token long-lived 1         $longlivedaccesstoken = $oauth2client->getlonglivedaccesstoken($accesstoken);         $request->session()->put('facebook_access_token', (string) $longlivedaccesstoken);         $request->session()->save();     }      if ($accesstoken) {         $fb->setdefaultaccesstoken($accesstoken);     } else {         die('no access token');     } 

i use laravel, session handling not framework agnostic. code example, better create service , move logic there.

now. first problem: while first call graph returned error: (#210) param id must page.

again confusing 2 things '{user-id}/feed' , '{page-id}/feed'. aim post carousel images , links. create carousel have provide child_attachments in fields array. while can send post '{page-id}/feed', cannot '{user-id}/feed'. so @ moment cannot post carousel users feed. when graph api getting post data applicable '{page-id}/feed', assuming have passed {page-id}. , when getting {user-id} instead, graph api yelled "(#210) param id must page.".


Comments