是否可以使用ajax从canvas教程中检索facebook应用程序授权代码?

发布于 2024-12-19 13:11:33 字数 1080 浏览 0 评论 0原文

这是画布教程中的代码 http://developers.facebook.com/docs/appsonfacebook/tutorial/

这得到授权代码,将用户重定向回画布页面并在 url 中显示代码。

类似于 http://apps.facebook.com/yourapp/?code=1234thecode5678

那么,是否可以使用ajax 来实现并获取代码而无需重定向?而不是这个方法?

提前致谢

     $app_id = "YOUR_APP_ID";

     $canvas_page = "YOUR_CANVAS_PAGE_URL";

     $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page);

     $signed_request = $_REQUEST["signed_request"];

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {
            echo ("Welcome User: " . $data["user_id"]);
     } 
 ?>

This is the code from the canvas tutorial
http://developers.facebook.com/docs/appsonfacebook/tutorial/

This gets the authorization code, redirects the user back to the canvas page and displays the code in the url.

Something like, http://apps.facebook.com/yourapp/?code=1234thecode5678

So, is it possible to do it using ajax and get the code without redirecting? Instead of this method?

Thanks in advance

     $app_id = "YOUR_APP_ID";

     $canvas_page = "YOUR_CANVAS_PAGE_URL";

     $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page);

     $signed_request = $_REQUEST["signed_request"];

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {
            echo ("Welcome User: " . $data["user_id"]);
     } 
 ?>

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

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

发布评论

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

评论(1

不念旧人 2024-12-26 13:11:33

您可以使用 Facebook JS SDK 让用户登录,而无需刷新或重定向用户。
摘自 Facebook JS SDK 文档 主题下的 FB.login

FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
       FB.logout(function(response) {
         console.log('Logged out.');
       });
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'email'});

注意:如果您使用的是 oAuth 2.0,这是实现(你确实应该如此)。取自同一网址:

如果没有 OAuth 2.0,范围应替换为权限,并且
response.authResponse 替换为response.session。 记住这一点
所有应用程序必须在 2011 年 10 月 1 日之前迁移到 OAuth 2.0。

You can use the Facebook JS SDK to log the user in with out having to refresh or redirect the user.
Taken from the Facebook JS SDK Documentation under the topic FB.login

FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
       FB.logout(function(response) {
         console.log('Logged out.');
       });
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'email'});

Note : This is the implementation if you are using oAuth 2.0 (and you really should be). Taken from the same url :

Without OAuth 2.0, scope should instead be replaced with perms, and
response.authResponse replaced with response.session. Remember that
all apps must migrate to OAuth 2.0 by October 1, 2011.

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