Facebook OAuthException: Error validating application - facebook

I have a simple PHP page that is going to be used to post a message to my own wall.
I've obtained an offline_access token that has "read_stream" and "publish_stream" permissions.
define('FB_APIKEY', 'MY_APP_KEY');
define('FB_SECRET', 'MY_APP_SECRET');
define('FB_SESSION', 'MY_OFFLINE_TOKEN');
require_once('facebook.php');
try {
$facebook = new Facebook(FB_APIKEY, FB_SECRET);
$facebook->api_client->session_key = FB_SESSION;
$attachment = array(
'message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'google.com'
))
);
$result = $facebook->api('/me/feed?access_token=' . FB_SESSION,
'post',
$attachment);
var_dump($result);
} catch(Exception $e) {
echo $e;
}
When I run this, I get "OAuthException: Error validating application".
I confirmed that my offline token is good. When I go to https://graph.facebook.com/me?access_token=[MY_OFFLINE_TOKEN], it returns my public profile in JSON format correctly.
So, I'm thinking I'm doing something wrong with the API call somewhere but for the life of me I can't seem to figure it out. I've been wrestling with this issue for the last two days. Could someone please help! :(

You don't have APP ID here. And make calls like this:
require_once('facebook.php');
$facebook = new Facebook( array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true
));
try {
$attachment = array(
'message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'google.com'
))
);
$attachment['access_token'] = $offline_access_token; // add it to the array
$result = $facebook->api('/me/feed', 'POST', $attachment );
var_dump($result);
} catch(Exception $e) {
// error_log( $e ); // should use something like this when in production
echo $e;
}
Untested, but should work. Let me know if it doesn't.

Related

upload and attach a photo to a facebook note

I am trying to attach a photo to a facebook note with the following code
$attachment = array(
'message' => 'photo caption',
'subject' => 'photo subject',
'id' => $note_id
);
$attachment[basename($file_path)] = '#' . realpath($file_path);
$result = $facebook->api('/notes', 'POST', $attachment);
which generates an error code: Uncaught OAuthException: (#100) Requires extended permission: create_note
note_id is holding a correct note_id which is created above with the following code:
$note = $facebook->api('/notes', 'POST', array(
'access_token' => $token,
'subject' => 'subject',
'message' => 'message'
));
$note_id = $note{'id'};
facebook note api reference https://developers.facebook.com/docs/reference/api/note/ doesn't provide much details on this task. plz help.

User object syntax in status post array

What is the syntax for referencing a specific user object in the status post array with the Facebook API? Below is what I have already.
$status = $facebook->api('/<PAGE_ID>/feed', 'POST', array(
'from' => "id": "<PAGE_ID>", "name": "<PAGE_NAME>",
'title' => 'This is the title',
'message' => 'This is the message',
'description'=> 'This is the description',
'caption' => 'This is the caption',
'link' => 'http://www.thisisthelink.com'
));
Everything works except the FROM field.
/<PAGE_ID>/feed doesn't accept from parameter, only parameters from http://developers.facebook.com/docs/reference/api/page/#posts are available to specify.
See also: http://facebook.stackoverflow.com/q/691425/251311
This works. I was confusing the parameters of a post with that of the feed.
<?
require_once 'fb_access.php';
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '****NOT*PHP*CODE*PLACEHOLDER*FOR*PAGE*ID*DUH****';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>

Facebook API offline access

I have a script I'd like to run via a cron job:
<?
require_once 'fb_access.php';
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '********';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
However when I try to run it via the cron job, it doesn't work. I've figured out this has something to do with the offline_access token. But from what I've read, a user must manually login to get the token. As I'd like to run this via a cron job, that's not feasible. Any ideas?
UPDATE:
I tried this; is it completely wrong?
require_once 'fb_access.php';
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
if ($user) {
try {
$page_id = '**********';
$args = array(
'access_token' => $token,
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
UPDATE #2:
<?php
require_once 'fb_access.php';
$user = $facebook->getUser();
$token = '*******LONG_ACCESS_TOKEN*******';
$page_id = '**********';
$args = array(
'access_token' => $token,
'message' => 'This is the message',
'link' => 'http://www.thisisthelink.com',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
?>
Using this I get: Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in /base_facebook.php on line 1106
First of all, you do not need offline_access, if you're requiring publish_stream permission.
You need manage_pages permission if you want to login/manage/share as your page.
You need user access token in order to get page access token. You need to authenticate once manually
header("Location: $facebook->getLoginUrl(array("scope" => "publish_stream,manage_pages")));
After you get an access token, save it to the persistent place (file, for example)
When in need, get the access token from your file and use it (just add access_token to your arguments)
Following code worked to me:
<?php
include(dirname(__FILE__)."/fb/facebook.php");
define('PAGE_ID', '123456498798');
$facebook = new Facebook(array(
'appId' => '178645249555182',
'secret' => 'csdf64sd65f4sd6f54f1c',
));
$a = array(
'access_token' => 'werf564s6d1cr98f965d6gf49w8sd49f87w9ed5c16d5f49s8f74w9e8rf74',
'message' => 'This is the message',
'caption' => 'This is the caption',
'description' => 'This is the description'
);
print_r($facebook->api('/'.PAGE_ID.'/feed', 'post', $a));
If it's a cron job, there's no-one there to do authorization with. You cannot do what you want. You'll need to look for an alternative.
An alternative like taking a valid auth token from one of the page admins that you've requested the extended token for (offline_access is being quickly deprecated see https://developers.facebook.com/docs/offline-access-deprecation/).
This 60 day token will need to be put into some config that the cron job can access.
You will need to manually update the 60 day token every couple of months.
You can get valid access tokens from the https://developers.facebook.com/tools/explorer.

Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action

I'm trying to post to my facebook group wall but I keep on getting the above error. Can anyone please tell me what I might be doing wrong here? I have checked the Facebook documentation but I still can't seem to fix it myself. My code is as follows:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'perms' => 'offline_access, user_groups, publish_stream',
'cookie' => true
));
$result = $facebook->api(
'/xxxxxxxxxgroupid/feed/',
'post',
array('access_token' => $facebook->getAccessToken(), 'message' => 'Playing around with FB Graph..')
);
I think you need user_groups extended permission.
Use Try before, that sould work fine
try {
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "Your post was successfully posted to UID: $user";
}
catch (FacebookApiException $e) {
$result = $e->getResult();
}

Facebook API access outside of apps.facebook.com

I am attempting to build a website that utilizes facebook auth based setting created through facebook.com/developers
Can I access a user’s wall (to post), multi friend selector and other elements if the website is hosted outside of facebook.com (apps.facebook.com) realm?
Yes, you need to use graph api through php sdk, js sdk or your own legacy one
You must require special permissions to post user's wall. Use a standard api mechanism to get access_token from user. Make sure user accepted the application permissions requirements such as offline_access, publish_stream etc.
public function friend_off()
{
$home_view = 'offline';
$this->load->library('facebook',array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$this->load->database();
$this->db->where('status', 'A');
$db_user = $this->db->get('users');
//$db_user = $this->db->where('access_pass',$val));
$row = $db_user->result();
//print_r($row);
//friends list
$post = array(
'from' => array(
'access_token' => '1302623452|1212291707|Dgz0FpB0wL0n5PYLy2x--5iGRVo',
'name' => 'Syama Varyar',
'id' => '100002653867222'
),
'to' => array(
'name' => 'Vishnu Jayendran',
'id' => '558987909'
),
'caption' => 'Test Caption',
'description' => 'Test Description',
'picture' => 'http://www.najeem.com/bird-2003.gif',
'message' => 'Test Message >> '. date('Y-m-d H:i:s'),
);
$friends = $this->facebook->api('/100002653867222/friends?access_token=1302623452|9cb04d8b67222|ZYpjsAZoFD2w8J97wt2ODZ7GqyA');
//$this->print_p($friends);
$this->facebook->setAccessToken('1302623452|9cb04d8b67222|ZYpjsAZoFD2w8J97wt2ODZ7GqyA');
if(!($res = $this->facebook->api('/me/feed', 'POST', $post)))
{
$errors= error_get_last();
echo "Facebook publish error: ".$errors['type'];
echo "<br />\n".$errors['message'];
}
}