-
Notifications
You must be signed in to change notification settings - Fork 31
Access Tokens
To get an access token, users must first login through the facebook login dialog. Facebook will then send the user back to your redirect uri with a code variable in the url which you then exchange for an access token.
Display a link for the user to click on and login with Facebook. Once the user logs in with Facebook, Facebook will send them to the redirect uri. The redirect uri in your code must also be entered in the Facebook Login settings of you app under "Valid OAuth Redirect URIs".
use Instagram\FacebookLogin\FacebookLogin;
$config = array( // instantiation config params
'app_id' => '<FB_APP_ID>', // facebook app id
'app_secret' => '<FB_APP_SECRET>', // facebook app secret
);
// uri facebook will send the user to after they login
$redirectUri = 'https://path/to/fb/login/redirect.php';
$permissions = array( // permissions to request from the user
'instagram_basic',
'instagram_content_publish',
'instagram_manage_insights',
'instagram_manage_comments',
'pages_show_list',
'ads_management',
'business_management',
'pages_read_engagement'
);
// instantiate new facebook login
$facebookLogin = new FacebookLogin( $config );
// display login dialog link
echo '<pre><a href="' . $facebookLogin->getLoginDialogUrl( $redirectUri, $permissions ) . '">' .
'Log in with Facebook' .
'</a>';
Once the user logs in through Facebook, Facebook directs them to your redirect uri and appends on a code. For the above example once the user logs in, Facebook would redirect them to "https://path/to/fb/login/redirect.php?code={code}". We then can exchange this code for the access token.
use Instagram\AccessToken\AccessToken;
$config = array( // instantiation config params
'app_id' => '<FB_APP_ID>', // facebook app id
'app_secret' => '<FB_APP_SECRET>', // facebook app secret
);
// we also need to specify the redirect uri in order to exchange our code for a token
$redirectUri = 'https://path/to/fb/login/redirect.php';
// instantiate our access token class
$accessToken = new AccessToken( $config );
// exchange our code for an access token
$newToken = $accessToken->getAccessTokenFromCode( $_GET['code'], $redirectUri );
if ( !$accessToken->isLongLived() ) { // check if our access token is short lived (expires in hours)
// exchange the short lived token for a long lived token which last about 60 days
$longLived = $accessToken->getLongLivedAccessToken( $newToken['access_token'] );
// we have a long lived user access token!
$userAccessToken = $longLived['access_token'];
}