The code below is not working for some reason:
require './facebook.php';
$facebook = new Facebook(array(
'appId' => 'working_app_id',
'secret' => 'working_secret'
));
// Get User ID
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
print $token; // token shows.
$attachment = array
(
'access_token'=>$token,
'message' => 'This is where the message will go.',
'name' => 'Name Text',
'link' => 'http://www.website.com',
'description' => 'Description text.'
);
$result = $facebook->api('/me/feed/','post',$attachment);
exit;
I am getting the following error:
Uncaught OAuthException: An active access token must be used to query information about the current user.
When I print the token, it appears to be a real token. I just have no idea what I could be doing wrong. All I want to do is post a very simple status update to my own page.
I don't know why I can't seem to figure this out. I'm banging my head against the wall on this one because it should just be so simple to post a quick little status update to my wall in Facebook from PHP.
Note: The App Type I created to get my appID and secret was a "Website".
Check out this answer, it appears that you are using the wrong method to get your access token.
Howto use FB Graph to post a message on a feed (wall)
Related
I'm making a facebook app which will show a user's friends, then the user will choose a friend and the app will send that friend an apprequest with PHP.
With that apprequest I also will send the user's id, so that when the receiver plays the app, I will know that it will be thanks to that user.
To get a user's friends I'm using the following code
$friends_array = $facebook->api('/me/friends');
(I know this will only return a user's friends who have already accepted the app's permissions, so I'm using 2 test accounts which are friends)
To send the apprequest
If I use the following
$param = array(
'message' => 'Check out the latest update',
'data' => 'some_data_string',
"to" => $_POST["friend_id"],
"method" => "POST",
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/me/apprequests", "POST", $param);
It is executed, but nothing is received.
If I use the following
$param = array(
'message' => 'Check out the latest update',
'data' => 'some_data_string',
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/".$_POST["friend_id"]."/apprequests", "POST", $param);
I get the following error "Fatal error: Uncaught OAuthException: (#2) Failed to create any app request thrown ..."
The app is public and available to all users.
Do I need maybe to submit the app for any special permissions in order to post an apprequest?
Also if you have any other suggestion to have the desired results I'm referring to at the beginning, please reply.
I make a Facebook app, now we change it from server and I run again the codes to test it, and I found that it works perfectly except for one FQL. I get the user info with ->(/me)
The FLQ works (I tested with Facebook tools). My connection and scope its correct. any idea?
I leave you my fql
$scope = 'email,read_stream';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
$fql = "SELECT type, source_id, share_count, likes, permalink, description, post_id, message, target_id, created_time, attachment
FROM stream
WHERE source_id = me() and type in (46, 80)
order by created_time desc
LIMIT 500";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r($fqlResult);
I wouldn't use FQL personally as there's a reason they depreciate the old API's, but to access feed data of the user you need to have granted access from the API for your application, it should handle access_tokens automatically but when your user logs in with your website you want to be using
$params = array(
'scope' => 'read_stream, friends_likes',
// Access to feed data (read_stream) and whatever else you need
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
Then you can access all the information you want with the api query:
$facebook->api('/me?fields=feed.fields(source,id,shares,likes,link,description,message,created_time)', 'GET');
I highly recommend using the Facebook Graph API Explorer to test API queries and retrieve all the information you need
Hope that helps mate!
In the parameter array, add one more parameter : access_token and give it the valid access token value
I found my problem,
the code works, but I was asking for two specific types.
and type in (46, 80)
When I remove it it appear all the stream :)
Thanks to everyone
I have created an app with no special permissions. With this app I query Facebook events to display on my website. That all works fine.
I now wanted to expand the website with displaying Facebook Notes of my Fan Page. When I try to Implement that, I get the error message:
"Requires user session"
On the FQL-Note API reference they say "any valid access_token if it is public note written by a page." I think I have a valid public token.
require '../src/fb-sdk/src/facebook.php';
$facebooknotes = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
'cookie' => false, // enable optional cookie support
));
$fql_notes = "SELECT uid, note_id, title, content, content_html, created_time, updated_time FROM note WHERE uid = 149018831796518";
$param = array(
'method' => 'fql.query',
'query' => $fql_notes,
'callback' => ''
);
$fql_notesResult = $facebooknotes->api($param);
foreach($fql_notesResult as $keys => $fbnotevalues){ echo "<h2>" . $fbnotevalues['title'] . "</h2>" . $fbnotevalues['content_html']; }
Can someone help?
Regards,
rimshot
FQL does not seem to be able to get a page's notes. The uid field is looking for a user id. My guess is if you got the appropriate access token, you would still end up with an error because your ID does not belong to a page.
To get at the notes of a page, you need to use the graph API:
$fql_notesResult = $facebooknotes->api('/149018831796518/notes', 'GET');
foreach($fql_notesResult as $keys => $fbnotevalues){
echo "<h2>" . $fbnotevalues['subject'] . "</h2>" . $fbnotevalues['message'];
}
I am trying in vain to post to a Facebook fan page wall as admin. I want to post new entries with pictures, link, description etc on my fan page wall. This is integrated into my custom cms so that whenever a story is updated on my website, it automatically posts the same on the fan page wall.
The problem is that whenever I post, the post does not appear as posted by admin. It appears as posts by others on the right side of the timeline.
How do I go around the following code so that it posts as admin on my fan page wall? I been looking at it for two days, checked similar stuff on the web and so, but I still cant seem to crack it. Help will be appreciated.
$page_id = 'xxxxxxxxxx';
$article_link = "http://xyz.com/articles/headlines/title-one/";
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
$og_title = "Sample title";
$og_image = "http://xyz.com/images/articles/919161344090182.jpg";
$msg = "some description of the article";
$feed_array = array(
'access_token' => "$page_access_token",
'message' => "$msg",
'picture' => "$og_image",
'link' => "$article_link",
'name' => "$og_title",
'caption' => "$og_title"
);
try {
$page_post = $facebook->api("/$page_id/feed","post",$feed_array);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
If you want to post as the page - that means that the post will appear as if the page posted an update as it appears here :
Then you will have to use a valid page access token. There is information on how to obtain this kind of token in the Facebook Authentication Documentation. It looks like you already have a token in your code - you can test it using the Facebook Debugger and see if it is still valid.
Another thing you may want to try, instead of appending the token to be one of the parameters of the post array, you could use the setAccessToken() function of the PHP SDK. More details can be found here - https://developers.facebook.com/docs/reference/php/facebook-setAccessToken/
$facebook->setAccessToken($new_access_token);
As a final BTW note, when you specify elements of an array, unless you are wanting to specifically add the quote character, there is no need to wrap the values in quotes.
$feed_array = array(
'access_token' => $page_access_token,
'message' => $msg,
'picture' => $og_image,
...
);
If you want to explicitly add quotes to the values you would have to do something like this -
$feed_array = array(
'string_value' => '"'.$value.'"',
...
);
I'm trying to retrieve profile information regarding users who allowed my app to collect data about them (birthday, location etc.). This works fine when, and only when, the user actually visits my app and authenticates with facebook through token exchange.
However, I want to be able to routinely check if the user location hasn't changed, also if a user adds information about himself on FB, I want to "sync" it with my app, so that the information stays the same.
So I save the users access token, and later on, with a cron job I use the token to get his profile:
$user = mysql_query ... <- get data from mysql
and then:
$user_profile = $facebook->api('/'.$user['uid']);
And it works, of course, but all the data I want from using extended permissions - just doesn't show up!
Is the the curse of the offline_access disabling? Or do I need to use a different way of obtaining such data. I tried adding ?fields=birthday and etc. on the end of the query, but it still doesn't work.
Not sure if this is what you meant by 'offline_access disabling' however Facebook are deprecating offline_access as you can see in the roadmap:
http://developers.facebook.com/roadmap/
You can read more information on the deprecation here:
http://developers.facebook.com/roadmap/offline-access-removal/
Also, are you correctly setting the access token received from the database?
$facebook->setAccessToken($user['access_token']);
Found out how to accomplish this - the access token has to be passed as an additional parameter on the api queries, like such:
$user_profile = $facebook->api('/'.$user['uid'].'', array('access_token' => $session['access_token']));
In case anyone wonders onto this page, here is a way to use saved access tokens, to access full information about the user you want, without having to authenticate:
<?
require "facebook.php";
$facebook = new Facebook(array(
'appId' => your_app_id,
'secret' => your_app_secret
));
// get the saved user details
$user = mysql_fetch_assoc(mysql_query('SELECT * FROM `facebook_tokens` WHERE `id` = "1"'));
$session = array(
'access_token' => $user['access_token'],
'secret' => $user['secret'],
'session_key' => $user['session_key'],
'uid' => $user['uid']
);
$sig = '';
foreach($session as $key => $value) {
$sig .= implode('=', array($key, $value));
}
$session['sig'] = md5($sessionStr.'Your App Secret');
$facebook->setSession($session, false);
$user_profile = $facebook->api('/'.$session['uid'], array('access_token' => $session['access_token']));
echo '<pre>';
print_r($user_profile);
echo '</pre>';
?>