Error using Facebook login repeatedly

Asked

Viewed 61 times

4

I’m using the Facebook API to log data into the database. As soon as I open and browser works well, but when I try to register more than once this error appears:

Uncaught excepetion 'InvalidArgumentException' with message 'The defautl access token must be of type "String" or Facebook\AccessToken'

After closing the browser goes back to normal, so I believe it is a problem to read the data in SESSION. Follows my code:

<?php
    session_start();
    include_once("php/conexao.php");
    require_once __DIR__ . '/lib/Facebook/autoload.php';

    $fb = new \Facebook\Facebook([
      'app_id' => '**********',
      'app_secret' => '**************',
      'default_graph_version' => 'v2.10',
      ;

    $helper = $fb->getRedirectLoginHelper();
    //print_r($helper);
    $permissions = ['email'];

    try {
        if(isset($_SESSION['face_access_token'])){
            $accessToken = $_SESSION['face_access_token'];
        }else{
            $accessToken = $helper->getAccessToken();
        }
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
    }

    if(!isset($accessToken)){
        $url_login = 'https://fidecard.net/conclui_cadastro_cliente.php';
        $loginUrl = $helper->getLoginUrl($url_login, $permissions);
    }else{
        $url_login = 'https://fidecard.net/conclui_cadastro_cliente.php';
        $loginUrl = $helper->getLoginUrl($url_login, $permissions);

        if(isset($_SESSION['face_access_token'])){
            $fb -> setDefaultAccessToken($_SESSION['face_access_token']);
        }else{
            $_SESSION['face_access_token'] = (string)$accessToken;
            $oAuth2Client = $fb -> getOAuth2Client();
            $_SESSION['face_access_token'] = $oAuth2Client->getLongLivedAccessToken($_SESSION['face_access_token']);
            $fb->setDefaultAccessToken($_SESSION['face_access_token']);
        }


        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $fb->get('/me?fields=name, email');
            $user = $response->getGraphUser();
            $nome_cliente = $user['name'];
            $email_cliente = $user['email'];
            $cpf_cliente = $_SESSION['cpf_cliente'];

            $result = "UPDATE cliente SET email_cliente = '$email_cliente', nome_cliente = '$nome_cliente', confirma_email_cliente = true WHERE cpf_cliente = '$cpf_cliente'";
            $resultado = mysqli_query($link, $result);          
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
          echo 'Graph returned an error: ' . $e->getMessage();
          exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
          echo 'Facebook SDK returned an error: ' . $e->getMessage();
          exit;
        }
    }
?>
  • 1

    If possible click on [Edit] and post the error in the format text.

  • What is line 41? The token is a sequence of numbers?

  • After $fb->setDefaultAccessToken($_SESSION['face_access_token']); add $_SESSION['face_access_token'] = (string)$accessToken;

  • line 41 is this: if(isset($_SESSION['face_access_token']){ $fb -> setDefaultAccessToken($_SESSION['face_access_token']); }

  • @Victorfialho you have tried the tip above?

  • I tried, it didn’t work, it doesn’t seem to recognize as string, but it should be a string

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.