Tanvi Kapoor
Tanvi Kapoor

Reputation: 41

Facebook OAuthException: (#1)

I have a few applications which upload image to user profile. A few hours ago all applications were working fine but now when uploading is requested, it gives this error

Fatal error: Uncaught OAuthException: (#1) An unknown error occurred thrown in      applications/fb-sdk/facebook.php on line 543

I'm using the following code to publish image.

$FILE = "images/$image";

$args = array('message' => 'My msg ');
$args['image'] = '@' . realpath($FILE);

$data = $facebook->api('/'.$uid.'/photos', 'post', $args);

Is it because of some policy change or some new feature?

I have all the permissions like upload is set to true and application takes permission to upload file.

P.s: when the application is used 2nd time, it works fine.

Upvotes: 4

Views: 6569

Answers (2)

Fenix Lam
Fenix Lam

Reputation: 386

No, the error is caused by the system cannot get the image file. Facebook will not allow the empty image field appear in the api. So it return Fatal error: Uncaught OAuthException: (#1) --- although it does not relate to the OAuth and OAuthException.

Upvotes: 1

William Fortin
William Fortin

Reputation: 777

You need to verify if the user is logged in AND has the permissions to post on wall. We're going to do that with a TRY/CATCH with a call to the user.

$userId = $facebook -> getUser();

if ($userId) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
      $userId = NULL;
      error_log($e);
  }
}

$app_permissions = array(
  'scope' => 'publish_stream'
  );

$logoutUrl = $facebook->getLogoutUrl();
$loginUrl = $facebook->getLoginUrl($app_permissions);

If the user is not logged in OR has authorized the app, you'll need to redirect him via header redirect or with a link.

if ($userId){
    //Then you can call the facebook api
    $data = $facebook->api('/'.$uid.'/photos', 'post', $args);
    //... ...
}

That's the easiest way i've found.

EDIT : This question on stack has helped me : Facebook PHP SDK Upload Photos

Upvotes: 2

Related Questions