2

我正在尝试为我的画布应用程序集成 facebook。当我使用以下代码从 facebook 运行应用程序时

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xx','xx');

$helper = new FacebookCanvasLoginHelper();

try {
    $data = array('oauth_token' => 'token');
    $data['algorithm'] = 'HMAC-SHA256';
    $data['issued_at'] = time();
    $base64data = base64_encode(json_encode($data));
    $rawSig = hash_hmac('sha256', $base64data, 'app_Secret', true);
    $sig = base64_encode($rawSig);

    $signedRequest =  $sig.'.'.$base64data;
    $_GET['signed_request'] = $signedRequest;
    $session = $helper->getSession();


} catch(FacebookRequestException $ex) {
   echo $ex;   
} catch(\Exception $ex) {
   echo $ex;  
}

整个页面只是变成空白的白色,因为 $_GET['signed_request'] = $signedRequest;.

我应该怎么做才能登录。如果我只是这样做 $session = $helper->getSession();而不是 Get,我会得到无效的签名参数 oAuth 数据丢失。

4

3 回答 3

1

你的 PHP 应该是:

$helper = new FacebookCanvasLoginHelper();

try {
    $session = $helper->getSession();
    if($session){
        try {
        $facebook_profile = (new FacebookRequest(
            $session, 'GET', '/me'
        ))->execute()->getGraphObject(GraphUser::className());
        echo $facebook_profile->getName;
    } catch(FacebookRequestException $e) {
    }
}


} catch(FacebookRequestException $ex) {
   echo $ex;   
} catch(\Exception $ex) {
   $facebookLoginHtml = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id={your_app_id}&redirect_uri={your_app_canvas_url}';"; 
}

然后在您的 HTML 中的某处:

<script>
    <?php if(isset($facebookLoginHtml)){ echo $facebookLoginHtml; } ?>
</script>

如果您想请求额外的权限,请在 URL 中添加 scope 参数,如下所示:

$facebookLoginHtml = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id={your_app_id}&redirect_uri={your_app_canvas_url}&scope=publish_actions';"; 

这会将页面重定向到登录页面,然后以适当的权限返回到您的画布应用程序。

这不应该像这样工作,因为它使用 Javascript 和 PHP SDK。这是 Facebook 正在解决的错误,您可以在此处关注:

https://developers.facebook.com/bugs/722275367815777

如果该错误得到解决,我将编辑答案。

于 2014-05-15T18:05:31.877 回答
0

多谢你们!

我的做法:

<?php
        session_start();

        require ({your_php_sdk_path} . 'autoload.php');

        use Facebook\FacebookCanvasLoginHelper;
        use Facebook\FacebookRedirectLoginHelper;
        use Facebook\FacebookSession;
        use Facebook\FacebookRequest;
        use Facebook\GraphUser;

        FacebookSession::setDefaultApplication({your_app_id},{your_app_secret});

        $helper = new FacebookCanvasLoginHelper();

        try {
            $session = $helper->getSession();

        }catch(FacebookRequestException $ex) {
            // When Facebook returns an error


        } catch(\Exception $ex) {
            // When validation fails or other local issues
        }
        if (!is_null($session)) {
            // Logged in

            try {

                //Get user name
                $user_profile = (new FacebookRequest(
                    $session, 'GET', '/me'
                ))->execute()->getGraphObject(GraphUser::className());

                $user_profile_name = $user_profile->getName();

                //Get user picture
                $request = new FacebookRequest(
                    $session,
                    'GET',
                    '/me/picture',
                    array (
                        'redirect' => false,
                        'height' => '135',
                        'width' => '135',
                    )
                );
                $response = $request->execute();
                $graphObject = $response->getGraphObject();

                $user_profile_picture = $graphObject->getProperty('url');

            } catch(FacebookRequestException $e) {
                 // When Facebook returns an error

            } catch(Exception $e) {
                // When validation fails or other local issues
            }
        }else{
            //First time -> ask for authorization
            $helper = new FacebookRedirectLoginHelper({your_canvas_url});
            $login_url = $helper->getLoginUrl();
        }     
?>

在你的 html 中放一个 javascript:

<script type="text/javascript">
    if($login_url != null){
        top.location.href = $login_url;
    }
</script>
于 2014-10-10T21:06:04.860 回答
-1
<?php

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('*********','*********'  );

$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(Exception $ex) {
// When validation fails or other local issues
}

if($session) {

// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
 // get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

}
else {
// show login url
echo '<a href="' . $helper->getLoginUrl($scope) . '">Login</a>';
}

?>
于 2014-05-09T16:30:25.640 回答