douyong1285 2015-01-09 18:14
浏览 36

Facebook Ads API Cron Job

We are trying to use the facebook ad reporting api to update a 3rd party app to keep up-to-date on our daily spending on facebook. We would like for these values to update automatically via a cron job but this doesn't appear to work. We have to go to the page and refresh it, for it to refresh the api data. Is it possible to get our script to update automatically? Here is our access token code.

I thought it was possible to get a long lived token and save it to the database and use that but we are unable to retrieve the accessToken because its :protected in the return json.

use FacebookAds\Api;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\AdGroup;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Fields\AdGroupFields;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
use Facebook\Entities\AccessToken;

// Initialize the SDK
FacebookSession::setDefaultApplication( $app_id, $app_secret );

// Create the login helper and replace REDIRECT_URI with your URL
// Use the same domain you set for the apps 'App Domains'
// e.g. $helper = new FacebookRedirectLoginHelper( 'redirect' );
$helper = new FacebookRedirectLoginHelper( $redirect_uri );

// Check if existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
 // Create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );

// Validate the access_token to make sure it's still valid
try {
  if ( ! $session->validate() ) {
    $session = null;
  }
} catch ( Exception $e ) {
  // Catch any exceptions
  $session = null;
}
} else {
 // No session exists
try {
$session = $helper->getSessionFromRedirect();

} catch( FacebookRequestException $ex ) {

// When Facebook returns an error
} catch( Exception $ex ) {

  // When validation fails or other local issues
  echo $ex->message;
}
}

// Check if a session exists
if ( isset( $session ) ) {

  // Save the session
   $_SESSION['fb_token'] = $session->getToken();
$access_token = $_SESSION['fb_token'];
$long_session = $session->getLongLivedSession();
//print_r($long_session);
$longtoken = $long_session->getToken();
  // Create session using saved token or the new one we generated at login
   $session = new FacebookSession( $long_session->getToken() );

  } else {
   // No session

   // Get login URL
   $loginUrl = $helper->getLoginUrl( $permissions );

//echo '<a href="' . $loginUrl . '">Log in</a>';
  header('Location: '.$loginUrl.'');
   }
    // Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);

// The Api object is now available trough singleton
$api = Api::instance();

// Get the GraphUser object for the cusrrent user:

try {
 $me = (new FacebookRequest(
   $session, 'GET', '/me'
 ))->execute()->getGraphObject(GraphUser::className());
// echo $me->getName();
} catch (FacebookRequestException $e) {
 // The Graph API returned an error
} catch (\Exception $e) {
  // Some other error occurred
 }
  • 写回答

1条回答 默认 最新

  • doushan1157 2015-01-10 18:49
    关注

    I read two questions in your post, the first is that you would like to have a background process (invoked by cron) that reads your daily spending and second that you don't have a long-lived user access token so your script isn't working correctly.

    If I understand your goals correctly, it seems that these two issues are a bit conflated which I recommend separating. Follow the Facebook access token documentation for getting a long-lived token and use it directly in your script. Skip PHP session management and just put the long-lived token in a database, simple config file, or directly into the script.

    And then if you're not already, have this script invoked by cron or another scheduled background processing system. It can pull your daily spending data and provide it to the 3rd party app.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题