Facebook:作为页面问题在页面上发布(访问令牌/php) [英] Facebook: Post on Page as Page issue (access token / php)

查看:22
本文介绍了Facebook:作为页面问题在页面上发布(访问令牌/php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的第一个 Facebook 应用项目感到非常沮丧.我在处理看似简单的任务时遇到了重大问题.

I'm totally frustrated with my first facebook app project. I'm having major issues with what seems to be a simple task.

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

I want to setup a cron job on my server (easy) that will post something smart on a facebook page (not my profile), and it should be posted as page. I coded myself into a corner and am totally confused now... Can anyone help, please?

我已经处理了几乎所有错误消息,但现在仍然坚持OAuthException:验证应用程序时出错."

I've been thru pretty much every error message and am now stuck on "OAuthException: Error validating application."

这是我现在所拥有的:

首先 php -> 为我的页面访问获取一个新的访问令牌.这部分似乎工作正常,因为我调用了下一个页面并收到一个新的访问令牌.

First php -> Gets a new Access Token for my page access. This part seems to work fine, as I get the next page called and receive a new access token.

<?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 都整理好了...

Next php -> Read the access token and post on the wall. I get the access token from my query string, but then why do I receive the error message. My id, secret and page id are all in order...

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

?>

我敢肯定有一种更简单的方法可以做到这一点......而且还有一种真正让它发挥作用的方法!任何帮助表示赞赏!

I'm sure there is a simpler way to do so.... and a way to actually make it work! Any help is appreciated!

我遵循了这个脚本从该页面本身更新 Facebook 页面状态和这个https://developers.facebook.com/blog/post/500

I followed this script Update facebook page status from that page itself and this https://developers.facebook.com/blog/post/500

谢谢.

推荐答案

从您的代码来看,您似乎没有获得所需的 PAGE 访问令牌,而是使用了 USER 访问令牌,因此为什么使用 API不喜欢.有关如何从 USER 获取 PAGE 访问令牌的更多信息,请参阅 页面登录 部分rel="nofollow">https://developers.facebook.com/docs/authentication/

From your code, it appears you are not getting a PAGE access token which you need, but instead you're using a USER access token, hence why the API doesn't like it. For more information on how to get a PAGE access token from a USER one, please see the Page login section of https://developers.facebook.com/docs/authentication/

这篇关于Facebook:作为页面问题在页面上发布(访问令牌/php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆