ccc

API Google: Obtener los datos del usuario mediante JS sin necesidad de mandarlo a otra página

En nuestra web:
<button class="button-linkedin" onclick="login_google();"><i class="fa fa-google-plus"></i>Registrarse con Google</button>

En api_google.js:
function login_google() {
  var myParams = {
    'clientid' : 'xxx.apps.googleusercontent.com',
    'cookiepolicy' : 'single_host_origin',
    'callback' : 'loginCallback',
    'approvalprompt':'force',
    'scope' : 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read'
  };
  gapi.auth.signIn(myParams);
}


function generar_password_random(length) {
  var iteration = 0;
  var password = "";
  var randomNumber;
  var special = false;


  while(iteration < length){
    randomNumber = (Math.floor((Math.random() * 100)) % 94) + 33;
    console.log(randomNumber);
    if(!special){
      if ((randomNumber >=33) && (randomNumber <=47)) { continue; }
      if ((randomNumber >=58) && (randomNumber <=64)) { continue; }
      if ((randomNumber >=91) && (randomNumber <=96)) { continue; }
      if ((randomNumber >=123) && (randomNumber <=126)) { continue; }
    }
    iteration++;
    password += String.fromCharCode(randomNumber);
  }
  return password;
}

function loginCallback(result) { // Se llamará a la de google/get_google.php
   if(result['status']['signed_in'])
    {
        var request = gapi.client.plus.people.get(
        {
            'userId': 'me'
        });
        request.execute(function (resp)
        {
            console.log("RESP:");
            console.log(resp);
            var email = '';
            if(resp['emails'])
            {
                for(i = 0; i < resp['emails'].length; i++)
                {
                    if(resp['emails'][i]['type'] == 'account')
                    {
                        email = resp['emails'][i]['value'];
                    }
                }
            }
         

            var str = "Name:" + resp['displayName'] + "<br>";
            str += "Image:" + resp['image']['url'] + "<br>";
            str += "<img src='" + resp['image']['url'] + "' /><br>";

            str += "URL:" + resp['url'] + "<br>";
            str += "Email:" + email + "<br>";
         
            // document.getElementById("profile").innerHTML = str;
         
            // alert("givenName:"+resp["name"]["givenName"]);
         
         
            var azar = Math.floor((Math.random() * 999) + 1);
            var username = "GOO_"+resp["name"]["givenName"].substr(0,4)+"_"+resp["name"]["familyName"].substr(0,4)+"_"+azar;
            // alert(username);
   
              rutaReg = "http://midominio.com/public/es/post_registro";
         
            var pass = generar_password_random(10);
            // alert("Sale del pass:"+pass);
         
            var html = '';
            html += '<form id="form_reg_linkedin" name="form_reg_linkedin" action="'+rutaReg+'" method="POST">';
            html += '<input type="hidden" name="nombre_completo" value="'+resp['displayName']+'">';
            html += '<input type="hidden" name="email" value="'+email+'">';
            html += '<input type="hidden" name="esRegLinkedin" value="1">';
            html += '<input type="hidden" name="id_pais" value="73">';
            html += '<input type="hidden" name="otra_ciudad" value="'+resp['placesLived'][0]['value']+'">';
            html += '<input type="hidden" name="cp" value="28000">';
            html += '<input type="hidden" name="fecha_nacimiento" value="31/12/1977">';
            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>';
            // alert(html);
            // $("#divHTML").html(html);
            document.write(html);
            // alert("Escrito el html");
         
            $(function() {
               //  alert("Enviar form a "+rutaReg);
                $("#form_reg_linkedin").submit();
             });
        });

    }

}
function onLoadCallback()
{
 
    gapi.client.setApiKey('AIzaxxx');
    gapi.client.load('plus', 'v1',function(){});
}


      (function() {
       var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
       po.src = 'https://apis.google.com/js/client.js?onload=onLoadCallback';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
     
     
     })();
   
     $(function() {

     });

No hay comentarios:

Publicar un comentario