Generate automatic access token with Curl
Automatic Generate Facebook access token you just need the client app id, secret key and short time access token to generating long time access token.
Here is the code :
$ch1 = curl_init(); //this part we set up curl
curl_setopt($ch1, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?client_id='.$wkapiid.'&client_secret='.$wkappsecret.'&grant_type=fb_exchange_token&fb_exchange_token='.$accesstoken.'');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_TIMEOUT, 15);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
$longaccesstoken = curl_exec($ch1);
curl_close($ch1);
header('Content-type: application/xml'); //specify as xml to not display as one long string
$data=json_decode($longaccesstoken);
This is the full code which is used to generate access token for each time just before 2 month.
jimport( 'joomla.filesystem.file' );
jimport( 'joomla.filesystem.stream' );
function getName($file) {
$slash = strrpos($file, DS);
if ($slash !== false) {
return substr($file, $slash + 1);
} else {
return $file;
}
}
$wkdates = getdate();
$searchdir = "modules/mod_facebookwall/elements";
$filestext = glob($searchdir.'/*.txt', GLOB_BRACE);
$filename = getName(JPATH_ROOT."/".$filestext[0]);
$accesstokenfinal = file_get_contents(JPATH_ROOT."/".$filestext[0]);
$filetime = explode(".txt", $filename);
$filerealtime = $filetime[0];
if($filerealtime < $wkdates[0])
{
if($filerealtime==0){
$ch1 = curl_init(); //this part we set up curl
curl_setopt($ch1, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?client_id='.$wkapiid.'&client_secret='.$wkappsecret.'&grant_type=fb_exchange_token&fb_exchange_token='.$accesstoken.'');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_TIMEOUT, 15);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
$longaccesstoken = curl_exec($ch1);
curl_close($ch1);
header('Content-type: application/xml'); //specify as xml to not display as one long string
$data=json_decode($longaccesstoken);
if($data->error->code==190){
echo "Error validating access token, Please Give right Accesstoken for that APPS ";
}
elseif ($data->error->code==101) {
echo "Error validating application, Please Give right API ID for that APPS ";
}
elseif ($data->error->code==1) {
echo "Error validating client secret, Please Give right Secret Key for that APPS ";
}
else{
$stripacceess = explode("access_token=", $longaccesstoken);
$finalstripacees = explode("&expires=", $stripacceess[1]);
$accesstokenfinal = $finalstripacees[0];
$explodedate = $finalstripacees[1];
$changefiletime = $wkdates[0] + $explodedate -2000;
$tempfilepath = JPATH_ROOT . DS . 'modules' . DS . 'mod_facebookwall'. DS . 'elements';
$fileIniName = JPATH_ROOT."/modules/mod_facebookwall/elements/".$filerealtime.".txt";
$fileChangeName = JPATH_ROOT."/modules/mod_facebookwall/elements/".$changefiletime.".txt";
file_put_contents(JPATH_ROOT."/".$filestext[0], $accesstokenfinal);
rename($fileIniName, $fileChangeName);
}
}
else{
$ch = curl_init(); //this part we set up curl
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?client_id='.$wkapiid.'&client_secret='.$wkappsecret.'&grant_type=fb_exchange_token&fb_exchange_token='.$accesstokenfinal.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$longaccesstoken = curl_exec($ch);
curl_close($ch);
header('Content-type: application/xml'); //specify as xml to not display as one long string
$data=json_decode($longaccesstoken);
if($data->error->code==190){
echo "Error validating access token, Please Give right Accesstoken for that APPS ";
}
elseif ($data->error->code==101) {
echo "Error validating application, Please Give right API ID for that APPS ";
}
elseif ($data->error->code==1) {
echo "Error validating client secret, Please Give right Secret Key for that APPS ";
}
else{
$stripacceess = explode("access_token=", $longaccesstoken);
$finalstripacees = explode("&expires=", $stripacceess[1]);
$accesstokenfinal = $finalstripacees[0];
$explodedate = $finalstripacees[1];
$changefiletime = $wkdates[0] + $explodedate -2000;
$tempfilepath = JPATH_ROOT . DS . 'modules' . DS . 'mod_facebookwall'. DS . 'elements';
$fileIniName = JPATH_ROOT."/modules/mod_facebookwall/elements/".$filerealtime.".txt";
$fileChangeName = JPATH_ROOT."/modules/mod_facebookwall/elements/".$changefiletime.".txt";
file_put_contents(JPATH_ROOT."/".$filestext[0], $accesstokenfinal);
rename($fileIniName, $fileChangeName);
}
}
}
View Comments
Comment or Ask a Question
Quick Links