0

我试图从存储在我的数据库中的用户访问令牌和页面 ID 开始获取页面访问令牌。到目前为止,我还没有使用 facebook.php,而只是使用了 php 的 curl_* 函数。到目前为止,我可以将帖子发送到页面(使用硬编码的页面 id),但我想在这样做时模拟页面。

如果没有 facebook.php,我可以轻松地做到这一点,那会很好,因为它可以让我避免觉得我应该重写我到目前为止所做的事情。如果不是,那么我将如何从 facebook 对象获取页面访问令牌 - 请记住,至少到目前为止我没有将用户 ID 或页面 ID 存储在我的数据库中,只是用户访问令牌,当然还有我的应用 ID 和机密。

我一直在查看获取页面访问令牌的示例,但我发现它并不是我所需要的,因为它获取了一个用户对象,这样做似乎每次都强制用户登录到 facebook,但我存储了用户访问权限令牌以避免发生这种情况。

我需要比 manage_page 和 publish_stream 更多的权限吗?我尝试添加 offline_access 但它似乎不再可用(路线图提到了这一点)。

这是我最近尝试使用 facebook.php 文件的一些代码:

  // try using facebook.php
    require_once 'src/facebook.php';
    // Create our Application instance 
    $facebook = new Facebook(array(
      'appId'  => $FB_APP_ID,      // $FB_APP_ID      hardcoded earlier
      'secret' => $FB_APP_SECRET,  // $FB_APP_SECRET  hardcoded earlier
    ));

    $facebook->setAccessToken($FB_ACCESS_TOKEN );
        //got user access token  $FB_ACCESS_TOKEN from database
    // Get User ID -- why?
    $user = $facebook->getUser();

    //------ get PAGE access token
    $attachment_1 = array(
        'access_token' => $FB_ACCESS_TOKEN
    );

    $result = $facebook->api("/me/accounts", $attachment_1);
    foreach($result["data"] as $page) {
        if($page["id"] == $page_id) {// $page_id hardcoded earlier
            $page_access_token = $page["access_token"];

            break;
        }
    }
    echo '<br/>'.__FILE__.' '.__FUNCTION__.' '.__LINE__.' $result= ' ; 
    var_dump($result); //this prints: array(1) { ["data"]=> array(0) { } }  
    $facebook->setAccessToken($page_access_token );
    // Get User ID, why - re-init with new token maybe?
    $user = $facebook->getUser();



    //------ write to page wall
    try {
        $attachment = array(
                    'access_token' => $page_access_token,
                    'link'          => $postLink,
                    'message'=>  $postMessage
            );

        $result = $facebook->api('/me/feed','POST', $attachment);
        echo '<br/>'.__FILE__.' '.__FUNCTION__.' '.__LINE__.' $result= ' ; 
        var_dump($result);

    } catch(Exception $e) {
         echo '<br/>'.__FILE__.' '.__FUNCTION__.' '.__LINE__.' $e= ' ; 
         var_dump($e);   /*this gives : "An active access token must
                           be used to query information about the 
                           current user." */

    }
    die;

谢谢

PS:我硬编码了用户ID并开始调用

$result = $facebook->api("/$user_id/accounts", $attachment_1);

我仍然得到一个空的结果。

PPS:即使我的帐户设置为经理,Graph API Explorer 也不会显示我的粉丝页面。我尝试发布作品但显示为来自我的帐户而不是来自页面。

PPPS:通过在图形浏览器页面上添加权限来获取访问令牌,取得了一些进展,但这无济于事,因为我需要以编程方式获取访问令牌。当拥有许多粉丝页面的用户登录到我的网站时,我想向他们展示他们的 Facebook 粉丝页面列表以供选择。实际上,不是刚刚授予应用程序的权限吗?

PPPPS:我的应用程序的权限列表现在位于:email、user_about_me、publish_actions 和扩展权限:manage_pages、publish_stream、create_note、status_update、share_item 我还需要更多吗?当我现在尝试时,我仍然无法从以下电话中获得任何信息:

$facebook->api("/$user_id/accounts", $attachment_1);

Px5S:DOH!!!我现在看到,当我的脚本第一次获取用户访问令牌并将其存储在数据库中时,我忽略了将 manage_pages 权限添加到我的用户访问令牌调用中。但是当我重用那个新的访问令牌时,我仍然得到错误:“必须使用活动的访问令牌来查询有关当前用户的信息。” 那么,不能重复使用这样的令牌吗?他们不是长期的吗?会读更多的东西...

4

1 回答 1

2

这是我的功能代码,仍然很乱,但似乎可以工作,请注意第一个 $dialog_url 上的范围,请随意模拟我的代码,甚至提出改进建议:

function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription=''){

global $FB_APP_ID, $FB_APP_SECRET; 

$APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?returnurl=1';

$code = $_REQUEST["code"];

$FB_ACCESS_TOKEN = getFaceBookAccessToken( );
$FB_ACCESS_TOKEN_OLD = $FB_ACCESS_TOKEN;


//if no code ot facebook access token get one
if( empty($code) && empty($FB_ACCESS_TOKEN) && $_REQUEST["returnurl"] != '1')  
{      
     // if(  $_REQUEST["returnurl"] == '1') die;
     $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream,manage_pages";                  
     header("Location:$dialog_url");
}


if( empty($FB_ACCESS_TOKEN) ){ 

    if($_REQUEST['error_code'] == '200'){
        return null;
     }else if (!empty($code)){        
        $token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code;   
        $access_token = file_get_contents($token_url);  
        $param1=explode("&",$access_token);
        $param2=explode("=",$param1[0]);
        $FB_ACCESS_TOKEN=$param2[1];    
    }else{
        return null;
    }
}
if(!empty($FB_ACCESS_TOKEN) && $FB_ACCESS_TOKEN_OLD != $FB_ACCESS_TOKEN)  {
    setFaceBookAccessToken( $FB_ACCESS_TOKEN);
}

$_SESSION['FB_ACCESS_TOKEN'] = $FB_ACCESS_TOKEN;

$page_name = '';
$page_id =   getFaceBookPageId(); //from db

if(empty($page_id ) ) return null;
//in case there are multiple page_ids separated by commas  
if(stripos($page_id, ',') !== false ){
   $page_ids = explode(',', $page_id)   ;// = substr($page_id, 0, stripos($page_id, ','));
}
$result = null;
foreach($page_ids as $page_id){
    $page_id = trim($page_id);
if( !empty($FB_ACCESS_TOKEN)){ 
    //get page_id
    require_once 'src/facebook.php';
    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => $FB_APP_ID,
      'secret' => $FB_APP_SECRET 
    ));  

    $facebook->setAccessToken($FB_ACCESS_TOKEN ); 

    //------ get PAGE access token
    $page_access_token ='';
    $attachment_1 = array(
        'access_token' => $FB_ACCESS_TOKEN
    );

    $result = $facebook->api("/me/accounts", $attachment_1);
    if(count($result["data"])==0)  {
         return null;
    }

    foreach($result["data"] as $page) {
        if($page["id"] == $page_id) {
            $page_access_token = $page["access_token"];
            break;
        }
    }

    //------ write to page wall
    try {
        $attachment = array(
                    'access_token' => $page_access_token,
                    'link'          => $postLink,
                    'message'=>  $postMessage 
        );

        $result = $facebook->api('/me/feed','POST', $attachment);

    } catch(Exception $e) {
         return null;
    }
    } //end if( !empty($FB_ACCESS_TOKEN)) 

}//end foreach
return $result; }

现在,我想知道我是否可以一次将相同的消息发送到多个页面......是的,只需遍历 id,见上文,它现在支持多个页面 id。除非有人想为代码做出贡献——有很多方法可以改进——我已经完成了。

于 2013-06-18T14:55:23.203 回答