2

我搜索了 2 天关于我的问题找到了很多解决方案,但没有一个对我有用。我想制作一个 facebook 应用程序,我将从 cronjob 访问一次 20 分钟。这个应用程序应该在我的页面上发布一张带有相册描述的照片。问题是令牌访问过期......所以代码......

//connect to api
$facebook = new Facebook(array(
          'appId'  => APP_ID,
          'secret' => APP_SECRET
        ));

//get the page token
$page_id = 222506244512259;
$page_info = $facebook->api("/$page_id?fields=access_token");

$page_info['access_token']; //so if i use this token is ok but it expires in 2 hours

//now we try to use something else to extend the time

$token_url="https://graph.facebook.com/oauth/access_token?client_id=".APP_ID."&client_secret=".APP_SECRET."&grant_type=fb_exchange_token&fb_exchange_token=".$facebook->getAccessToken();

//so if i use this formula i will receive a token that never expires, is ok but if I try to use the acces token for posting it doesn work is not valid

$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$page_info['access_token'] = $params['access_token'];


//and this is the rest code for posting

$image['file'] = 'images/rezultate/'.$link;
$args = array( 'access_token' => $page_info['access_token'], 'message' => ':-) <3', 'no_story' => 0);
$args['image'] = '@' . $image['file'];
$target_url = "https://graph.facebook.com/356048711158011/photos";

$ch = curl_init(); 
curl_setopt ($ch,CURLOPT_URL,$target_url); 
curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_exec($ch); 
curl_close ($ch);

我更新了代码但仍然无法正常工作,也许你有什么问题......

define('TITLU', 'Postare automată'); //titlu aplicatie
define('APP_ID',123096994520827); //id aplicatie
define('APP_SECRET', '25dedb6a969e9bd934e1ea9f16944a83'); //secret aplicatie
define('TABEL', 'postareautomata'); //numar random maxim


//start facebook
$facebook = new Facebook(array(
      'appId'  => APP_ID,
      'secret' => APP_SECRET
));

//get page id and 2 hour page token
$page_id = 222506244512259;
$page_info = $facebook->api("/$page_id?fields=access_token");
//access token 2 hours
$page_info['access_token'];

//here i get my account but for nothing
$acc = $facebook->api("/100001036884257/accounts");
//echo var_export($acc, true);


//here i get the long token it returns a code value but when i used down don`t work
$longtoken = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=fb_exchange_token&fb_exchange_token='.$facebook->getAccessToken();     
$response = file_get_contents($longtoken);
$params = null;
parse_str($response, $params);
$longtoken = $params['access_token'];

echo"Acces Token short: ".$page_info['access_token'];
echo"<br />Acces Token long: ".$longtoken;

//here i get my folder picture and pictures
$a = file_get_contents('http://www.ingersidemon.ro/proiecte/pitzi-world.ro/.facebook/postareautomata/images/rezultate/');
$dom = new DOMDocument;
$dom->loadHTML($a);
$nodes = $dom->getElementsByTagName('a');
$nrpoze = $dom->getElementsByTagName('a')->length - 1;
$picid = get_last_picture();
$link = $nodes->item($picid['id'] + 1)->getAttribute('href');
//echo "Link poza: ".$link;



if($picid['id'] + 1 <= $nrpoze) {
    $data = array(
        'link' => $link,
        'desc' => ':-) <3'
    );
    insert_picture($data);
//and here is the bog problem if i use short acces token it works for 2 hours but the long never work
    $params = array('access_token' => '$longtoken', 'message' => ':-) <3');
    $params['source'] = '@' . realpath('images/rezultate/'.$link);
    $facebook->setFileUploadSupport(true);
    $post_id = $facebook->api('/'.$page_id.'/photos','post',$params);

}

更新 3:该应用程序运行良好,但前提是我访问它,而不是来自 cronjob,并且访问令牌是 2 个月,在 cronjob 中给出

PHP 致命错误:未捕获的 OAuthException:请求此资源需要用户访问令牌。

define('TITLU', 'Postare automată'); //titlu aplicatie
define('APP_ID',123096994520827); //id aplicatie
define('APP_SECRET', '25dedb6a969e9bd934e1ea9f16944a83'); //secret aplicatie
define('TABEL', 'postareautomata'); //numar random maxim

$facebook = new Facebook(array(
      'appId'  => APP_ID,
      'secret' => APP_SECRET
));


// Get User
$user = $facebook->getUser();


// ask for the extended token and get it from session ...
$facebook->setExtendedAccessToken();
$access_token = $_SESSION["fb_".APP_ID."_access_token"];
// now set it into the facebook object ....
$facebook->setAccessToken($access_token);
// now our fb object will use the new token as usual ...
$accessToken = $facebook->getAccessToken();
echo $access_token;

$params = array(
                'access_token' => 'AAABv9MChnvsBAALtC4XoywkLdGQMzmJHYbN8G2RjgZCGlzR6DnkB8X12FZCE6XIZB81KkYevIYzC2pu5UvVZBoOFvw8j0cgjLeHcwvcOpwZDZD',
                'message'=> "cron"
        );
    $post_id = $facebook->api('/'.$page_id.'/feed','post',$params);
4

4 回答 4

0

使用Access Token Tool中的App Token

于 2012-12-17T17:05:10.927 回答
0

普通访问令牌在 2 小时后过期,扩展令牌有效期为 2 个月。

如果您想要扩展令牌,请执行以下操作:How to extend access token validation since offline_access deprecation

(您可以使用Facebook 的调试工具来检查您的访问令牌的有效性。)

于 2012-12-18T05:43:28.013 回答
0

在此处查看我对类似问题的回答

从开发人员页面:

通过使用长期用户访问令牌,查询 [User ID]/accounts 端点现在将为用户管理的页面提供不会过期的页面访问令牌。

于 2012-12-18T07:22:20.917 回答
-1

页面令牌的持续时间与用于获取它们的用户访问令牌一样长。你不能拥有一个无限持久的令牌。您需要能够定期更新您的令牌并处理意外过期的令牌。去年的 FB 博客文章中提供了更多信息。

于 2012-12-17T21:27:00.710 回答