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

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

问题描述

我对我的第一个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
));

//存储在数据库中的已知有效访问令牌
$ access_token =abc ....;

$ code = $ _REQUEST [code];

//如果我们得到一个代码,这意味着我们已经重新创建了用户
//,并且可以获得一个有效的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 =。 $代码。 &安培;显示=弹出;
$ response = file_get_contents($ token_url);
$ params = null;
parse_str($ response,$ params);
$ access_token = $ params ['access_token'];

}

//尝试查询图表:
$ graph_url =https://graph.facebook.com/me?
。 access_token =。 $的access_token;
$ response = curl_get_file_contents($ graph_url);
$ decoded_response = json_decode($ response);

//检查错误
if($ decoded_response-> error){
//检查这是否是一个oAuth错误:
if($ decoded_response-> error-> type ==OAuthException){
//检索有效的访问令牌。
$ 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其他错误已发生;
}
} else {

// success
echo(success。$ decoded_response-> name);

}

函数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' 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);

尝试{
$ attachment = array(
'access_token'=> $ access_token,
'message'=>Hello World
);

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

} catch(异常$ e){
echo $ e;
}

?>

我确定有一个更简单的方法这样做....让它工作!
任何帮助都不胜感激!



我按照这个脚本
从该页面更新Facebook页面状态
和$ $ $ $ $ $ $ $ $ $ $ $ $ ://developers.facebook.com/blog/post/500rel =nofollow noreferrer> https://developers.facebook.com/blog/post/500



谢谢

解决方案

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


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.

解决方案

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天全站免登陆