Facebook PHP SDK 4.0 : Getting Long Term Access Token

前端 未结 3 1858
抹茶落季
抹茶落季 2021-02-04 19:13

I\'m trying to use the PHP sdk v4.0 to get a long term access token for PAGE management.

I\'m grabbing the access token from the user\'s login (Yes, I\'m grabbing the Pa

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 19:19

    Use this code PHP

    getSessionFromRedirect();
    
    	if ($session):
    		$_SESSION['facebook'] = $session->getToken();
    		header('Location: index.php');
    	endif;
    
    	if (isset($_SESSION['facebook'])):
    		$session = new FacebookSession($_SESSION['facebook']);
    		$page_id = '000000000000';
    		// get page access token
    		$access_token = (new FacebookRequest( $session, 'GET', '/' . $page_id,  array( 'fields' => 'access_token' ) ))
        ->execute()->getGraphObject()->asArray();
     
    			// save access token in variable for later use  
    		$access_token = $access_token['access_token'];
    		
    		$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
        		'access_token' => $access_token,
    		    'name' => 'TITULO DEL ENLACE',
    		    'link' => 'http://www.example.com/',
    		    'caption' = 'Example text',
    		    'message' => 'This is my link!',
      ) ));
    		$response = $page_post->execute();
    		$graphObjectClass = $response->getGraphObject();
    		$facebook_POST = $graphObjectClass;
    	endif;
    } catch(FacebookRequestException $ex) {
      // When Facebook returns an error
      echo $ex;
    } catch(\Exception $ex) {
      // When validation fails or other local issues
      echo $ex;
    }
    ?>

提交回复
热议问题