Obligate to allow email in facebook api

1

Hello.

I've been using facebook's api (http) for some time, but I detected a problem a few days ago ... There are people removing the email permission which later causes inconsistencies in the system.

Is there a way to force you to see the email? That is, you can not go ahead if you do not let the email go.

I was trying another solution. In the callback see if you have the email variable or not. The problem is that the user can not log in again because on facebook already is stored their preference of not allowing the email.

    
asked by anonymous 12.10.2015 / 17:36

1 answer

2

I do not know how you are using and what version, but the logic does not change much, if you did something like:

$fb = new Facebook\Facebook(array(
    'app_id' => '{app-id}',
    'app_secret' => '{app-secret}',
    'default_graph_version' => 'v2.2'
});

try {
    $response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph retornou um erro: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK retornou um erro: ' . $e->getMessage();
    exit;
}

$user = $response->getGraphUser();

if (empty($user['email'])) {
      echo 'Esta página requer que você informe o seu e-mail';
      exit;
}

//Resto do script...

Now if the first access still has the e-mail and only in the future the user removes the e-mail, you can save the e-mail in a database.

$user = $response->getGraphUser();

$result = mysqli_query($link, 'SELECT id, email FROM users WHERE idUser = ' . $user['id']);
if (false === $result) {
    echo 'Erro na query';
    exit;
}

$data = mysqli_fetch_assoc($res);

if ($data === NULL && empty($user['email'])) {
      //Emite erro se não houver usuário no banco e nem email no acesso
      echo 'Esta página requer que você informe o seu e-mail';
      exit;
} else if ($data === NULL && empty($user['email'])) {
      //Grava email no banco se ainda não existir
      if (false === mysqli_query($link, "INSERT INTO user (idUser, email) VALUES ($id, $email)")) {
           echo 'Erro ao inserir no banco';
           exit;
       }
} else if ($data !== NULL && false === empty($user['email']) &&
            $user['email'] !== $data['email']
) {
      //Atualiza email do banco de dados se ambos forem diferentes
      if (false === mysqli_query($link, "UPDATE user SET email = '" . $user['email'] . "' WHERE id = " . $data['id'])) {
           echo 'Erro ao atualizar o email';
           exit;
       }
} else {
      $user['email'] = $data['email']; //Seta a variavel vinda do banco na sua variavel principal
}

//Resto do script...

Note that the script is only illustrative, you can use any database that is within reach of your server, in case I used a simple example and may not be functional, but it is just to understand the logic, then read the documentation:

12.10.2015 / 18:09