0
votes

To add social login Capability to Website, I am using hybridauth php library open source social sign.

But when I want to login to site via twitter account mentioned error message is shown without any details.

"Authentication failed! Twitter returned an error"

I think that all scripts and options is correct However still displayed error.

This is a piece of config.php file :

return
    array(
        "base_url" => "http://emrc-tgf.org/vendor/hybridauth/hybridauth/hybridauth/",
        "providers" => array(
            "Google" => array (
                "enabled" => true,
                "keys"    => array ( "id" => "82763127799-sfdsoe990b9ugu0g7l0mlll0u9phvkj1.apps.googleusercontent.com", "secret" => "THIS IS SECRET" ),
            ),
            "Facebook" => array(
                "enabled" => true,
                "keys" => array("id" => "867928933297942", "secret" => "THIS IS SECRET"),
                "scope"   => "email",
                'trustForwarded'=>false
            ),
            "Twitter" => array(
                "enabled" => true,
                "keys" => array("key" => "siXI8ArTFNmlCkwqIDUxQZSha", "secret" => "THIS IS SECRET")
            )
    )

and this is my login Script :

if (isset($_GET['provider'])) {

    // the selected provider
    $provider_name = $_REQUEST["provider"];

    try
    {
        // initialize Hybrid_Auth class with the config file
        $hybridauth = new Hybrid_Auth( '../vendor/hybridauth/hybridauth/hybridauth/config.php' );

        // try to authenticate with the selected provider
        $adapter = $hybridauth->authenticate( $provider_name );

        // then grab the user profile
        $user_profile = $adapter->getUserProfile();

    }
    catch( Exception $e )
    {
        exit ($e->getMessage()) ;
        /*header("Location: ./");*/
    }


    $user_exist = get_user_by_provider_and_id( $provider_name, $user_profile->identifier );
//        print_r($user_exist);
    // if the used didn't authenticate using the selected provider before
    // we create a new entry on database.users for him
    if($user_exist['count']==0 )
    {
        $user_id_generated  =
            create_new_hybridauth_user(
                $user_profile->email,
                $user_profile->firstName,
                $user_profile->lastName,
                $provider_name,
                $user_profile->identifier
            );

        if ($user_id_generated) {
            $userID     =   $user_id_generated;
        }
    }else{
        $userID     =   $user_exist['user_id'];
    }

    // set the user as connected and redirect him
    $_SESSION["user"] = $userID;
    header('location:/forum');

}//else if (isset($_GET['provider'])) {

Does anyone know the solution is ?

1

1 Answers

0
votes

Had the same issue. Make sure you have a callback URL set in your Twitter app settings. Whatever page you want your users to see after they log in.