Facebook Canvas iFrame 应用程序 - 使用新的 OAuth 协议授权用户

发布于 2024-08-30 23:33:45 字数 528 浏览 2 评论 0原文

我正在 iFrame 中开发一个新的 Facebook Canvas 应用程序并尝试向用户授权。新的 OAuth api 建议我重定向到以下内容以在我的应用程序中授权用户:

https:// graph.facebook.com/oauth/authorize? client_id=...& redirect_uri=http://www.example.com/oauth_redirect

然而,这会产生一个奇怪的问题,其中一个完整的向用户请求权限的 Facebook 页面在 iFrame 本身内呈现(即 Facebook 内的 facebook)。有谁知道如何使用新的 OAuth API 解决这个问题,因为我不想开始使用旧的 REST API 方法。

I'm developing a new Facebook Canvas application within an iFrame and trying to authorize users. The new OAuth api recommends I do a redirect to the following to authorize a user in my app:

https://graph.facebook.com/oauth/authorize?
client_id=...&
redirect_uri=http://www.example.com/oauth_redirect

However this produces a weird problem where a full Facebook page requesting permissions from the user is rendered within the iFrame itself (i.e. facebook within Facebook). Does anyone know how to solve this with the new OAuth API as I don't want to start using old REST API methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽清梦 2024-09-06 23:33:45

即使我也遇到了同样的问题,并将其发布在 Facebook 论坛上。版主告诉我,这是一个目前还没有解决方案的问题。看看这个帖子 - http://forum.developers.facebook.com /viewtopic.php?id=56590

Even I had the same issue and I posted it in facebook forum. The moderator informed me that it is an issue for which there is no solution as of now. Take a look at this thread - http://forum.developers.facebook.com/viewtopic.php?id=56590

在巴黎塔顶看东京樱花 2024-09-06 23:33:45

相反,我找到了解决这个问题的方法,我在我的博客文章 此处。一探究竟。

On the contrary, I have found a solution to this problem that I have outlined in my blog post here. Check it out.

暮倦 2024-09-06 23:33:45

还有另一种方法可以使用 oAuth v2 来完成此操作,这在 facebook 文档中进行了描述,但分为几个页面,因此不容易理解。

首先,您需要在应用程序的“高级参数”中激活“OAuth 2.0 for Canvas”标志。

然后,这是一个 PHP 示例,解释如何处理它:

function parse_signed_request($signed_request, $secret) {
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

    // decode the data
    $sig = base64_url_decode($encoded_sig);
    $data = json_decode(base64_url_decode($payload), true);

    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
            error_log('Unknown algorithm. Expected HMAC-SHA256');
            return null;
    }

    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
            error_log('Bad Signed JSON signature!');
            return null;
    }

    return $data;
}

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));
}

$data = parse_signed_request($_REQUEST["signed_request"], <your facebook app api secret>);

if (empty($data["user_id"]) && !isset($_REQUEST['redir'])) {
    // The user isn't authenticated
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" .
            <your facebook app id> . "&redirect_uri=" .
            urlencode('http://apps.facebook.com/<yourapp>/?redir=1');
    echo("<script> top.location.href='" . $auth_url . "'</script>");
    die;
}
// Here the user is authenticated
echo ("Welcome User: " . $data["user_id"]);
// And now you have the Graph API auth token in $data["oauth_token"],
// so you can use any graph api method

there is an other way to do it still with oAuth v2, and this is described in the facebook docs, but splitted in several pages, so not easy to understand.

First, you need to activate the "OAuth 2.0 for Canvas" flag ine the "advanced parameters" of you app.

And then, here is a PHP example explaining how to handle it :

function parse_signed_request($signed_request, $secret) {
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

    // decode the data
    $sig = base64_url_decode($encoded_sig);
    $data = json_decode(base64_url_decode($payload), true);

    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
            error_log('Unknown algorithm. Expected HMAC-SHA256');
            return null;
    }

    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
            error_log('Bad Signed JSON signature!');
            return null;
    }

    return $data;
}

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));
}

$data = parse_signed_request($_REQUEST["signed_request"], <your facebook app api secret>);

if (empty($data["user_id"]) && !isset($_REQUEST['redir'])) {
    // The user isn't authenticated
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" .
            <your facebook app id> . "&redirect_uri=" .
            urlencode('http://apps.facebook.com/<yourapp>/?redir=1');
    echo("<script> top.location.href='" . $auth_url . "'</script>");
    die;
}
// Here the user is authenticated
echo ("Welcome User: " . $data["user_id"]);
// And now you have the Graph API auth token in $data["oauth_token"],
// so you can use any graph api method
雨轻弹 2024-09-06 23:33:45

试试这篇文章 http://novacoders.blogspot.com/ 2011/04/facebook-apps-oauth-20-authorization.html

如果您不使用任何网络服务器,则需要使用 Javascript SDK。 FB.init() 返回所有必需的数据,例如 access_token。

Try this article http://novacoders.blogspot.com/2011/04/facebook-apps-oauth-20-authorization.html

If you don't use any web server you need to use Javascript SDK. FB.init() returns all necessary data like access_token.

倾`听者〃 2024-09-06 23:33:45

过去两天一直在努力解决这个问题,并在 Facebook 开发者论坛

Have been struggle with this for the past two days and found a hack to this problem on the Facebook developers forum.

梦一生花开无言 2024-09-06 23:33:45

您无法在画布 iframe 内执行简单的重定向 302 或 301,因为这只会重定向 iframe 内的内容。 Facebook 建议发送一小段 JavaScript,将 top.location 设置为对话/oauth 页面。

<script>top.location='https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=publish_actions';</script>.

clientid 是您的 AppId,redirect_uri 是处理来自身份验证对话框页面的重定向的页面。

You cannot do a simple redirect 302 or 301 within the canvas iframe as this will only redirect the content within the iframe. What Facebook recommends is to send a small bit of JavaScript that will set the top.location to the dialog/oauth page.

<script>top.location='https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=publish_actions';</script>.

clientid being your AppId and redirect_uri being the page which handles the redirect from the auth dialog page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文