the script gives error "The parameter app_id is required" in facebook login even when passed in the code

15,524

I suspect that problem might be hiding here:

<form name="fblogin" action="<?php echo $loginUrl;?>">
        <input type="submit" name="fbsubmit" value="Login with Facebook"/>
</form>

You are sending get request to the generated link, why? User is supposed to follow that link and give your application permissions, try something like this instead of the form:

<a href="<?php echo $loginUrl?>">Login with Facebook </a>

In order to redirect user to certain page, add redirect_uri to getLoginUrl:

$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, user_birthday,user_location,email,first_name,last_name,gender',
redirect_uri => PAGE_URL
));

Make sure that this page belongs to the domain your set up in app settings.

Share:
15,524
The-Rohan D. Shah
Author by

The-Rohan D. Shah

Updated on June 04, 2022

Comments

  • The-Rohan D. Shah
    The-Rohan D. Shah almost 2 years

    i have developed a website in which i want to incorporate "login with facebook" button. i have coded the necessary part and also made the facebook application for it..in my code i have even passed the app id and secret key. Even after doing so when i press the button to login via facebook it gives me an error "The parameter app_id is required" . this is my login script

        <?php 
                include 'fbaccess.php'
                if(empty($user))
                {
                ?>
                <form name="fblogin" action="<?php echo $loginUrl;?>">
                <input type="submit" name="fbsubmit" value="Login with Facebook"/>
                </form>
                <?php                   
                } 
                else {
                    echo $user_info;
                }
                ?>
    

    and this is my fbaccess.php code

        <?php
         $app_id        = APP_ID;
         $app_secret    = APP_SECRET;
         $site_url  = "www.jajabora.com/index.php";
    
         include_once "src/facebook.php";
    
         //creating the object of facebook from the API
         $facebook = new Facebook(array(
     'appId'        => $app_id,
     'secret'   => $app_secret,
      ));
    
          //getting the user id to check whether the user is logged in or not
           $user = $facebook->getUser();
    
          //if user is not authenticated api/me will throw an exception, hence we will know                he isnt logged in after logging out
          /*checks if the user is logged in or not*/if($user){
          // Single query method 
      try{
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
       }/*if exception the user has logged out after logging in hence not authenticated*/
      catch(FacebookApiException $e){
        error_log($e);
        $user = NULL;
      }
          // Single query method ends 
          }
           if($user){
      // Get logout URL
      $logoutUrl = $facebook->getLogoutUrl();
          }else{
      // Get login URL
      $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'publish_stream, user_birthday,user_location,email,first_name,last_name,gender',
        ));
           }
          if($user){
      // Proceed knowing you have a logged in user who has a valid session.
    
          //========= Batch requests over the Facebook Graph API using the PHP-SDK ========
      // Save your method calls into an array
      $queries = array(
        array('method' => 'GET', 'relative_url' => '/'.$user)/*,
        array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
        array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
        array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),*/
        );
    
       // POST your queries to the batch endpoint on the graph.
       try{
        $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
         }catch(Exception $o){
        error_log($o);
          }
    
        //Return values are indexed in order of the original array, content is in   ['body'] as a JSON
       //string. Decode for use as a PHP array.
        $user_info      = json_decode($batchResponse[0]['body'], TRUE);
        $feed           = json_decode($batchResponse[1]['body'], TRUE);
        /*$friends_list     = json_decode($batchResponse[2]['body'], TRUE);
        $photos         = json_decode($batchResponse[3]['body'], TRUE);*/
            //========= Batch requests over the Facebook Graph API using the PHP-SDK ends =====
    
           try {
       $publishStream = $facebook->api("/$user/feed", 'post', array(
        'message'       => 'Check out jajabora.com',
        'link'          => 'http://jajabora.com'/*,
        'picture'       => 'http://25labs.com/images/25-labs-160-160.jpg'*/,
        'name'          => 'Jajabora',
        'caption'       => 'jajabora.com',
        'description'       => 'A carpooling website. highly recommended to  save fuel and cost of travel',
        ));
               }catch(FacebookApiException $e){
             error_log($e);
                   }
                    }
                  ?>
    

    Please guide me. i am a newbie in facebook login coding