Hi all,

I'm trying to make a Facebook login for a website, which also stores users' data who log in.

In order to do this, I do have an index.php

<script>
  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {  
        var accessToken = response.authResponse.accessToken;
      // Logged into your app and Facebook.

      console.log("token = " + accessToken);
      window.location.href = "login.php?token=" + accessToken;
    } else {
      // The person is not logged into your app or we are unable to tell.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
         document.getElementById('status').innerHTML = '' +
        'Alcuni dati serviranno ad ottimizzare domande e contenuti per te.';
    }

  } 

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  window.fbAsyncInit = function() {
    FB.init({
      appId      : '943543095813368',
      cookie     : true,  // enable cookies to allow the server to access 
                          // the session
      xfbml      : true,  // parse social plugins on this page
      version    : 'v2.12' // use graph api version 2.8
    });

    // Now that we've initialized the JavaScript SDK, we call 
    // FB.getLoginStatus().  This function gets the state of the
    // person visiting this page and can return one of three states to
    // the callback you provide.  They can be:
    //
    // 1. Logged into your app ('connected')
    // 2. Logged into Facebook, but not your app ('not_authorized')
    // 3. Not logged into Facebook and can't tell if they are logged into
    //    your app or not.
    //
    // These three cases are handled in the callback function.

    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });

  };

  // Load the SDK asynchronously
  (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = 'https://connect.facebook.net/it_IT/sdk.js#xfbml=1&version=v2.12&appId=2101457016807136&autoLogAppEvents=1';
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }
</script> <!--
  Below we include the Login Button social plugin. This button uses
  the JavaScript SDK to present a graphical Login button that triggers
  the FB.login() function when clicked.
--> <div align="center"> <img src="logo.png" width="200"> <h1>LOG IN TO START THE QUIZ</h1> <fb:login-button autologoutlink="true"
  scope="public_profile,email,user_relationships,user_religion_politics,user_work_history,user_education_history,user_birthday,user_hometown"
  onlogin="checkLoginState();"> </fb:login-button> <div id="status">

which takes logged user to login.php?token= his token

<?php $token = $_GET['token']; 
$memetoken = "'access_token=" . $token . "'";
?> <?php

$facebook_data_array = array(
  'base_url'     => 'https://graph.facebook.com/v2.12/',
  'node'         => 'me?',
  //'query_param'  => 'q=',
  //'root_node'    => 'type=application',
  //'coords'       => 'center=35.7796,-78.6382',
  'fields'       => 'fields=id,name,email,education,birthday,political,hometown,work,relationship_status',
  'access_token' => $memetoken,
    //. FB_API_KEY,
);

/**
 * The get_facebook_data function accepts one argument, the array of
 * values that comprise the URL to the data we are trying to collect. We
 * simply concatenate the values and store the string in the $url
 * variable. We're using PHP's built in curl library to pass the URL to
 * the browser and storing the response in the $response varialbe.
 */

function get_facebook_data( $args ) {

  /* Concatenate the array values. */
  $url = $args['base_url'] . $args['node'] . $args['query_param'] . '&' . $args['root_node'] . '&' . $args['coords'] . '&' . $args['fields'] . '&' . $args['access_token'];

  /* Initiate request. Store the results in the $response varialbe */
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );
  $response = curl_exec( $ch );
  curl_close( $ch );

  /* Return the values in the $response variable. */
  return $response;

}

/**
 * Call the function and display the results in the browser.
 */

get_facebook_data( $facebook_data_array );

?> <script>
  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      //window.location = "start";
    } else {     
      // The person is not logged into your app or we are unable to tell.
     // document.getElementById('status').innerHTML = '' +
        //'Alcuni dati serviranno ad ottimizzare domande e contenuti per te.';
    }
  }

</script>

And at that point here I get the error

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500,"fbtrace_id":"Epvw09aJoEc"}}

Now, I'm new into Facebook API so my code is probably like trash, but I'd really really apprecciate any kind of help about this issue, or alternately another way to get user's data granted on the Facebook Login access stored when he logs in.

Thanks

Sebastiano

Hi Hemendra_2,

thanks for answering.

I noticed the only difference between your SDK load code and mine was an opening and closing brace.

Unfortunately that hasn't changed the error result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.