Using facebook SDK for php for an FQL query? - facebook

I would like to know how to use the following fql query to get a single message from my facebook inbox using facebook sdk for php (or another way if it's possible )
https://graph.facebook.com//fql?q=SELECT+body+FROM+message+WHERE++mssage_id="XXXXXXXXXXXXXXXXX_XXX"&access_token=...
(returns a JSON result)
OR
https://api.facebook.com/method/fql.query?query=SELECT+body+FROM+message+WHERE+message_id="XXXXXXXXXXXXXXXXX_XXX"&access_token=...
(returns an XML result)
both methods need an access token for a read_mailbox permission. All i need is to get the result using php. Any help ?
Thanks.

The most flexible way is to use the api method of the PHP SDK. The Facebook documentation site has examples of this.
If you already have an access token, this code will get you the data into a php array:
<?php
$query = 'SELECT body FROM message WHERE message_id = "?????????????_??"';
$token = '????';
$result = json_decode( file_get_contents('https://graph.facebook.com/fql?q=' .
urlencode($query) . '&access_token=' . $token) );
printf('<pre>%s</pre>', print_r($result, true) );
Yes you need an access token for this. If you're trying to read the mailbox of a random user, you need an app, and you'll have to follow one of the authentication workflows to get them to log into your app and give you the read_mailbox permission.

Related

Getting "The permission publish_actions are not available" when I am using publish_pages

I have been trying to get to post on my facebook page using Facebook php sdk, and I got the error "(#200) The permission(s) publish_actions are not available", the weird thing is that I am asking for the "pages_show_list, manage_pages, publish_pages and user_photos" permissions instead. I have been trying to look for the cause but I can't find anything.
This is how I am asking for them:
$loginUrl = $helper->getLoginUrl(
'http://localhost:8080/fbTest/fb-callback.php',
array('scope' => 'pages_show_list, manage_pages, publish_pages, user_photos'));
and this is my post:
$res = $fb->post($pageID . '/photos/', $data, $accessToken);
$post = $res->getGraphObject();
I am pretty new to this things and I've been reading the documentation as much as I can, but I can't find a solution. My app is not published just yet, I am still testing things with it and I have used both my admin account and a test user I made...(For the record, I managed to make the post using javascript sdk and I have no problem there, I made the post). Any idea on what could be the problem? I am pretty much using the example code facebook provides for the login and most the things I am using here. Thanks!
Solution:
Got the solution to my case, as the mighty WizKid said, I was using a user token and not a page token.
So to get my page access token I used the following code:
$accessToken = $helper->getAccessToken();
$longLivedToken = $fb->getOAuth2Client()->getLongLivedAccessToken($accessToken);
$fb->setDefaultAccessToken($longLivedToken);
$response = $fb->sendRequest('GET', $pageID, ['fields' => 'access_token']) -
>getDecodedBody();
$foreverPageAccessToken = $response['access_token'];
$fb->setDefaultAccessToken($foreverPageAccessToken);
Then I made my request like this:
// POST request.
$res = $fb->post($pageID . '/photos/', $data, $foreverPageAccessToken);
$post = $res->getGraphObject();
It worked just fine for me! Source I found on how to get the page access token: https://github.com/facebook/php-graph-sdk/issues/785

How do I remove the Facebook login prompt when displaying images pulled with graph api on a page?

I recently started playing around with Facebook graph API and wanted to integrate images pulled from my Facebook page to display on my website. I have the images displaying properly but my problem is whenever anyone tries to view the page they are asked to log into Facebook first. Is there any way to display the images without prompting the user to log into Facebook?
Here is what I am using to make the session:
$app_id = 'id';
$app_secret = 'secret';
$redirect = 'my webpage';
// init app with app id and secret
FacebookSession::setDefaultApplication($app_id,$app_secret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper($redirect);
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
Any help would be appreciated.
Step one: familiarize yourself with the API. Read the docs, and play around with the Graph API Explorer
Step two: the code you provided has nothing to do with what you are trying to achieve (displaying page photos). That code is basically the getting started code used in the docs. If you need help, post the relevant code.
Step three: as mentioned by #CBroe, authenticating the visitor is not needed to display photos from a page. What you might want to explore:
The page admin authenticating your app with the right permissions (maybe manage_pages)
with the user access token you just got, you extend it to long-lived one
then you query the API to get a page access token that won't expire
you store this access token and query the API to get the relevant data and store it (GET /{PAGE-ID}/photos or GET /{PAGE-ID}/albums ... etc)
you show the stored data to your visitors
Notes:
Do not make these calls on client-side ... i.e. reveal your page access token, since you can do this in the backend.
Use the realtime updates to get notified when you should query the API and get new photos instead of periodically querying the API to pull new photos, or even worst, querying the API on each user visit.

Using Phonegap's Facebook access token on the server-side

I have Phonegap's Facebook plugin working perfectly within my Android app. This plugin uses Facebook's client-side Javascript flow. However, I would like to upload photos to FB from the server-side, instead of client-side, as this is much (much) more efficient.
Is it possible to use Facebook's access token generated from client-side on the server-side? (BTW, I have Facebook's PHP SDK working perfectly on stand alone web pages.)
I am getting a "(#200) Permissions Error" trying to pull this off.
If the client-side token cannot be used on the server, any other suggestions? I do not know of a way to generate a server-side Facebook token from a Phonegap app.
Edit:
It appears that you actually can use a client side Facebook token on the server. It's simple matter of upgrading to the latest Facebook PHP SDK, and using the $facebook->setAccessToken() function with a token sent from Javascript.
An access token is valid via client or server. On facebook end it is the same thing: a request through HTTP.
If you are not able to upload photos, check that you have the appropriate user permissions for your application.
If you are trying to figure out what graph api calls you need, a good starting place is the javascript console:
https://developers.facebook.com/tools/console/
Here you can see in the javascript what calls are being made in order to read fromt he api, or make a feed story or upload. You will use the same graph URLS and variables using server side code.
Yes you can use client side token in server side.Actually when user is logged into facebook ,fb create an access token in cookies ,which you can access on both client as well as server and when user is logged out ,cookie is destroyed and access token becomes invalid.
And to upload the photos to facebook i used following code :
// $friends is an array of friend id to be tagged in photo
// $x and $y are the position of tags.
for($i=0;$i<sizeof($friends);$i++)
{
$tags[] = array(
'tag_uid'=>$friends[$i],
'x' => $x[$i],
'y' => $y[$i],
);
}
$photo_details = array(
'access_token'=>$access_token ,
'message'=> 'Any message',
'tags' => $tags
);
$file=$photo_name; //Example image file
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);

Having Trouble Accessing Email from Facebook Oauth

I can't get the proper token to reach the extended user information (specifically email) from the Facebook API. I've correctly given my app permission to retrieve the email, so I know I have the correct permissions. I'm trying to convert a session to a token using the exchange_session method as you can see below in my code:
$url = 'https://graph.facebook.com/oauth/exchange_sessions';
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,'type=client_cred&client_id=' . FB_APPID . '&client_secret=' . FB_SECRET . '&sessions=' . $session->session_key);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$tokendata = json_decode($buffer);
$user = json_decode(file_get_contents('https://graph.facebook.com/me/?wrap_access_token=' . $tokendata[0]->access_token));
print_r($user);
If I use the '/me' it always errors out with
"An active access token must be used to query information about the current user."
If I swap out '/me' with the current user ID, it will give me the basic user info, but not the extended info.
The token that I get back is indeed the full token with session info in this format:
123456789012345|6.abcdefghijklmnopqrstuv__.1234.1234567890-1234567890|abcdefghijklmnopqrstuvwxyza.
Help?
Thanks!
Andy
Should it be /me/?access_token= instead of /me/?wrap_access_token=?
Use official facebook's php sdk to avoid the headache with tokens handling

How to get if a user is admin of a page (isAdmin) using the Facebook Graph API?

Using the old REST API, you could do call https://api.facebook.com/method/pages.isAdmin to know if a user was the admin of a given page.
How can you do that using the Graph API? I can't find anything about it in the documentation and start to wonder if it's even possible.
You may be able to view that info in the accounts connection
$session_key = $_REQUEST['fb_sig_session_key'];//maybe there is a better way to get this
$access_token = $facebook->getAccessToken();
$is_admin = $facebook->api(array('method'=>'pages.isadmin', 'page_id'=>'YOUR_PAGE_ID', 'session_key'=>$session_key, 'access_token'=>$access_token));
Another option would be to run FQL on page_admin table.
You can try this:
$page=$facebook->api('/[PAGE ID]?fields=access_token');
if (!$page['access_token'])
echo "is not admin";
From FB docs: GET /v2.2/{page-id}/roles/{user-id}
Also see How can i get the admins user id of a page in facebook.?.