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

发布于 2024-12-31 22:08:01 字数 3404 浏览 0 评论 0 原文

我对我的第一个 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 ->阅读访问令牌并将其张贴在墙上。我从查询字符串中获取了访问令牌,但是为什么我会收到错误消息。我的 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;
}

?>

我确信有一种更简单的方法可以做到这一点...以及一种实际使其工作的方法! 任何帮助表示赞赏!

我按照这个脚本 从该页面本身更新 Facebook 页面状态 还有这个 https://developers.facebook.com/blog/post/500

谢谢。

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

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?

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

Here's what I have now:

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 -> 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!

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2025-01-07 22:08:01
// 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;
                                        }
// 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;
                                        }
甜心 2025-01-07 22:08:01

从您的代码来看,您似乎没有获得所需的 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/

呢古 2025-01-07 22:08:01

经过几个小时的反复试验,这是我迄今为止有效的解决方案。

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

?>

After many hours of trial and error, here's my solution that works so far.

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文