ccc

API Facebook: Obtener todos los datos de un usuario

Registrar una APP en https://developers.facebook.com/ para obtener las claves

<!DOCTYPE html>
<html>
<head>
  <title>Facebook Login JavaScript Example</title>
  <meta charset="UTF-8">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
</head>
<body style="background-color:#2C2961;color:#FFF;margin:0 auto;width:800px;">
  <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.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
      'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Por favor, identifícate con tu cuenta de Facebook para que podamos obtener tus datos personales, tu cumpleaños, tu país y tu ciudad para poder almacenarlos en nuestra base de datos';
    }
  }

  // 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      : 'xxx',
    cookie     : true,  // enable cookies to allow the server to access
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.2' // use version 2.2
  });

  // 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 = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

  function show_props(obj, objName) {
    var result = "";
    for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "<br>";
    }
    return result;
  }


  function testAPI() {
    // console.log('Welcome!  Fetching your information.... ');
    FB.api('/me?fields=id,name,email,first_name,last_name,picture,birthday,location', function(response) {
      console.log(response);


    arr = response.location.name.split(" ");
    arr[0] = arr[0].replace(",","");
    arr[0] = arr[0].replace(" ","");
    arr[1] = arr[1].replace(" ","");

    // alert(arr[0]);
    // alert(arr[1]);
    var otra_ciudad = arr[0];

    var cp = "";
    var id_pais = 0;
   
    if (arr[1] == "Spain") {

      cp = "28033";
      id_pais = 73;
    }
   
    arr = response.birthday.split("/");
    var fecha_nacimiento = arr[1]+"/"+arr[0]+"/"+arr[2];


    var azar = Math.floor((Math.random() * 999) + 1);
    var username = "FB_"+response.first_name.substr(0,4)+"_"+response.last_name.substr(0,4)+"_"+azar;
   


    <?php
    // Generar contraseña aleatoria
    $cadena = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
    $longitudCadena = strlen($cadena);
    $pass = "";
    $longitudPW = 10;
   
    for($i=1 ; $i<=$longitudPW ; $i++){
     $pos=rand(0,$longitudCadena-1);
     $pass .= substr($cadena,$pos,1);
   }
   ?>
   var html = '';
   html += '<form id="form_reg_linkedin" name="form_reg_linkedin" action="<?=$rutaReg;?>" method="POST">';
   html += '<input type="hidden" name="nombre_completo" value="'+response.first_name+" "+response.last_name+'">';
   html += '<input type="hidden" name="email" value="'+response.email+'">';
   html += '<input type="hidden" name="esRegLinkedin" value="1">';
   html += '<input type="hidden" name="id_pais" value="'+id_pais+'">';
   html += '<input type="hidden" name="otra_ciudad" value="'+otra_ciudad+'">';
   html += '<input type="hidden" name="cp" value="'+cp+'">';
   html += '<input type="hidden" name="fecha_nacimiento" value="'+fecha_nacimiento+'">';
   html += '<input type="hidden" name="nif" value="2222">';
   html += '<input type="hidden" name="username" value="'+username+'">';
   html += '<input type="hidden" name="pw1" value="<?=$pass;?>">';


   html += '</form>';

$("#divHTML").html(html);


$(function() {
  $("#form_reg_linkedin").submit();
});
});
}
</script>

<fb:login-button scope="public_profile,email,user_birthday,user_location,user_website,user_friends" onlogin="checkLoginState();">
</fb:login-button>


<div id="status">
</div>

<div id="divHTML" name="divHTML" style="margin-top:20px;"></div>

</body>
</html>

No hay comentarios:

Publicar un comentario