验证facebook画布应用程序返回?code = [英] authenticating facebook canvas application returns ?code=

查看:189
本文介绍了验证facebook画布应用程序返回?code =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的Facebook画布应用程序,目前正在开发我在 http:// localhost:8080 < a>

i have a problem with my facebook canvas app that is currently on development i'm working on http://localhost:8080

我的画布网址是 http:// localhost:8080 / fbcanvas /

在Facebook上的网址设置为 http://apps.facebook.com/app_name/

on facebook the url is set to http://apps.facebook.com/app_name/

问题是,在用户批准我的应用程序后,我得到一个代码作为 $ _ GET ['code'] 变量。
在facebook文档它没有说任何关于获得 $ _ GET ['code'] 它只是说获得 signed_request

the problem is i'm getting a code as an $_GET['code'] variable after a user approves my app. in facebook documentation it doesnt say anything about getting a $_GET['code'] it just says getting signed_request

这是我从facebook使用的代码示例。

this is the code i'm using from facebook examples.

    require_once($_SERVER['DOCUMENT_ROOT'] . '/classes/Page.php');
require($_SERVER['DOCUMENT_ROOT'] . '/core/config.fb.php');

$canvas_page = 'http://apps.facebook.com/khawamusic/';
$auth_url = 'https://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 {

    $page = new Page;

    $styles = array('reset.css', 'fbcanvas.css');
    $scripts = array(
        'https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', 
        'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js',
        'http://connect.facebook.net/en_US/all.js#xfbml=1',
        '/sources/js/fbcanvas.js'
    );


    $page->set_title('Khawa');
    $page->set_styles($styles);
    $page->set_scripts($scripts);
    $page->start_page();
    require($_SERVER['DOCUMENT_ROOT'] . '/fbcanvas/fb.tpl');
    $page->end_page();

}

所以会发生什么是用户批准我的应用程序,重定向到 http://apps.facebook.com/khawamusic/?code=blabla

so what happens is a user approves my app then he gets redirected to http://apps.facebook.com/khawamusic/?code=blabla

我被cnfused因为在文档中它不说我想假设得到一个 $ _ GET ['code']

i'm cnfused because in the documentation it doesn't say i'm suppose to get a $_GET['code']

推荐答案

如果用户按允许,您的应用程序将被授权。 OAuth对话框将使用授权码将用户的浏览器重定向到(通过HTTP 302)redirect_uri参数中传递的网址:

If the user presses Allow, your app is authorized. The OAuth Dialog will redirect (via HTTP 302) the user's browser to the URL you passed in the redirect_uri parameter with an authorization code:

http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER

使用此代码,下一步,应用程序身份验证,以获取您需要进行API调用的访问令牌。

With this code in hand, you can proceed to the next step, app authentication, to gain the access token you need to make API calls.

EDIT:
下面是一个认证示例,这将不会显示?code = Blabla ..
首先从这里下载最新的Facebook PHP SDK:
https://github.com/facebook/php-sdk/tree/master/src

确保保存所有3个文件,facebook.php,base_facebook.php和fb_ca_chain_bundle.crt
现在用您的应用程序ID和应用程序密钥替换文本YOUR_APP_ID和YOUR_APP_API_SECRET从Facebook,我已经添加了样本墙张贴使用图形API,如果你不想要,你可以删除它,如果你通过我的代码和评论,你会明白它做什么,你不想做任何获取访问令牌,只需使用$ access_token变量,它会给你当前用户的access_token,如果你想要用户的ID,然后使用$ user变量,如果你想要用户的基本信息,使用$ userInfo变量,它'使用graph api获取用户的数据并返回数组中的所有信息,您将获得id,name,first_name,last_name,link,hometown,location,bio,work,education,gender,timezone.etc等当前用户的信息。

Make sure you save all 3 files, facebook.php, base_facebook.php and fb_ca_chain_bundle.crt Now replace the text "YOUR_APP_ID" and ""YOUR_APP_API_SECRET" with your Application ID and App Secret from facebook, I've added sample wall posting using graph api, if you don't want, you can remove it, if you go through my codes and comments, you'll understand what it does and you don't want to do anything to get access token, just use $access_token variable and it'll give you the access_token of the current user and if you want the user's ID then use $user variable, if you want user's basic information, use $userInfo variable and it'll fetch user's data using graph api and returns all information in an array, you'll get the current user's info like id,name,first_name,last_name,link,hometown,location,bio,work,education,gender,timezone.etc.

使用您的着陆页网址或画布页网址更改$ RedirectUrl

Change $RedirectUrl with your landing page URL or your canvas page url

 <?php
        require 'facebook.php';

        define('FACEBOOK_APP_ID', "YOUR_APP_ID"); // Your App ID
        define('FACEBOOK_SECRET', "YOUR_APP_API_SECRET"); // Your App API Secret

        $RedirectUrl = "http://apps.facebook.com/myapp/"; // Your Landing Page URL, User's will be redirect to this URL after they allow your app.

        function d($d){
                echo "<pre>";
                print_r($d);
                echo "</pre>";
        }

        $user  = null;

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

        $user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.  

        if(isset($_GET['code'])){
            header("Location: $RedirectUrl");
        }

        if($user == 0) {
            // If User is not connected to your app, then redirect User to Authentication Page.
         /**
          * Get a Login URL for use with redirects. By default, full page redirect is
          * assumed. If you are using the generated URL with a window.open() call in
          * JavaScript, you can pass in display=popup as part of the $params.
          * 
          * The parameters:
          * - redirect_uri: the url to go to after a successful login
          * - scope: comma separated list of requested extended perms
          */
          $login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream", 'redirect_uri' => $RedirectUrl));
          echo("<script> top.location.href='" . $login_url . "'</script>");
        } else {
            // If User is connected to your app, then do something.
            $signed_request = $facebook->getSignedRequest(); // Get the data from a signed_request token.

            $access_token = $facebook->getAccessToken(); // Determines the access token that should be used for API calls.

            $userInfo = $facebook->api("/me"); // Get's User Info

            try {
                // Posts to user's wall after the user allows your app.
                $wallpost = array(
                            'message' => "I like this",
                            'link'    => 'http://google.com',
                            'picture' => 'http://i.imgur.com/8iz6L.png',
                            'name'    => 'This is cool',
                            'description'=> 'Checkout this cool app'
                );
                $publishStream = $facebook->api("/$user/feed", "post", $wallpost); // WallPost to User's Wall using Graph API
echo "Your post was successfully posted to UID: $user";
            }
            catch (FacebookApiException $e) {
                d($e);
            } 

        }
        ?>

这篇关于验证facebook画布应用程序返回?code =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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