php - How to get access token facebook api correct way? -
just playing around facebook api couldn't basic setup.downloaded sdk facebooksdk. cant seem access token working
$fb = new facebook\facebook([ 'app_id' => 'keyxx', 'app_secret' => 'key', 'default_graph_version' => 'v2.4', ]); $helper = $fb->getredirectloginhelper(); $accesstoken = $helper->getaccesstoken(); try { // returns `facebook\facebookresponse` object $response = $fb->get('/me?fields=id,name',$accesstoken); } catch(facebook\exceptions\facebookresponseexception $e) { echo 'graph returned error: ' . $e->getmessage(); exit; } catch(facebook\exceptions\facebooksdkexception $e) { echo 'facebook sdk returned error: ' . $e->getmessage(); exit; } $user = $response->getgraphuser(); echo 'name: ' . $user['name']; ?>
forgot mention app canvas try redirecting not work. see blank canvas
<?php require_once 'facebook-php-sdk-v4/src/facebook/autoload.php'; session_start() echo 'hello'; $fb = new facebook\facebook([ 'app_id' => 'x', 'app_secret' => 'y', 'default_graph_version' => 'v2.4', 'default_access_token' => isset($_session['facebook_access_token']) ? $_session['facebook_access_token'] : 'x|y' ]); try { $response = $fb->get('/me?fields=id,name'); $user = $response->getgraphuser(); echo 'name: ' . $user['name']; exit; //redirect, or whatever want } catch(facebook\exceptions\facebookresponseexception $e) { //echo 'graph returned error: ' . $e->getmessage(); } catch(facebook\exceptions\facebooksdkexception $e) { //echo 'facebook sdk returned error: ' . $e->getmessage(); } $helper = $fb->getredirectloginhelper(); $permissions = ['email', 'user_likes']; $loginurl = $helper->getloginurl('http://www.unsquaringthewheel.com/newtemp/login-callback.php', $permissions); echo '<a href="' . $loginurl . '">log in facebook!</a>'; ?>
this callback login page
<?php require_once 'facebook-php-sdk-v4/src/facebook/autoload.php'; session_start(); $fb = new facebook\facebook([ 'app_id' => 'x', 'app_secret' => 'y', 'default_graph_version' => 'v2.4', 'default_access_token' => 'x|y' ]); $helper = $fb->getredirectloginhelper(); try { $accesstoken = $helper->getaccesstoken(); } catch(facebook\exceptions\facebookresponseexception $e) { // when graph returns error //echo 'graph returned error: ' . $e->getmessage(); } catch(facebook\exceptions\facebooksdkexception $e) { // when validation fails or other local issues //echo 'facebook sdk returned error: ' . $e->getmessage(); } if (isset($accesstoken)) { // logged in! $_session['facebook_access_token'] = (string) $accesstoken; } elseif ($helper->geterror()) { // user denied request } header('location: index.php');
$fb = new facebook\facebook([ 'app_id' => '{app-id}', 'app_secret' => '{app-secret}', 'default_graph_version' => 'v2.4' ]); $res = $fb->get('/me', '{access-token}');
Comments
Post a Comment