javascript Facebook - 如何使用 iframe 应用程序直接进入请求权限对话框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4382017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:14:03  来源:igfitidea点击:

Facebook - how to go directly to the request permission dialog with an iframe app?

phpjavascriptfacebookpermissionslogin

提问by Scott Szretter

When a user accesses my facebook app via http://apps.facebook.com/myappxxxxx, I want it to immediately show the request permissions fb dialog - like so many others do.

当用户通过http://apps.facebook.com/myappxxxxx访问我的 facebook 应用程序时,我希望它立即显示请求权限 fb 对话框 - 就像许多其他人一样。

I have tried all sorts of things - including php redirect using the getLoginUrl, javascript window location redirect using the same.

我已经尝试了各种各样的事情 - 包括使用 getLoginUrl 的 php 重定向,使用相同的 javascript 窗口位置重定向。

The problem is that it shows a big facebook icon instead of the request permissions. However, if you click on that icon, it in fact goes to the correct request permissions page!

问题是它显示了一个大的 Facebook 图标而不是请求权限。但是,如果您单击该图标,它实际上会转到正确的请求权限页面!

Does anyone know how to redirect to the facebook permissions page properly?

有谁知道如何正确重定向到 Facebook 权限页面?

I am using the PHP api and JavaScript SDK with an iFrame FB app.

我正在将 PHP api 和 JavaScript SDK 与 iFrame FB 应用程序一起使用。

采纳答案by demux

I had the exact same problem a while ago. Here's a little hack:

不久前我遇到了完全相同的问题。这是一个小技巧:

if(!$me) {  
    $url =   "https://graph.facebook.com/oauth/authorize?"
                ."client_id=YOUR_APP_ID&"
                ."redirect_uri=http://apps.facebook.com/APP_SLUG/&"
                ."scope=user_photos,publish_stream";
    echo "<script language=javascript>window.open('$url', '_parent', '')</script>";
} else {
    your code...
}

How it looks in my code (a bit dirty, I had a deadline):

它在我的代码中的样子(有点脏,我有一个截止日期):

require_once 'facebook-php-sdk/src/facebook.php';

$appid = '';

$facebook = new Facebook(array(
  'appId'  => $appid,
  'secret' => '',
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}
if(!$me) {
    // here we redirect for authentication:
    // This is the code you should be looking at:
    $url =   "https://graph.facebook.com/oauth/authorize?"
            ."client_id=$appid&"
            ."redirect_uri=http://apps.facebook.com/APP_SLUG/&"
            ."scope=user_photos,publish_stream";
    ?>
    <!doctype html>
    <html xmlns:fb="http://www.facebook.com/2008/fbml">
        <head>
            <title>G?ngum til góes</title>
            <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
            <script language=javascript>window.open('<?php echo $url ?>', '_parent', '')</script>
        </head>

        <body>
            Loading...
        </body>
    </html>

    <?php
    exit;
} else {
    your code ...
}

A simple header redirect won't work since it will only redirect your iframe. Javascript is needed to access the _parent window.

简单的标头重定向将不起作用,因为它只会重定向您的 iframe。访问 _parent 窗口需要 Javascript。

Edit

编辑

Keep in mind that there is a proper way to do this (see answer below) and I do not recommend doing this. But hey, what ever floats your boat...

请记住,有一种正确的方法可以做到这一点(请参阅下面的答案),我不建议这样做。但是,嘿,是什么让你的船漂浮...

回答by Omar Al-Ithawi

Why don't you try the Facebook authenticated referrals? It's the proper solution for such problem. Since it asks Facebook to traps the non-connected users and redirect them directly to the connect page.

你为什么不试试Facebook 认证的推荐?这是解决此类问题的正确方法。因为它要求 Facebook 捕获未连接的用户并将他们直接重定向到连接页面。

This insure that all of the users are connected to your app by default.

这确保默认情况下所有用户都连接到您的应用程序。

回答by Johnny Oshika

Here's another option:

这是另一种选择:

<script>
    (function () {
        var url = 'your_permission_url';
        window.top.location.href = url;
    } ());
</script>

回答by MeetM

The url should be from getLoginUrl($PARAMS). It worked for me. In your case,

url 应该来自 getLoginUrl($PARAMS)。它对我有用。在你的情况下,

$PARAMS = array('scope' => 'user_photos,publish_stream', redirect_uri => 'https://apps.facebook.com/myappxxxxx');

$PARAMS = array('scope' => 'user_photos,publish_stream', redirect_uri => 'https://apps.facebook.com/myappxxxxx');

回答by Preethi

When I change the url as below, it worked !!!!!

当我更改如下网址时,它起作用了!!!!

if(!$me) {  
    $url =   "https://www.facebook.com/dialog/oauth?"
                ."client_id=YOUR_APP_ID&"
                ."redirect_uri=http://apps.facebook.com/APP_SLUG/&"
                ."scope=user_photos,publish_stream";
    echo "<script language=javascript>window.open('$url', '_parent', '')</script>";
} else {
    your code...
}

Thanks for the post

谢谢你的帖子