且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

验证facebook画布应用程序返回?code =

更新时间:2022-12-21 19:04:46

如果用户按允许,您的应用程序将被授权。 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);
            } 

        }
        ?>