facebook页面访问令牌使用PHP?

时间:2014-07-10 10:10:27

标签: php facebook facebook-graph-api facebook-php-sdk

我想知道如何使用php获得page access token。我做的每件事都不会返回访问令牌。

第一个代码如下,工作并以用户身份发布消息。但我需要发布一个页面,根据文档,我需要一个页面访问令牌才能发布为页面/应用程序。

所以我将我的工作代码(第一个代码)编辑到第二个代码(返回一个空白页面)。

有人可以告诉我丢失的内容或我做错了吗?

以用户身份发布的工作代码:

<?php
 include_once 'inc/facebook.php';

 $appId = '00000000000000000';
 $secret = '00000000000000000000000000000';
 $returnurl = 'https://mydomain.com';
 $permissions = 'manage_pages, publish_actions';

 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));

 $fbuser = $fb->getUser();



 if($fbuser){

    if(isset($_POST['msg']) and $_POST['msg']!=''){
        try{


            $message = array(
                'message' => $_POST['msg']
            );
            $posturl = '/'.$_POST['pageid'].'/feed';
            $result = $fb->api($posturl,'POST',$message);
            if($result){
                echo 'Successfully posted to Facebook Wall...';
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }
    }

    try{
        $qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
        $pages = $fb->api(array('method' => 'fql.query','query' => $qry));

        if(empty($pages)){
            echo 'The user does not have any pages.';
        }else{
            echo '<form action="" method="post">';
            echo 'Select Page: <select name="pageid">';
            foreach($pages as $page){
                echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
            }
            echo '</select>';
            echo '<br />Message: <textarea name="msg"></textarea>';
            echo '<br /><input type="submit" value="Post to wall" />';
            echo '</form>';
        }

    }catch(FacebookApiException $e){
        echo $e->getMessage();
    }

 }else{
    $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
    echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
 }



 echo $page_info['access_token']
?> 

第二个/已编辑的代码,当从浏览器查看时返回空白页:

<?php
 include_once 'inc/facebook.php';

 $appId = '0000000000000';
 $secret = '000000000000000000000000000';
 $returnurl = 'https://mydomain.com';
 $permissions = 'manage_pages, publish_actions';

 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));

 $fbuser = $fb->getUser();

 if($fbuser){

    if(isset($_POST['msg']) and $_POST['msg']!=''){
        try{

            $page_info = $facebook->api("/$pageId?fields=access_token");
            if (!empty($page_info['access_token'])) {


            $message = array(
            'access_token' => $page_info['access_token'],
                'message' => $_POST['msg']
            );
            $posturl = '/'.$_POST['pageid'].'/feed';
            $result = $fb->api($posturl,'POST',$message);
            if($result){
                echo 'Successfully posted to Facebook Wall...';
            }
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }
    }


    try{
        $qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
        $pages = $fb->api(array('method' => 'fql.query','query' => $qry));

        if(empty($pages)){
            echo 'The user does not have any pages.';
        }else{
            echo '<form action="" method="post">';
            echo 'Select Page: <select name="pageid">';
            foreach($pages as $page){
                echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
            }
            echo '</select>';
            echo '<br />Message: <textarea name="msg"></textarea>';
            echo '<br /><input type="submit" value="Post to wall" />';
            echo '</form>';
        }


    }catch(FacebookApiException $e){
        echo $e->getMessage();
    }

 }else{
    $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
    echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
 }

?> 

编辑,这将生成访问令牌,但我不确定它是否是页面访问令牌,或者它是用户访问令牌,因为所有页面的访问令牌都相同: < / p>

 include_once 'inc/facebook.php';

 $appId = '000000000000000000';
 $secret = '000000000000000000000000000000';
 $returnurl = 'https://mydomain.com';
 $permissions = 'manage_pages, publish_actions';

 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));

 $fbuser = $fb->getUser();

 if($fbuser){

    if(isset($_POST['msg']) and $_POST['msg']!=''){
        try{


            $message = array(
                'message' => $_POST['msg']
            );
            $posturl = '/'.$_POST['pageid'].'/feed';


            //$posturl2 = '/'.$_POST['pageid'].'/tabs';

            //$result2 = $fb->api($posturl2,"post", array("access_token" => $pageAccessToken,  "app_id" => $appId));


            $result = $fb->api($posturl,'POST',$message);
            if($result){
                echo 'Successfully posted to Facebook Wall...';
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }
    }

    try{
        $qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
        $pages = $fb->api(array('method' => 'fql.query','query' => $qry));


        $pageIds=$fb->api('/me/accounts');
        $pageAccessToken=$pageIds["data"][1]["access_token"];




        if(empty($pages)){
            echo 'The user does not have any pages.';
        }else{
            echo '<form action="" method="post">';
            echo 'Select Page: <select name="pageid">';
            foreach($pages as $page){
                echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
            }
            echo '</select>';


                        echo 'Select Page: <select name="pageid">';
            foreach($pageIds as $pageId){
                echo '<option value="'.$page['page_id'].'">'.$pageAccessToken.'</option>';
            }
            echo '</select>';


            echo '<br />Message: <textarea name="msg"></textarea>';
            echo '<br /><input type="submit" value="Post to wall" />';
            echo '</form>';
        }

    }catch(FacebookApiException $e){
        echo $e->getMessage();
    }

 }else{
    $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
    echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
 }



 echo $pageAccessToken;

2 个答案:

答案 0 :(得分:1)

我不太可能通过你的一堆代码。但你想要的很简单。只需按照以下步骤操作即可 -

(我会在用户成功授予manage_pagespublish_stream权限后开始

  1. 获取page access token -

    \GET /{page-id}?fields=access_token
    
  2. 使用页面访问令牌在页面上发布供稿 -

    \POST /{page-id}/feed?fields=page_access_token //send any parameters with this
    
  3. 那就是它!

答案 1 :(得分:0)

这些访问令牌与用户访问令牌类似,不同之处在于它们为读取,写入或修改属于Facebook页面的数据的API提供权限。要获取页面访问令牌,您需要先获取用户访问令牌并询问manage_pages权限。获得用户访问令牌后,您将通过Graph API获取页面访问令牌。  授予此权限后,您可以使用以下Graph API请求检索页面访问令牌:

  

获取/ {user-id} / accounts

阅读本页 [页面访问令牌] [1]

[1] https://developers.facebook.com/docs/facebook-login/access-tokens