首页 文章

Facebook Graph Api - 作为管理员发布到粉丝页面

提问于
浏览
17

我设置了一个脚本,允许用户将消息发布到Facebook上的粉丝页面 . 这一切都有效,但有一个小问题 .

The Problem:

将帖子添加到页面Feed时,它会显示发布用户的个人帐户 . 我希望它能够显示页面的帐户(例如,当您管理页面时,它说它来自该页面) . 我发布的帐户拥有该页面的管理员权限,但它仍然显示为个人帖子 .

HTTP POST

$url = "https://graph.facebook.com/PAGE_ID/feed";
$fields = array (
    'message' => urlencode('Hello World'),
    'access_token' => urlencode($access_token)
);

$fields_string = "";
foreach ($fields as $key => $value):
    $fields_string .= $key . '=' . $value . '&';
endforeach;
rtrim($fields_string, '&');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);
curl_close($ch);

7 回答

  • 1

    据我所知,您所要做的就是在调用stream.publish时指定 uid (即页面的ID)

    编辑

    看看impersonation

  • 17

    要以页面而不是用户身份发布,您需要以下内容:
    Permissions:

    • publish_stream

    • manage_pages

    Requirements:

    • 页面ID和 access_token (可以获得,因为我们获得了上述所需的权限)

    • 当前用户是管理员(能够检索页面的 access_token

    • access_token 与其中一个管理员的long-lived expiration time如果要离线执行此操作(从后台脚本)

    PHP-SDK Example:

    <?php
    /**
     * Edit the Page ID you are targeting
     * And the message for your fans!
     */
    $page_id = 'PAGE_ID';
    $message = "I'm a Page!";
    
    
    /**
     * This code is just a snippet of the example.php script
     * from the PHP-SDK <http://github.com/facebook/php-sdk/blob/master/examples/example.php>
     */
    require '../src/facebook.php';
    
    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => 'app_id',
      'secret' => 'app_secret',
    ));
    
    // Get User ID
    $user = $facebook->getUser();
    
    if ($user) {
      try {
        $page_info = $facebook->api("/$page_id?fields=access_token");
        if( !empty($page_info['access_token']) ) {
            $args = array(
                'access_token'  => $page_info['access_token'],
                'message'       => $message 
            );
            $post_id = $facebook->api("/$page_id/feed","post",$args);
        } else {
            $permissions = $facebook->api("/me/permissions");
            if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
               !array_key_exists('manage_pages', $permissions['data'][0])) {
                    // We don't have one of the permissions
                    // Alert the admin or ask for the permission!
                    header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
            }
        }
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }
    
    // Login or logout url will be needed depending on current user state.
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
    }
    // ... rest of your code
    ?>
    

    这里连接的 $user 应该是管理员 .

    Result:

    enter image description here

    更多内容tutorial

  • 1

    因为这是谷歌搜索结果中唯一相关的帖子“facebook图表不会以页面形式发布到页面”我想记下我找到的解决方案 . 您需要具有manage_pages权限的访问令牌 . 然后打电话

    https://graph.facebook.com/<user_id>/accounts?access_token=<access_token>
    

    这将列出用户有权访问的所有页面,并为每个页面提供访问令牌 . 然后,您可以使用这些令牌作为页面发布 .

  • 1

    Graph API期望将参数 page_id (扇区页的对象ID)作为参数传递给API调用,以获取在Fanpage墙中发布的事件 . 在官方的Graph API文档中没有提到,但它可以工作 . 我已经使用官方PHP SDK v3.0.1成功测试了它

    所需的应用程序权限为 create_eventmanage_pages

    示例看起来像这样:

    //Facebook/Fan Page Id
    $page_id = '18020xxxxxxxxxx';
    
    //Event Start Time
    $next_month = time() + (30 * 24 * 60 * 60);
    
    //Event Paramaeters
    $params = array(
        'page_id'     =>  $page_id, // **IMPORTANT**
        'name'        => 'Test Event Name',
        'description' => 'This is the test event description. Check out the link for more info: http://yoursite.com',
        'location'    => 'Kottayam, Kerala, India',
        'start_time'  =>  $next_month       
    );
    
    
    $create_event = $facebook->api("/$page_id/events", "post", $params);
    
  • 1

    答案取决于在 FB:login 按钮上获得"manage_pages"的权限,如下所示:

    <fb:login-button perms="publish_stream,manage_pages" autologoutlink="true"></fb:login-button>`
    

    获得这些权限后,您可以获得登录用户为Admin的所有页面的结构化列表 . 要求的URL是:

    https://graph.facebook.com/me/accounts?access_token=YourAccessToken
    

    我讨厌Facebook文档,但这里有一个页面,上面有一些信息:https://developers.facebook.com/docs/reference/api/特别是在该页面上查看'Authorization'和'Page Login'部分 .

    把所有这些放在一起的很好的资源(对于Coldfusion开发者)是Jeff Gladnick关于RIA Forge的CFC:http://facebookgraph.riaforge.org/

    如果您愿意使用它,我将以下UDF添加到Jeff的CFC:

    <cffunction name="getPageLogins" access="public" output="true" returntype="any" hint="gets a user's associated pages they manage so they can log in as that page and post">
        <cfset var profile = "" />
        <cfhttp url="https://graph.facebook.com/me/accounts?access_token=#getAccessToken()#" result="accounts" />
        <cfif IsJSON(accounts.filecontent)>
            <cfreturn DeserializeJSON(accounts.filecontent) />
        <cfelse>
            <cfreturn 0/>
        </cfif>
    </cffunction>
    

    返回的是登录用户为Admin的所有页面的结构 . 它返回页面NAME,ID,ACCESS_TOKEN和CATEGORY(在此上下文中不需要) .

    所以, VERY IMPORTANT: 您传递的ID是设置您要发布到的页面,而ACCESS_TOKEN是您传递给谁设置POST的AS .

    获得页面列表后,可以解析数据以获得三元素数组:

    ID - ACCESS_TOKEN - NAME

    但要小心,因为Facebook ACCESS_TOKEN确实使用了一些奇怪的字符 . 如果您需要任何其他帮助,请与我们联系 .

  • 9

    您必须检索用户管理的页面和应用程序的access_tokens .

    可以通过Graph API调用 //accounts 来查询访问令牌 .

    更多细节:

    https://developers.facebook.com/docs/facebook-login/permissions/v2.0 - >参考 - >页面

  • 1

    这就是我使用PHP SDK 4.0和Graph API 2.3的方法:

    /**
     * Posts a message, link or link+message on the page feed as a page entity
     * 
     * @param FacebookSession $session (containing a page admin user access_token)
     * @param string $pageId
     * @param string $message - optional
     * @param string $link    - optional
     * 
     * @return GraphObject
     */
    function postPageAsPage( $session, $pageId, $message = '', $link = '' ){
    	
    	// get the page token to make the post request
    	$pageToken = ( new FacebookRequest( 
    			$session, 
    			'GET', 
    			"/$pageId" . "?fields=access_token"
    	))->execute()->getGraphObject();					
    	
    	return ( new FacebookRequest(
    			$session,
    			'POST',
    			"/$pageId/feed",
    			array(
    				'access_token' => $pageToken->getProperty( 'access_token' ),
    				'message'      => $message,
    				'link'         => $link,
    			)
    	))->execute()->getGraphObject();	
    }
    

相关问题