将Facebook php sdk整合到Facebook Canvas应用程序中,提供空白页面或oauthData错误 [英] Integrating facebook php sdk in Facebook Canvas App giving Blank page or oauthData error

查看:170
本文介绍了将Facebook php sdk整合到Facebook Canvas应用程序中,提供空白页面或oauthData错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的画布应用程序整合Facebook。当我从Facebook上运行以下代码的应用程序

I am trying to integrate facebook for my canvas app. When i run app from facebook with following code

// 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;。

The entire page just turns blank white because of $_GET['signed_request'] = $signedRequest;.

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

What should I do to get login. If i just do $session = $helper->getSession(); instead of Get i get invalid signed paramters oAuth data missing.

推荐答案

您的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中的某个地方:

And then somewhere in your HTML:

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

如果要要求额外权限,请在URL中添加范围参数,如下所示:

If you want to ask for extra permission, add the scope parameter in the URL like this:

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

这将重定向页面到登录页面,然后回到你的画布应用程序与适当的许可。

That will redirect the page to the login page, and then come back to your canvas app with the proper permission.

这不应该像这样使用Javascript与PHP SDK。这是一个正在由Facebook解决的错误,您可以在此处查看:

This shouldn't work like this as it's using Javascript with the PHP SDK. It's a bug that is being addressed by Facebook which you can follow here:

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

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

I'll edit the answer if that bug ever gets resolved.

这篇关于将Facebook php sdk整合到Facebook Canvas应用程序中,提供空白页面或oauthData错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆