【问题标题】:Facebook: Post on Page as Page issue (access token / php)Facebook:在页面上发布为页面问题(访问令牌/php)
【发布时间】:2012-02-22 09:37:05
【问题描述】:

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

我想在我的服务器上设置一个 cron 作业(简单),它将在 facebook 页面(不是我的个人资料)上发布一些智能内容,并且它应该作为页面发布。我把自己编码到一个角落里,现在完全糊涂了……有人可以帮忙吗?

我几乎浏览了所有错误消息,现在卡在了 “OAuthException:验证应用程序时出错。

这是我现在拥有的:

First 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、secret 和 page 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

谢谢。

【问题讨论】:

  • 我在你的代码中不清楚(我自己不是 PHP 开发人员)关于你像 stackoverflow.com/questions/4309154/… 那样提取页面访问令牌的位置。
  • 我在第一个脚本中获取了访问令牌,因为当我使用从您链接到的示例中获取的访问令牌时,该令牌被限制为 2 小时。第一个脚本检查旧的访问令牌 -> 意识到它是旧的 -> 并在查询字符串中返回一个新的...

标签: php facebook access-token


【解决方案1】:
// 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;
                                        }

【讨论】:

  • 感谢您,代码和 cmets 为如何正确使用 SDK 提供了一个很好的示例。 SO 需要一个选项来收藏答案。
【解决方案2】:

从您的代码看来,您没有获得所需的 PAGE 访问令牌,而是使用了 USER 访问令牌,因此 API 不喜欢它。有关如何从 USER 获取 PAGE 访问令牌的更多信息,请参阅https://developers.facebook.com/docs/authentication/

Page login 部分

【讨论】:

  • 感谢您的回答。访问令牌的概念让我感到非常愚蠢。不知何故,我无法理解它......:/无论如何,我会再次检查并(希望)尽快发布工作代码......
  • 是的,你需要花时间来理解它。从字面上看,我花了三天的开发时间才达到可以使用它的程度。快乐编码。希望我的回答对你有所帮助。
  • 感谢您的意见,DMCS。我已经让它工作了(见下面的答案),虽然它远非完美。
  • 我没有任何不尊重的意思。是的,由于您的回复,我更正了页面访问令牌问题。不幸的是,由于我没有足够的声望点,我无法投票给答案。无论如何,我想为任何可能感兴趣的人发布整个代码。那我不应该将其作为新答案发布吗?
  • 你有代码的地方很好,但你可以随时编辑你的问题本身,如果你愿意,可以发布整个代码,并标记这是你根据我的帮助提出的。感谢您接受答案。 :) 编码愉快!
【解决方案3】:

经过数小时的反复试验,这是我目前有效的解决方案。

<?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;
}

?>

【讨论】:

  • 老兄,这不好。 DMCS 给了你正确的答案,而你从他/她那里偷走了它。 -1 你的答案。
  • 抱歉,我显然不太了解 stackoverflow 评级和接受的工作原理。我用复选标记标记了这个答案,因为它包含整个解决方案。显然 DMCS 对此做出了很大贡献。那我应该怎么做?或者更好的是,我该如何更改才能使其正确?
  • 你明白了。那个灰色的复选标记一开始有点难以弄清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 2012-08-03
  • 1970-01-01
  • 2017-01-05
  • 2012-01-04
相关资源
最近更新 更多