如何在画布中使用重定向来授权 Facebook 应用程序? [英] How to authorize Facebook app using redirect in canvas?

查看:31
本文介绍了如何在画布中使用重定向来授权 Facebook 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作 Facebook 应用,但我无法在画布内的重定向方案中获得授权.

I'm trying to get into making Facebook apps but I'm having trouble getting authorization working in a redirect scheme inside the canvas.

使用 javascript api,我让它在弹出方案中很容易地工作:

Using the javascript api, I got it working pretty easily in a popup scheme:

$("#loginButton").click(function(e) {
    FB.login(function(response) {
        if (response.perms) {
            perms();
        }
}, {perms : 'publish_stream'});

但是弹出窗口应该是不必要的额外点击,因为我看到的每个其他应用程序甚至在向您显示登录页面之前都会请求授权.像这样:

But the popup should be an unnecessary extra click, because every other application I see requests the authorization before even showing you the landing page. Like this:

http://i.imgur.com/yBGzL.png

我认为他们只是在使用重定向方案.所以我花了一整天的时间尝试我能找到的不同方法:

I figure they're simply using a redirect scheme. So I spent the entire day trying the different ways I could find:

header("Location: https://graph.facebook.com/oauth/authorize?client_id=" . $gAppId . "&redirect_uri=" . urlencode($gUrl) . "&perms=publish_stream");

header("Location: http://www.facebook.com/login.php?v=1.0&api_key=" . $gApiKey . "&next=" . urlencode($gUrl) . "&canvas=");

header("Location: http://www.facebook.com/connect/uiserver.php?app_id=" . $gAppId . "&next=" . urlencode($gUrl) . "&return_session=0&fbconnect=0&canvas=1&legacy_return=1&method=permissions.request");

但所有这些都没有显示授权请求内容,而是显示如下链接:

But all of them, instead of showing the authorization request stuff, show a link like this:

http://i.imgur.com/bLwPQ.png

有趣的是,如果我在新选项卡中打开 iframe 的地址,就会收到我想要的授权请求.但我希望它立即显示,无需额外点击,就像其他所有应用程序一样.

Hilariously, if I open the iframe's address in a new tab, I get the authorization request like I wanted. But I want it to display immediately, without an extra click, like every other app.

以下是一个应用示例,它在显示着陆页之前以重定向形式一次性完成授权和请求权限:

Here's an example of an app that is doing authorization and requesting permissions all in one go, in redirect form, before even displaying the landing page:

www.facebook.com/send.fortune.cookies

www.facebook.com/send.fortune.cookies

他们是怎么做的?

推荐答案

我知道这已经有几个月的历史了...但这是您应该做的来向画布添加权限检查.

I know that this is months old now... but this is what you should do to add permission checking to your canvas.

if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
    $accesstoken=$session['access_token'];
  } catch (FacebookApiException $e) {
    error_log($e);
  }
} 

if($me)
{
   // do what you have to do
}else {
    $loginUrl = $facebook->getLoginUrl(
        array(
            'canvas' => 1,
            'fbconnect' => 0,
            'req_perms' => 'publish_stream'
        )
    );
    echo '<script>top.location="'.$loginUrl.'";</script>';
    //echo '<fb:redirect url="' . $loginUrl . '" />';
   //header('Location: '.$loginUrl);
}

这篇关于如何在画布中使用重定向来授权 Facebook 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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