Facebook:作为页面问题发布在页面上(访问令牌/ php)

时间:2012-01-30 15:41:08

标签: php facebook access-token

我对我的第一个Facebook应用程序项目感到非常沮丧。我在看似简单的任务方面遇到了重大问题。

我想在我的服务器上设置一个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;
}

?>

下一个php - &gt;阅读访问令牌并在墙上发布。我从查询字符串中获取访问令牌,但是为什么我会收到错误消息。我的身份,秘密和页面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 itself 还有这个 https://developers.facebook.com/blog/post/500

谢谢。

3 个答案:

答案 0 :(得分: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;
                                        }

答案 1 :(得分:1)

从您的代码中,您似乎没有获得所需的PAGE访问令牌,而是您正在使用USER访问令牌,因此API不喜欢它。有关如何从USER获取PAGE访问令牌的更多信息,请参阅https://developers.facebook.com/docs/authentication/页面登录部分

答案 2 :(得分:-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;
}

?>