Facebook Login using HybridAuth showing error You

2019-04-09 22:22发布

问题:

I am trying to write a plugin for my website to connect with facebook using the HybridAuth classes.

I just tried following code

function authenticatewith( $provider ) {
    ini_set('display_errors','on');

    //includes
    $config   = dirname(__FILE__) . '/hybridauth-2.1.2/hybridauth/config.php';
    require_once("hybridauth-2.1.2/hybridauth/Hybrid/Auth.php");

    $provider_name = $provider;

    //$config = $this->getconfig($id);
    try {
        // initialize Hybrid_Auth with a given file
        $hybridauth = new Hybrid_Auth($config);

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

        // then grab the user profile 
        $user_profile = $adapter->getUserProfile();
    }
    catch( Exception $e ) {
        echo "Error: please try again!";
        echo "Original error message: " . $e->getMessage();
    }

    echo "User Details: ";
    var_dump($user_profile);
}

When I call this function form the plugin class. In the browser it shows the following error:

You cannot access this page directly.

...and the URL in the address bar of the browser is something like this:

http://zyx.com/oinmonm/plugins/sociallogin/hybridauth-2.1.2/hybridauth/?hauth.start=Facebook&hauth.time=1415168326

After searching stackoverflow I found a similar question that describes about similar problem, but I have not been able to figure out how to apply the suggestions there to my code fix the issue:

You cannot access this page directly - HybridAuth Social Login

Most probably the problem is with the different domain names, as my website is running on two different domains.

How can I fix my code to prevent this issue?

More Details

I have a facebook link on www.bca.com(example) then when you click it goes to a controller.php file that will access the plugin that I am writing . Usually if I save a folder in the website like the plugin folder that is not accessible by www.bca.com instead its accessible by some other domain name .

What I am trying to say is that the session is starting in bca.com but the Hybridauth classes are saved in some other domain . And I think this is the reason the error is coming.

I tried to debug and found out the error is popping from following lines in Endpoint.php file

// Check if Hybrid_Auth session already exist
if (! $storage->config("CONFIG")) { 
    header("HTTP/1.0 404 Not Found");
    die("You cannot access this page directly.");
}

I have been trying to fix this from last 3 days . But I am stuck at this point plz some one help me out. Thanks in advance

回答1:

Try after adding @session_start(); statement at the top of your files.



回答2:

I had the same issue. It relates to our custom session handler which is set by session_set_save_handler(). Hybrid Auth uses standard PHP sessions, so after redirecting and opening a new session, Hybrid Auth starts using standard PHP file sessions instead of your custom session handler. This result in the loss of config data from our session and getting this error message.

I resolved this issue by adding our own custom session handler at the top of hybridauth/index.php (located in the same dir as config.php and live.php). This forces Hybrid Auth to use your custom session handler.



回答3:

For anyone with the same problem have a question: The file that calls the API is in the same directory it?

Me only worked when I put my file in the same folder as the config.php file. Try it there and tell me if it works!