Problems with facebook php sdk v4 - php

I am trying to get facebook php sdk v4 to work with my app. I am using Drupal and an nginx server with phpfpm.
When I try to get the session from facebook sdk, I get the error - Missing redirect uri parameter. While debugging, I found 2 strange things are happening -
1) If I copy and paste the get url(generated by the sdk) in my browser, a valid access token is returned.
2) If I change the method for retrieving the access token from GET(default) to POST in the sdk, a valid access token is returned to my app.
Also, when using the access token I received from POST call, if I make further GET requests to the graph api, none of them work.
However, if I make a POST request, such as posting to my wall, it works.
Also it should be noted that the sdk is internally using curl to make all requests.
Edit :
Code Example -
To generate login url
$helper = new \Facebook\FacebookRedirectLoginHelper($this->redirectUrl);
$loginUrl = $helper->getLoginUrl(array('public_profile', 'email', 'user_friends', 'publish_actions'));
Now the person is taken to facebook for authentication and redirected to my redirect url. Here the code is -
$helper = new \Facebook\FacebookRedirectLoginHelper($this->redirectUrl);
$session = helper->getSessionFromRedirect();
Since getSessionFromRedirect internally uses a GET call, it doesn't work for me. When I change the sdk code to use POST, a valid access token is returned.
Also when I get a valid session using a POST request, this works
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
But this doesn't work -
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
Clearly there seems to be an issue with all the GET requests. Any hints on what can be the problem?

Solved the problem finally. Turns out drupal is changing the & in url for GET requests to & .
Related to this .

Related

How to use facebook access_token

I´m building a facebook app with php,
everything works perfect, I do successful dialog auth
I have the short_live token
I generate the long_live_token and save it to some directory
what I want to do is that in canvas app the user selects some stuff and activates a mechanism that regularly posts stuff, this is why I save the token.
but what can I do with it?!
I find a lot about generating the access_token but nothing about how to use it!?
Where can I add it as parameter? What is the key?
example:
I´m using facebook sdk for php for post sth. to a wall like
$msg_body = array(
'message' => "wassup yo"
);
$facebook->api($uri, 'post', $msg_body );
but this only works if
$facebook->getUser();
is returning a user
how can I use my stored access_token to do the same?
I believe there is a function called "setAccessToken" in the Facebook PHP SDK. You would just need to set it with that function and it gets added to every call automatically.
Manual way:
$params = array(
'message' => 'wassup yo',
'access_token' => '[your-token]'
);
$facebook->api($uri, 'post', $params);
You could also do this with CURL, this would be an example URL;
$url = 'https://graph.facebook.com/' . $userId .
'/feed' .
'&access_token=' . $accessToken .
'&message=' . $userMessage;
Basically you just add the Access Token as a parameter like the message.
Just make sure you are using secure calls, see this article for an example of using CURL with the Facebook API and usage of "appsecrect_proof": http://www.devils-heaven.com/extended-page-access-tokens-curl/
IMPORTANT: Be sure that the message parameter is always 100% user generated without any prefilling (see Platform Policy) and keep in mind that you need to go through a review process with pulish_actions to make it available for other Users: https://developers.facebook.com/docs/apps/changelog

Share post via facebook php sdk version 4.0.9, Graph API v2.0

Using facebook php sdk version 4.0.9 & Graph API v2.0
Here is my application settings for publishing post.
My code is :
$requestjojo = new FacebookRequest($session, 'POST', '/me/feed', array('message' => 'testing'));
$responsejojo = $requestjojo->execute();
// get response
$graphObjectjojo = $responsejojo->getGraphObject()->asArray();
// print Graph data
echo '<pre>' . print_r( $graphObjectjojo, 1 ) . '</pre>';
This is not working for me, I want to know where's the bug.
Note : I am getting user data(user profile) properly from this sdk.
Using echo 'Login';
for login button, when login with Facebook, Facebook is not asking me for this extended permission(publish_stream) too.
The loginUrl is not build up properly.
$helper->getLoginUrl(array(
'scope' => 'publish_stream',
'redirect_uri' => $your_redirect_uri
));
Finally, got the answer.
The reason is that "publish_stream" is now deprecated; use "publish_actions" instead.
From :
Error (#200) The user hasn't authorized the application to perform this action

Facebook Graph API - Issues with friends that have installed the app

I have an android client, that initiate log-in to facebook, receives access token and other details about the profile.
The android client passes the access_token and details to the Server (PHP).
Both have facebook sdk installed.
When I initiate a FacebookRequest from the PHP, for example '/me/' It's working.
BUT when I initiate a (friends who have installed the APP) FacebookRequest from the PHP
'/me/friends'. I get "null".
When I use the graph explorer provided by facebook the result is :
{
"data": [
]
}
Additional information for the helpers:
The app contains only two users at the moment, which are friends on facebook.
Those users are both administrators of the app.
Currently the app is not live\published.
We haven't "Start a Submission" in Stats and Reviews as suggested somewhere.
We asked for the permission 'user_friends'.
Since everything in stack overflow requires reputation,
This is how the permissions look like:
http://i.stack.imgur.com/kURwB.png
Thanks
EDIT:
OK I added two test users, Made them friends of each other, and /me/friends through graph explorer is working for them.
BUT why doesn't it work for non-test-users?
Okay,
So apparently Facebook had a problem providing new Access Tokens.
Don't know why, but it provided the same access token, for new Log-Ins.
Now that they fixed that here is a Handy function that I wrote in PHP that will let users
Parse Facebook responses, this might come in hand since there is NOTHING explained over the Docs, Google, StackOverFlow.
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
function find_my_facebook_friends($access_token){
require_once 'facebook-php-sdk-v4-4.0-dev/autoload.php';
FacebookSession::setDefaultApplication('app_key','secret_key');
$session = new FacebookSession($access_token);
$request = new FacebookRequest(
$session,
'GET',
'/me/friends'
);
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
return $graphObject;
}
Usage:
include_once 'path/to/function.php';
//You wanna use it, just so the PHP file contains your app_key, secret_key Wont be exposed.
$access_token = isset($_POST['access_token']) ? $_POST['access_token'] : NULL;
$facebook_object = find_my_facebook_friends($access_token);
for($i = 0 ; $i < count($facebook_object['data']) ; $i++){
$id = get_object_vars($facebook_object['data'][$i]);
echo $id['id'];
echo $id['name'];
//You don't have to echo, you could handle those fields as you wanted.
}
This function refers to a Mobile Client passing his Log In User's access token to the server.
I hope this will save you lots of time!

offline fb posting not working

I have to post on a users wall when he is offline. I went through number of posting and checked the code written below seems ok and also not throwing error and complete successful yet I am unable to see posting on users wall. Pl help.
$facebook = new Facebook(
array(
'appId' => "446262598768110",
'secret' => "<REMOVED>",
)
);
$facebook->setAccessToken("<REMOVED>");
//create message with token gained before
echo " lets try posting";
$access_token = $facebook->getAccessToken();
$user_id = $facebook->getUser();
echo "\ndone3..... $access_token.-----.$user_id.----new\n";
$post = array(
'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
);
//and make the request
$res = $facebook->api("/me/feed", 'GET', $post); //have tried both Post and Get method but result remain same
offline access was removed quite a few months ago. also, you do have to "POST" to facebook graph, everytime you want to publish something.
without seeing the result of the facebook graph, it's kind of impossible to help you at all.
nevertheless: double check, that you're using the correct access token. you have to extend the short lived access tokens by your own (https://developers.facebook.com/docs/howtos/login/extending-tokens/), so that the access token itself is still valid when you want to post by cron-job.
if you're trying to post by using the app access token, you can't use "/me/feed" at all.

How do I post to a Facebook timeline?

I have an app which uses the Facebook PHP SDK. It used to be a simple matter to post to a user's Facebook wall. I would do the authentication, and then something like:
$ret_obj = $Facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
and on their wall the link would appear. Since Timeline was introduced, however, this doesn't seem to work. I don't get an error, I just don't get anything back. I can't find any helpful documentation on how this should work.
More info
I don't get any error, I just get "NULL" returned in $ret_obj.
If I do something like
$ret_obj = $Facebook->api('/me', 'GET');
then it works fine - I get the user's information back. So it's connecting ok, just not posting.
Double check that you have allowed publish_stream permission.
You can refer to PHP SDK and JavaScript SDK Feed Dialog for more examples.
Official Documentation: http://developers.facebook.com/docs/reference/dialogs/feed/

Categories