Facebook登录PHP SDK

时间:2015-03-09 09:21:41

标签: php facebook facebook-login

使命:要从我的网站成功登录Facebook,返回用户ID,最后发送电子邮件等,以添加到我的数据库数据库。

我从developers.facebook.com下载了最新的4.0.0 SKD PHP并将其上传到我的文件管理器。

我正在使用GitHub上的代码:http://packetcode.com/rd/6e20a9

请参阅index.php

下面的代码
<?

/* INCLUSION OF LIBRARY FILEs*/
    require_once( 'lib/Facebook/FacebookSession.php');
    require_once( 'lib/Facebook/FacebookRequest.php' );
    require_once( 'lib/Facebook/FacebookResponse.php' );
    require_once( 'lib/Facebook/FacebookSDKException.php' );
    require_once( 'lib/Facebook/FacebookRequestException.php' );
    require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
    require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
    require_once( 'lib/Facebook/GraphObject.php' );
    require_once( 'lib/Facebook/GraphUser.php' );
    require_once( 'lib/Facebook/GraphSessionInfo.php' );
    require_once( 'lib/Facebook/Entities/AccessToken.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
    require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');


/* USE NAMESPACES */

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;
    use Facebook\FacebookHttpable;
    use Facebook\FacebookCurlHttpClient;
    use Facebook\FacebookCurl;
/*PROCESS*/

    //1.Stat Session
     session_start();

     //2.Use app id,secret and redirect url 
    $app_id = 'my id';
     $app_secret = 'my secret';
     $redirect_url='my url';

     //3.Initialize application, create helper object and get fb sess
     FacebookSession::setDefaultApplication($app_id,$app_secret);
     $helper = new FacebookRedirectLoginHelper($redirect_url);
     $sess = $helper->getSessionFromRedirect();
    //4. if fb sess exists echo name 
        if(isset($sess)){
            //create request object,execute and capture response
        $request = new FacebookRequest($sess, 'GET', '/me');
        // from response get graph object
        $response = $request->execute();
        $graph = $response->getGraphObject(GraphUser::className());
        // use graph object methods to get user details
        $name= $graph->getName();
        echo "hi $name";
    }else{
        //else echo login
        echo '<a href='.$helper->getLoginUrl().'>Login with facebook</a>';
    }

    ?>

我已经复制了视频教程,当它这样做时它会起作用所以我迷失了我的错误。

当我点击“使用Facebook登录”链接时,它将我带到了Facebook,在我点击“立即播放”时请求了许可,然后将其重定向回我的网址,但是出现以下错误。

现在,每当我点击“登录facebook”链接时,我都会直接看到错误..

Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message 'Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request' in /home/*****/public_html/lib/Facebook/FacebookRequestException.php:104 Stack trace: #0 /home/*****/public_html/lib/Facebook/FacebookRequest.php(268): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 400) #1 /home/****/public_html/lib/Facebook/FacebookRedirectLoginHelper.php(146): Facebook\FacebookRequest->execute() #2 /home/*****/public_html/index.php(48): Facebook\FacebookRedirectLoginHelper->getSessionFromRedirect() #3 {main} thrown in /home/*****/public_html/lib/Facebook/FacebookRequestException.php on line 104

在这里和谷歌看后,我似乎无法找出导致此错误的原因以及如何解决此问题。

1 个答案:

答案 0 :(得分:0)

管理找到答案。

我想在这里发帖,让别人知道谁可能会挣扎。

Verrrry简单..

确保您的重定向网址末尾有一个/ ..

例如,

http://mytestsite.com/