Search code examples
phpfacebook-graph-api

Can't get the renew access token in FACEBOOK APP


I'm working in Facebook bot that replies to some comments (so far so good), yet at some point I need to renew the user authentification token (access_token), yet I still can't get it right. This is my webhook so far...yet I can't get the access token. I used 3 methods yet still nothing. How can I get the renew access token or how to get it with logic that I'm using? No FB SDK implemented so I rather would like to know a solution based on this code.

<?php

if(isset($_GET['hub_mode']) && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token'])){
    if($_GET['hub_verify_token'] == '1234567890')echo $_GET['hub_challenge'];
}else{
    
    
    $feedData = file_get_contents('php://input');
    $data = json_decode($feedData);
    $code = NULL;
    //$code = $_REQUEST["code"]; //I Tried $_REQUEST yet I can't get the code value.
    
    if (isset($_GET['code'])) { //I tried $_GET yet still no luck.
        $code = $_GET['code'];
        $app_id = 4444444444444444;
        $my_url = 'https://example.com/robots/jotabot-comment-replier/fbwebhook.php';
        $app_secret = 'ee....8';
        
        $token_url="https://graph.facebook.com/oauth/access_token?client_id="
            . $app_id . "&redirect_uri=" . urlencode($my_url)
            . "&client_secret=" . $app_secret
            . "&code=" . $code . "&display=popup";
            $response = file_get_contents($token_url);
            $params = null;
            parse_str($response, $params);
            $access_token = $params['access_token'];
            
            //KEEP GOING FROM HERE WITH TOKEN.
            http_response_code(200);
            return;
    }
    
    if($data->object == "page"){
        
        $userMsg = $data->entry[0]->changes[0]->value->message;
        $commentID = $data->entry[0]->changes[0]->value->comment_id;
        $accessToken = "EAAKGo...AZDZD";
        $url = 'https://graph.facebook.com/v15.0/me/messages?access_token='.$accessToken;
        
        $reply = "Thanks for your comment";
        $btn1 = array("type"=>"postback","title"=>"OPTION 1","payload"=>"BUDGET_10_PAYLOAD");
        $btn2 = array("type"=>"postback","title"=>"OPTION 2","payload"=>"BUDGET_20_PAYLOAD");
        $data = array("recipient"=>array("comment_id"=>$commentID),"message"=>array("attachment"=>array("type"=>"template","payload"=>array("template_type"=>"button","text"=>$reply,"buttons"=>[$btn1,$btn2]))));
        $postdata = json_encode($data);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        $response = curl_exec($ch);
        curl_close($ch);
      
         
        $data = json_decode($response);        
        $errorCode = $data->error->code;
       
        
        // If we had an error code that means that there is an authentification error.
        if ($errorCode) {
            
            $my_url = 'https://example.com/robots/jotabot-comment-replier/fbwebhook.php';
            $token_url="https://graph.facebook.com/oauth/authorize?client_id=88884888&redirect_uri=".urlencode($my_url);
            
            //If I put this URL with this parameters manually in my browser I successfully got the redirection with the code that I need: 
            //https://example.com/robots/jotabot-comment-replier/fbwebhook.php?code=AQS...Puw#_=_
            
            //I tried 3 methods, JS (1st is how documentation suggests), file_get_contents and cURL.

           //echo("&lt;script> top.location.href='" . $token_url . "'&lt;/script>"); //I tried the way the documentation suggests, yet still no luck.
            
            //$response = file_get_contents($token_url);
            //$params = null;
            //parse_str($response, $params);
            //$access_token = $params['access_token']; //There's no $access_token
                        
            $url = $token_url;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);          
            $result = curl_exec($ch); //I tried to catch the code after the redirection yet I can't.
            curl_close($ch);
             
        }
            
    }
    
}

http_response_code(200);

Solution

  • You can't renew your User's access_token without going through Facebook Login flow.

    That's why for your application it's better to use Page access_token which has no expiration date. You can read here how to obtain one