2011-10-31 64 views
2

我知道這裏有很多questons,但似乎都需要用戶登錄...我一直在使用所有可能的代碼片段教程,但似乎沒有工作。發佈到粉絲頁面的牆上,從另一個PHP站點頁面

這是場景: 我有一個照片社區在PHP上運行,我在Facebook上有一個粉絲頁面。當主站點上的圖像收集到一定數量的選票時,會觸發一個功能將圖像鏈接發佈到Facebook牆上。鏈接(帖子)以頁面形式發佈,而不是以管理員身份發佈。管理員當然不會上網......這些日子甚至有可能這樣做嗎?我有最新的PHP SDK,這是我需要在插件進入主站點之前獨立工作的功能。

好的。如果我登錄到Facebook,此代碼完美工作,但如果我不是 - 它不會發布...該應用程序具有所有必要且不必要的:)權限,以我的(管理員)身份與我的頁面進行交互。任何想法將不勝感激。

<?php 
//facebook application 
$fbconfig['appid' ]  = "1848740815xxxxx"; 
$fbconfig['secret']  = "a5aa62bb3a8ddcb98d5d9dbe4a3xxxxx"; 
$fbconfig['pageid']  = "121409594622865"; 

$user = null; //facebook user uid 
try{ 
    include_once "facebook.php"; 
} 
catch(Exception $o){ 
    error_log($o); 
} 
// Create our Application instance. 
$facebook = new Facebook(array(
    'appId' => $fbconfig['appid'], 
    'secret' => $fbconfig['secret'] 
)); 
//Facebook Authentication part 
$user  = $facebook->getUser(); 
$loginUrl = $facebook->getLoginUrl(
     array(
      'scope'   => 'offline_access,publish_stream' 
      ) 
     ); 
$logoutUrl = $facebook->getLogoutUrl(); 
$pageid = $fbconfig['pageid']; 

if ($user) { 
     try { 
     $page_info = $facebook->api("/$pageid?fields=access_token"); 

     if(!empty($page_info['access_token'])) { 
     $args = array(
      'access_token' => $page_info['access_token'], 
      'message' => 'This is a test feed message', 
      'link' => 'http://www.fotodvor.com', 
      'picture' => 'http://www.fotodvor.com/data/media/15/1319971991.jpg', 
      'name' => 'Test Picture', 
      'description'=> 'Description of the test picture!' 
     ); 
     $post_id = $facebook->api("/$page_id/feed","post",$args); 
    } 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 
?> 

在此先感謝

+0

對我們的任何錯誤的變化? – Ashley

+0

nope ...沒有任何錯誤...如果我登錄這個腳本發佈一切正常...當我註銷 - 什麼都沒有... –

回答

5

這裏是解釋修改腳本調整爲API

<?php 
//facebook application 
$fbconfig['userid'] = "5332534xx"; 
$fbconfig['appid' ] = "184874081548xxx"; 
$fbconfig['secret'] = "a5aa62bb3a8xxxb98d5d9dbe4a368xxx"; 
$fbconfig['pageid'] = "121409594622xxx"; 

$fbconfig['token1']  = "AAACoJFn1eLABAKpL9W0nZBrw0e3zzdSNVsTg6FWDMhSnOUeinjid6yAQ2z9JDxxxxxxc1hMHBC3GG18KZBwppGDehWMEwLe56wagZDZD"; // step 1 - returned by loggin in. 
$fbconfig['token2']  = "AAACoJFn1eLABAC2Q8OLnqxjUSKzdn9CzaXhy8nsG61vzp2ufePr5iwHZA7TM7Ibxxxxxxyf868O04FeBxMrIo0RCumrNaB78hZAp2uRrbVlGVPXP"; // step 3 - this is a page access token as page 
$fbconfig['my_ulr']  = 'http://'.$_SERVER['SERVER_NAME']; 

include_once "facebook.php"; 

// Create our Application instance. 
$facebook = new Facebook(array(
    'appId' => $fbconfig['appid'], 
    'secret' => $fbconfig['secret'], 
)); 

// 1. we need to get a user access_token first, (old approach with offline_access forces the tokens received not to expire (good examples here - http://developers.facebook.com/docs/authentication2/ and http://www.howtobe.pro/tag/graph-api) 
// run the file and see step 1 instructions. 

//offline_access has been deprecated in May 2012 and therefore excluded from the request below 
$token_url1 = "https://www.facebook.com/dialog/oauth?"."client_id=".$fbconfig['appid']."&redirect_uri=".urlencode($fbconfig['my_ulr'])."&scope=manage_pages,publish_stream&response_type=token"; 

echo "<h2>This little working example should give you a working non expiring token to use in your PHP script to enable it to post to your Facebook page as a page and not as a user</h2><br>"; 
echo "1 - Click on the link below (this redirects to uri with token attached). Log in as admin of the page you are trying to post to. Then copy the token you will get in the address bar to be used in the script in step 2.<br>"; 
echo "<a href='".$token_url1."' target='_blank'>$token_url1</a>"; 

//2. then paste the token you received into "step 1" variable in the config section above. Run this script again when logged in to receive all info. 
$token_url2 = "https://graph.facebook.com/me?access_token=".$fbconfig['token1']; 
$me = json_decode(file_get_contents($token_url2), true); 

//echo "<hr><br>this URL gives you all pages that you as admin have access to, but these are NOT what we need to post to the fan page as PAGE so this is just for the heck of it...<br>"; 
//echo "<a href='".$token_url2."' target='_blank'>$token_url2</a>"; 
//echo "<hr>this is a raw server reply<br>"; 
//echo d($me['id'])."<hr>"; 

//new changes to API - https://developers.facebook.com/roadmap/offline-access-removal/#extend_token 
$new_token_url2 = "https://graph.facebook.com/oauth/access_token?client_id=".$fbconfig['appid']."&client_secret=".$fbconfig['secret']."&grant_type=fb_exchange_token&fb_exchange_token=".$fbconfig['token1']; 
$new_token2 = file_get_contents($new_token_url2); 
$vars = explode('&', $new_token2); 
//d($vars); 

echo "2. We now obtain a long lasting token (based on <a href='https://developers.facebook.com/roadmap/offline-access-removal/#extend_token' target='_blank'>this</a>) 
<br><br>We send the request<br>'".$new_token_url2."'<br><br>and the reply is:<br>'".$new_token2."'<br><br>"; 

//http://developers.facebook.com/tools/explorer?method=GET&path=533253476&accounts&access_token=AAACoJFn1eLABAFZBftV0vtlBiHKA7ZAIrukrriyp2coWSavj3L4CbfJ9r3WY76IPi7pwUgt3wsubaI4iBbsr663PrbNyaLdZAhLxneOLAZDZD 

echo"So now open this page <a href='http://developers.facebook.com/tools/explorer' target='_blank'>http://developers.facebook.com/tools/explorer</a>, 
then put the token above to put into the 'Access Token: ' field and press enter... You will need to press 'accounts' link to the right from the response window. 
<br>you will see another response and copy your page access token from there. Automating this task into one query did not work... I tried many times.. the token returned IS NOT THE SAME as you would get following the instructions step by step... 
This approach does not work - <br>"; 
echo "http://developers.facebook.com/tools/explorer?method=GET&path=".$me['id']."%2Faccounts&".$vars[0]."<BR><BR><BR>"; 
echo "Please check it here <a href='http://developers.facebook.com/tools/debug' target='_blank'>https://developers.facebook.com/tools/debug</a> and make sure it never expires..."; 


$pageid = $fbconfig['pageid']; 
try { 

//Step 3. Run this script WHEN LOGGED IN to and paste the resulting token into step 3 variable above to check the functionality 

/*  $page_info = $facebook->api("/$pageid?fields=access_token&".$new_token2); //wrong approach to use this straight. THIS is the access token is that we need. BUT this will work only if user is logged in. so 
    echo "<hr>this is a page_info breakdown"; 
    d($page_info); 
    echo "and the access token you needs to paste into fbconfig['token2'] variable is this:<br>"; 
    echo $page_info['access_token']; */ 

    $args = array(
      'access_token' => $fbconfig['token2'], //do not attempt to plug the $page_info['access_token'] here... it will be empty once you log off Facebook 
      'message' => 'This is a test feed message', 
      'link' => 'http://www.test.com', 
      'picture' => 'https://www.google.com/intl/en_com/images/srpr/logo3w.png', 
      'name' => 'Test Picture', 
      'description'=> 'Description of the test picture!' 
     ); 

     //uncomment this once you are ready to post and you can see all the access token in the last step. Then comment out all echo and d()'s to make the script silent...    
     //$post_id = $facebook->api("/$pageid/feed","post",$args); 
     echo "<hr>This will show once the message is posted - post_id is: <br>"; 
     d($post_id); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    } 

function d($d){ 
    echo '<pre>'; 
    print_r($d); 
    echo '</pre>'; 
} 
?> 
1

您應該使用的頁面訪問令牌,如果我理解你想要正確地做什麼 - 這是一個憑證,以便讓您的應用程序作爲頁面,不是作爲管理員之一。 這是在/me/accounts端點訪問,當你有一個用戶訪問令牌manage_pages權限 - 如果您使用的訪問令牌張貼到/{page id}/feed(或照片等),它會出現在頁面

+0

我不認爲這是完全準確的。我在上面發佈了我的發現。您收到的作爲/ me /帳戶回覆的令牌不會讓我發佈爲頁面。 –

+0

它應該有;這是我一直這樣做的;你是否意外地從另一個頁面拿走了令牌?如果沒有,問題是什麼? – Igy

+0

我真的不知道爲什麼這不適合我...這種方法仍然適用於你的情況嗎?你有這樣的腳本嗎? –

2

我想我有找到答案。經過長時間的測試,這裏是解決方案: 對於那些新手來說,這只是一個小樣本/指南。 代碼和輸出具有所有必要的信息:

<?php 
//facebook application 
$fbconfig['appid' ] = "184874081XXXXXX"; 
$fbconfig['secret'] = "a5aa62bb3a8ddcb98d5d9dbe4aXXXXXX"; 
$fbconfig['pageid'] = "121409594XXXXXX"; 
$fbconfig['token1']  = "AAACoJFn1eLABAHn92JsWIHZCESQWXmkXZBCedXXXXXXcyUG5vrCYZBXcgsNHN0IUvBj0Sec9vOxVsUgtMHflXXF2cbOF1oZD"; // step 1 - returned by loggin in. 
$fbconfig['token2']  = "AAACoJFn1eLABAAUAPthH5DaZCmasZCh5DGGSnZXXXXXXSDh8v1WYYUEWJYuFdua9E5EfJ63c03lfwXrVJbP4VQj35aVcztFgKRYZAheHPNfDeLfbkPys"; // step 3 - this is a page access token as page 
$fbconfig['my_ulr']  = 'http://'.$_SERVER['SERVER_NAME']; 

include_once "facebook.php"; 

// Create our Application instance. 
$facebook = new Facebook(array(
    'appId' => $fbconfig['appid'], 
    'secret' => $fbconfig['secret'], 
)); 

// 1. we need to get a user access_token first, offline_access forces the tokens received not to expire (good examples here - http://developers.facebook.com/docs/authentication2/ and http://www.howtobe.pro/tag/graph-api) 
//run the file and see step 1 instructions. 
$token_url1 = "https://www.facebook.com/dialog/oauth?"."client_id=".$fbconfig['appid']."&redirect_uri=".urlencode($fbconfig['my_ulr'])."&scope=manage_pages,offline_access,publish_stream&response_type=token"; 
echo "1 - this redirects to uri with token attached. Copy and paste this line into your browser and log in as admin of the page you are trying to post to. Make sure you change redirect_uri to your own. Then copy the token you will get in the address bar to be used in the script in step 2.<br>"; 
echo $token_url1; 


//2. then paste the token you received into "step 1" variable in the config section above. Run this script again when logged in to receive all info. 
$token_url2 = "https://graph.facebook.com/me/accounts?access_token=".$fbconfig['token1']; 
$app_token2 = file_get_contents($token_url2); 
echo "<hr><br>2 - this URL gives you all pages that you as admin have access to, but these are NOT what we need to post to the fan page as PAGE<br>"; 
echo $token_url2; 
echo "<hr>2 - this is a raw server reply<br>"; 
d($app_token2); 


$pageid = $fbconfig['pageid']; 
try { 

    //Step 3. Run this script WHEN LOGGED IN to and paste the resulting token into step 3 variable above 
    $page_info = $facebook->api("/$pageid?fields=access_token"); //wrong approach to use this straight. THIS is the access token is that we need. BUT this will work only if user is logged in. so 
    echo "this is a page_info breakdown"; 
    d($page_info); 
    echo "and the access token you needs to paste into fbconfig['token2'] variable is this:<br>"; 
    echo $page_info['access_token']; 

    $args = array(
      'access_token' => $fbconfig['token2'], //do not attempt to plug the $page_info['access_token'] here... it will be empty once you log off Facebook 
      'message' => 'This is a test feed message', 
      'link' => 'http://www.test.com', 
      'picture' => 'https://www.google.com/intl/en_com/images/srpr/logo3w.png', 
      'name' => 'Test Picture', 
      'description'=> 'Description of the test picture!' 
     ); 
     //uncomment this once you are ready to post and you can see all the access token in the last step. Then comment out all echo and d()'s to make the script silent...    
     //$post_id = $facebook->api("/$pageid/feed","post",$args); 
     echo "<hr>This will show once the message is posted - post_id is: <br>"; 
     d($post_id); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    } 

function d($d){ 
    echo '<pre>'; 
    print_r($d); 
    echo '</pre>'; 
} 
?> 
+0

你覺得我有一個不必要的步驟; '/ me/accounts'端點中的頁面訪問令牌應該可以與來自'/ {PAGE ID} /?fields = access_token'調用的令牌互換 – Igy

+0

嗯,我的測試顯示它們不是....或者我錯過了一些東西......這對我沒有用。如果我登錄了FB,您的方式就可以工作......即使我離線,我的方法也能正常工作。 –

+0

你從哪裏得到'$ fbconfig ['token1']'和'$ fbconfig ['token2']'? 是否有可能在沒有任何對話框和瀏覽器的情況下(例如在cron-job腳本中)獲得「用戶的訪問令牌」? – Lari13

相關問題