2011-03-16 126 views
12

是否可以作爲應用程序發佈在Facebook牆上而不是用戶?據我瞭解,應用程序不是Facebook的有效用戶,但有許多關於發佈爲頁面而不是用戶或應用程序的評論。作爲頁面發佈在Facebook牆上,而不是用戶

我該如何使用PHP Facebook API來做到這一點?

回答

4

您必須先通過Facebook連接授予用戶授權,並請求manage_pages權限作爲此請求的一部分。您可以使用此權限爲相關頁面獲取令牌和祕密。然後,您使用這些來授權請求​​,而不是您爲用戶收到的令牌/祕密。

看看這個頁面的一些額外信息「頁面登錄」部分: http://developers.facebook.com/docs/authentication/

+0

是必要的消息被簽署爲屬於該應用程序(或任何Facebook頁面),而比Facebook用戶。想象一下以下情況: 1.應用程序的用戶(不是Facebook的!)在其中執行一些操作; 2.應用程序添加一條消息,瞭解剛剛發生在自己的Facebook牆或頁面上的內容。 嚴格來說,應用程序的用戶不需要在Facebook上註冊。只有Facebook中的應用程序纔有必要在應用程序的牆上發佈消息,並且可以在應用程序上發佈作者。 – WASD42 2011-03-16 14:48:44

10

您需要publish_stream,manage_pages權限。該代碼是這樣的:

<?php 
// This code is just a snippet of the example.php script 
// from the PHP-SDK <https://github.com/facebook/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_id = 'page_id'; 
    $page_info = $facebook->api("/$page_id?fields=access_token"); 
    if(!empty($page_info['access_token'])) { 
     $args = array(
      'access_token' => $page_info['access_token'], 
      'message'  => "I'm a Page!" 
     ); 
     $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 
?> 

我已經寫和深入的教程這一點:How To: Post On Facebook Page As Page Not As Admin User Using PHP-SDK

+0

這仍然需要用戶登錄到Facebook,對吧?我收到「必須使用有效的訪問令牌來查詢有關當前用戶的信息。」錯誤:/我不知道:「嘿,應用程序,發貼到牆上!」? :] – WASD42 2011-03-17 10:05:46

+0

@ WASD42:正如你在這裏可以看到'/ me/accounts',你需要頁面的管理員,這樣你就可以從他的'accounts'中獲得'access_token'頁面來使用它。 – ifaour 2011-03-17 12:13:03

+2

您應該也可以添加'offline_access'權限! – vinzenzweber 2011-03-26 14:02:08

相關問題