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;
}
}
?>
Related
I've made App and facebook login for my site. But I've tested it and it works for another type of mail. I tried now with gmail and it's showing:
ErrorException [ Notice ]: Undefined index: email
My method for that is:
public function action_fbLogin(){
$facebook = new Facebook(array(
'appId' => 'MyAppId',
'secret' => 'MySecret',
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me', array('fields' => 'id,email,name,first_name,last_name,picture'));
$user_id = Model_UserFunctions::checkIfUserExist($user_profile['email']);
if($user_id > 0)
{
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
exit;
}
$values = array(
'email' => $user_profile['email'],
'username' => $user_profile['email'],
'password' => '12345678'
);
$user = ORM::factory ( 'User' );
$user->values($values);
try
{
if($user->save()){
$user_new_id = $user->as_array();
$user_new_id = $user_new_id['id'];
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_new_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
}
}
catch (ORM_Validation_Exception $e)
{
$result = $e->errors('models');
echo '<pre>';
print_r($result);
exit;
}
}
else
{
$this->redirect($facebook->getLoginUrl(array('id,email,name,first_name,last_name,picture')));
}
exit;
}
Are there other things that I should add to my code for gmail users? Thank you in advance!
Edited: my method for logut is:
public function action_logout(){
Session::instance()->delete('user');
$this->redirect('/');
}
Maybe problem is that it doesn't logout. I'm not sure. It shows me the same user data although I'm logged with different user.
I have a facebook application that can post (message,link,image) to user wall
The tagline(via status appname) does not appeared when I see the userwall or homepage
This is the code:
// in case of image
$image = "image url"
$attachment = array(
'message' => $message,
'source' => $image,
'url' => $image
);
$photo = true;
// in case of youtube link
$attachment = array(
'message' => $message,
'link' => 'http://www.youtube.com/watch?v=' . $video_id,
'source' => 'http://www.youtube.com/v/' . $video_id,
'description' => $description,
'picture' => 'http://img.youtube.com/vi/' . $video_id . '/0.jpg',
);
//incase of text
$attachment = array(
'message' => $message,
'name' => $title,
);
}
foreach($users as $uid=>$token){
$attachment['access_token'] = $token;
if (isset($photo)) {
$facebook->setFileUploadSupport(true);
$fb_result = $facebook->api('/' . $uid . '/photos', 'post', $attachment);
} else {
$fb_result = $facebook->api('/' . $uid . '/feed/', 'post', $attachment);
}
}
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.
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();
}
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.