How to avoid fatal error: Uncaught OAuthException

2019-06-26 00:33发布

问题:

Hi hope somone can help with this one. Ive had a birthday reminder app built, that aquires the usual permissions including offline access etc.

The app requires a daily cron job to be run on my server. When I run the cron file a recieve the below error

Fatal error: Uncaught OAuthException: Invalid OAuth access token signature. thrown in blah/base_facebook.php on line 1140;

Is there a common reason for the error, am i doing anything wrong that stands out, and should i be displaying more code to get help from people?

below are the lines leading up to the error. My code ends on line 1140;

<?php

        $name = 'api';
    if (isset($READ_ONLY_CALLS[strtolower($method)])) {
      $name = 'api_read';
    } else if (strtolower($method) == 'video.upload') {
      $name = 'api_video';
    }
    return self::getUrl($name, 'restserver.php');
  }

  protected function getUrl($name, $path='', $params=array()) 
{
    $url = self::$DOMAIN_MAP[$name];
    if ($path) {
      if ($path[0] === '/') {
        $path = substr($path, 1);
      }
      $url .= $path;
    }
    if ($params) {
      $url .= '?' . http_build_query($params, null, '&');
    }

    return $url;
  }

  protected function getCurrentUrl() {
    if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
      $protocol = 'https://';
    }
    else {
      $protocol = 'http://';
    }
    $currentUrl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $parts = parse_url($currentUrl);

    $query = '';
    if (!empty($parts['query'])) {
      // drop known fb params
      $params = explode('&', $parts['query']);
      $retained_params = array();
      foreach ($params as $param) {
        if ($this->shouldRetainParam($param)) {
          $retained_params[] = $param;
        }
      }

      if (!empty($retained_params)) {
        $query = '?'.implode($retained_params, '&');
      }
    }

    // use port if non default
    $port =
      isset($parts['port']) &&
      (($protocol === 'http://' && $parts['port'] !== 80) ||
       ($protocol === 'https://' && $parts['port'] !== 443))
      ? ':' . $parts['port'] : '';

    // rebuild
    return $protocol . $parts['host'] . $port . $parts['path'] . $query;
  }

  protected function shouldRetainParam($param) {
    foreach (self::$DROP_QUERY_PARAMS as $drop_query_param) {
      if (strpos($param, $drop_query_param.'=') === 0) {
        return false;
      }
    }

    return true;
  }

   protected function throwAPIException($result) {
    $e = new FacebookApiException($result);

   ?>

CRON.php

<?php
require_once("src/facebook.php");
include("custom.php");

set_time_limit(0);

$config = array();
$config['array'] = $appID;
$config['secret'] = $appSecret;
$facebook = new Facebook($config);

$day = abs(date("j"));
$month = abs(date("n"));

$result = mysql_query("SELECT uid, uid2, name2 FROM birthdays WHERE birthmonth = '$month' AND birthday = '$day'");
while(($row = mysql_fetch_assoc($result)) && mysql_num_rows($result)) {
    $link = $hostURL.'post.php?uid='.$row['uid'].'&uid2='.$row['uid2'];
    $facebook->api('/'.$row['uid'].'/feed', 'POST',
        array(
            'link' => $link,
            'from' => '299185790135651',
            'picture' => $hostURL.'image.php?id='.$row['uid2'],
            'name' => 'Send Cake',
            'message' => 'It\'s '.$row['name2'].'\'s birthday today! Send them a virtual cake!',
            'caption' => 'Sponsored by Intercake Ltd'
        ));
}

?>

also... what is 'from' => '299185790135651', ?

want to check my developer has put the right number here. Thanks

回答1:

The best way to handle this is to use a try...catch statement. As follows:

try {
   // some code that calls Facebook
} catch ( Exception $e ) {
   // $e will contain the error - do what you want with it here
   // e.g. log it or send an email alert etc.
}

The 'from' => '299185790135651' is a User / Page ID that publishes the message to the Feed. In this case, it's pointing to a Test Facebook Page.