2012-01-30 191 views
1

我完全沮喪與我的第一個Facebook應用程序項目。我遇到了一些似乎是一件簡單任務的重大問題。Facebook:在頁面發佈頁面問題(訪問令牌/ PHP)

我想在我的服務器上設置一個cron作業(簡單),它將在Facebook頁面(不是我的個人檔案)上張貼一些聰明的東西,並且它應該作爲頁面發佈。我將自己編入一個角落,現在完全混淆了......請任何人幫忙嗎? 「OAuthException:錯誤校驗應用程序

我一直通幾乎每一個錯誤信息,我現在被困在

這是我現在有:

的第一個PHP - >獲取新的訪問令牌爲我的頁面訪問。這部分似乎工作正常,因爲我得到下一個頁面調用並接收新的訪問令牌。

<?php 
require_once 'facebook.php'; 

$app_id = "1234...."; 
$app_secret = "5678...."; 
$my_url = "http://.../next.php"; 

$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
'cookie' => true 
)); 

// known valid access token stored in a database 
$access_token = "abc...."; 

$code = $_REQUEST["code"]; 

// If we get a code, it means that we have re-authed the user 
//and can get a valid access_token. 
if (isset($code)) { 
    $token_url="https://graph.facebook.com/oauth/access_token? 
client_id=" 
     . $app_id . "&redirect_uri=" . urlencode($my_url) 
     . "&client_secret=" . $app_secret 
     . "&code=" . $code . "&display=popup"; 
    $response = file_get_contents($token_url); 
    $params = null; 
    parse_str($response, $params); 
    $access_token = $params['access_token']; 

} 

// Attempt to query the graph: 
$graph_url = "https://graph.facebook.com/me?" 
    . "access_token=" . $access_token; 
$response = curl_get_file_contents($graph_url); 
$decoded_response = json_decode($response); 

//Check for errors 
if ($decoded_response->error) { 
    // check to see if this is an oAuth error: 
    if ($decoded_response->error->type== "OAuthException") { 
     // Retrieving a valid access token. 
     $dialog_url= "https://www.facebook.com/dialog/oauth?" 
     . "client_id=" . $app_id 
     . "&redirect_uri=" . urlencode($my_url); 
     echo("<script> top.location.href='" . $dialog_url 
     . "'</script>"); 
    } else { 
     echo "other error has happened"; 
    } 
} else { 

    // success 
    echo("success" . $decoded_response->name); 

} 

function curl_get_file_contents($URL) { 
    $c = curl_init(); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($c, CURLOPT_URL, $URL); 
    $contents = curl_exec($c); 
    $err = curl_getinfo($c,CURLINFO_HTTP_CODE); 
    curl_close($c); 
    if ($contents) return $contents; 
    else return FALSE; 
} 

?> 

Next php - >閱讀訪問令牌併發布在牆上。我從查詢字符串中獲取訪問令牌,但爲什麼會收到錯誤消息。我的ID,祕密和頁面ID都是爲了...

<?php 
require_once 'facebook.php'; 

$app_id = "1234...."; 
$app_secret = "5678...."; 
$page_id = "0909...."; 

$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
'cookie' => true 
)); 

$access_token = $_GET['code']; 
echo($access_token); 

try { 
    $attachment = array(
       'access_token' => $access_token, 
       'message'=> "Hello World" 
     ); 

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

} catch(Exception $e) { 
    echo $e; 
} 

?> 

我敢肯定有一個簡單的方法,這樣做....和方式其實就是使它的工作! 任何幫助表示讚賞!

我跟着這個腳本 Update facebook page status from that page itselfhttps://developers.facebook.com/blog/post/500

謝謝。

+0

我在你的代碼不清楚(不是PHP dev的我自己)爲你在哪裏提取的頁面訪問令牌就像http://stackoverflow.com/questions/4309154/update-facebook- page-status-from-that-page-itself-graph-api-php。 – DMCS 2012-01-30 20:31:51

+0

我在第一個腳本中獲取訪問令牌,因爲當我使用從鏈接的示例中獲取的訪問令牌時,令牌限制爲2小時。第一個腳本檢查舊的訪問令牌 - >意識到它是舊的 - >並返回給我一個新的查詢字符串... – Peter1178373 2012-02-01 13:09:06

回答

1

從您的代碼看來,您沒有獲得您需要的PAGE訪問令牌,而是您使用的是USER訪問令牌,因此API爲什麼不喜歡它。有關如何從用戶的一個遇到頁面訪問令牌的更多信息,請參閱的https://developers.facebook.com/docs/authentication/

+0

感謝您的答案。訪問令牌概念讓我感到非常愚蠢。不知何故,我無法繞過它...:/無論如何,我會再次檢查出來(希望)很快發佈一個工作代碼... – Peter1178373 2012-02-02 16:46:25

+0

是的,它需要時間來包裹你的頭。它從字面上花了我三個發展日來達到我可以使用它的程度。快樂編碼。我希望我的回答能幫助你。 – DMCS 2012-02-02 17:11:54

+0

感謝您的輸入,DMCS。我已經開始工作了(見下面的答案),儘管它遠非完美。 – Peter1178373 2012-02-13 07:04:33

-1

頁面登錄部分多小時的反覆試驗後,這裏是我的解決方案,到目前爲止的作品。

<?php 

require_once 'facebook.php'; 

//------ vars 
$app_id = "123..."; 
$app_secret = "abc..."; 
$page_id = "999..."; 
$my_url = "http://exactly the same as defined /"; 

//------ fb object 
$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
'cookie' => true 
)); 

//------ verification CODE 
// this is not pretty -> the code should be received with a another graph query... 
// but I couldn't get this to work. Thus I'm using my last known Verification Code 
$code = "ABC123..."; 

//------ get USER access token 
if (isset($code)) { 
    $token_url="https://graph.facebook.com/oauth/access_token?client_id=" . $app_id 
     . "&client_secret=" . $app_secret 
     . "&code=" . $code 
     . "&redirect_uri=" . $my_url; 

    $response = file_get_contents($token_url); 
    $params = null; 
    parse_str($response, $params); 
    $user_access_token = $params['access_token']; 

} else { 
    echo("No code"); 
} 

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

$result = $facebook->api("/me/accounts", $attachment_1); 
    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, 
       'message'=> 'hello world!' 
     ); 

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

} catch(Exception $e) { 
    echo $e; 
} 

?> 
+0

夥計,那不好。 DMCS給了你正確的答案,你偷走了他/她。 -1在你的答案。 – CoderFromOuterSpace 2012-02-13 14:07:47

+1

對不起,我顯然不太瞭解stackoverflow的評級和接受如何工作。我用複選標記標記了這個答案,因爲這包含整個解決方案。很顯然,DMCS對此做出了很大的貢獻。所以我應該怎麼做?或者更好的是,我如何改變以使其正確? – Peter1178373 2012-02-13 16:50:15

+0

你明白了。那灰色的複選標記起初很難弄清楚。 – CoderFromOuterSpace 2012-02-13 18:43:26

6
// firstly include the fb sdk 

$facebook = new Facebook(array(
       'appId' => 'your app id', 
       'secret' => 'your app secret',`enter code here` 
       'cookie' => true, 

    )); 


$session = $facebook->getUser(); 
    $loginUrl = $facebook->getLoginUrl(
        array(
         'scope' => 'user_status,publish_stream,email,user_location,user_birthday,friends_birthday,manage_pages', 
         'redirect_uri' => 'http://www.example.com/' 
        ) 
       ); 

    $logoutUrl = $facebook->getLogoutUrl(
        array(
         // any url u want to redirsct onto 
         'redirect_uri' => 'http://www.example.com/' 

        ) 
       ); 

// when redirected from facebook get the code 
    $code = $_GET['code']; 

// 

             $my_url  = 'http://www.example.com/'; 

             $app_id  = 'your app id '; 
             $app_secret = 'your app secret '; 
             // here we have the access token so we will play with it 
             if (isset($code)) { 
               $token_url="https://graph.facebook.com/oauth/access_token?client_id=" 
                . $app_id . "&redirect_uri=" . urlencode($my_url) 
                . "&client_secret=" . $app_secret 
                . "&code=" . $code ; 
               $response = file_get_contents($token_url); 
               $params = null; 
               parse_str($response, $params); 
               $user_access_token = $params['access_token']; 
              } 

// here we got the access token of user over here in $user_access_token 
// now we will get for our page 
//------ get PAGE access token 
             $attachment_1 = array(
              'access_token' => $user_access_token 
             ); 
             $page_id = 'your page id '; 
             $result = $facebook->api("/me/accounts", $attachment_1); 
              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, 
                 'message'=> 'hello world posting like admin!', 
                 'page_id'=> $page_id 
               ); 

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

             } catch(Exception $e) { 
              echo $e; 
             } 
+0

感謝你的這些,代碼和評論爲如何正確使用SDK提供了一個很好的例子。 SO需要一個選項來獲得最喜歡的答案 – 2014-04-13 08:32:12