Facebook login php hash_hmac() problem after getiing data

I am trying to create a facebook login via php based on this tutorial: https://youtu.be/AMoozUkI2Fg

Everything is fine, I have downloaded data and saved in the session, but I get an error:

Warning: hash_hmac () expects parameter 2 to be string, object given in classes / fb-php-sdk / vendor / facebook / graph-sdk / src / Facebook / Authentication / AccessToken.php on line 70

Probably for this code:

        if(isset($_SESSION['facebook_access_token'])){
            $facebook_access_token = $_SESSION['facebook_access_token'];
        }else{
            $facebook_access_token = $facebook_helper->getAccessToken();
            $_SESSION['facebook_access_token'] = $facebook_access_token;
            $facebook->setDefaultAccessToken($_SESSION['facebook_access_token']);
        }

        $graph_response = $facebook->get('/me?fields=name,email', $facebook_access_token);

1 answer

  • answered 2021-05-14 17:37 maniek

    $facebook = new \Facebook\Facebook([
        'app_id' => 'APP_ID',
        'app_secret' => 'APP_SECRET',
        'default_graph_version' => 'v10.0',
    ]);
          
    define('FB_REDIRECT_URL', $redirect_url);
    
    // Get redirect login helper
    $facebook_helper = $facebook->getRedirectLoginHelper();
    $_SESSION['FBRLH_state'] = $_GET['state'];
    
    if(($_GET['code'] && $_GET['state'])) {
    
        if(isset($_SESSION['facebook_access_token'])){
            $facebook_access_token = $_SESSION['facebook_access_token'];
        }else{
            $facebook_access_token = $facebook_helper->getAccessToken();
            $_SESSION['facebook_access_token'] = $facebook_access_token;
            $facebook->setDefaultAccessToken($_SESSION['facebook_access_token']);
        }
    
        $graph_response = $facebook->get('/me?fields=name,email', $facebook_access_token);
    
        $facebook_user_info = $graph_response->getGraphUser();
    
        if(!empty($facebook_user_info['name']))
        {
            $_SESSION['facebook_user_name'] = $facebook_user_info['name'];
        }
    
        if(!empty($facebook_user_info['email']))
        {
            $_SESSION['facebook_user_email'] = $facebook_user_info['email'];
        }
    }
    else
    {
        $facebook_permissions = ['email'];
    
        $facebook_login_url = $facebook_helper->getLoginUrl(FB_REDIRECT_URL, $facebook_permissions);
    }