Wednesday, May 13, 2009

Trouble with Facebook Client API

I have working on facebook application development (it's a game named Fighter Jets), and facing some weired problem. I have decided to share my experience here.

Today I tried the post link feature of the facebook API. First I tried the following code:
$facebook->api_client->links_post($user_id, 'http://khaaan.com/','Best. Website. Ever.');
I got it from their API documentation.

I got error using it. It says that 'The url you supplied is invalid'. May be it's invalid. Then I changed the URL to http://www.google.com, which is valid definitely! Still getting the same problem!! WTF!

Then I searched their forum and found that I am not the only victim, some people already faced this problem and one of them solved the problem in the following way:
$facebook->api_client->links_post('http://khaaan.com/','Best. Website. Ever.', $user_id);

Which works! Needed to use the user id as the third parameter, but from the example in the API documentation we see that it's the first parameter.

I really don't understand why the documentation is wrong.

10 comments:

Unknown said...

The API doc is probably wrong because that function was labeled "beta", and they switched the order of the parameters in the real API code.

Would you mind posting the whole code here, not just the function call?

I am also having trouble posting a link to my page, even with the right order of parameters.

Thanks,
Robert

Tamim Shahriar said...

@Robert, I think you need the permission first to post link to profile. Please check this link: http://wiki.developers.facebook.com/index.php/Extended_permission

Unknown said...

I already granted offline_access and share_item permissions in my account, using these links:

http://www.facebook.com/authorize.php?api_key=b9af2acb12229b7162c9cf39bf9f02a7&ext_perm=offline_access

http://www.facebook.com/authorize.php?api_key=b9af2acb12229b7162c9cf39bf9f02a7&ext_perm=share_item

Tried deleting the app, adding again and granting permissions again. Sometimes this worked, and I could get past the 'Managing shared items requires the extended permission share_item' error.

Then, I had problems with 'Session key invalid or no longer valid', even though links_post() does not specifically require sessions. I saw this error mostly under FF, not IE.

After various tries, I sometimes end up without any errors, and links_post() works, but doesn't return any link ID, and the link is not shown on my Facebook account.

This is basically the code I use:

$facebook = new Facebook($apiKey, $secret);
$link_id = $facebook->api_client->links_post('http://khaaan.com/','Best. Website. Ever.',$userId);
var_dump($link_id);

The best result I could get was only an empty string from var_dump() :(

I was hoping that from your code I could get some insight. Did your posted link appear on your profile?

Thanks and regards,
Robert

Tamim Shahriar said...

Yes, my posted link appears on my profile. Unfortunately I can't share the whole code as I wrote it for the company where I work. Hope you understand.

Basically the code is:
include_once 'facebook.php';

$facebook = new Facebook($apiKey, $secretKey);
$facebook->require_frame();

$user = $facebook->require_login();
$facebook->api_client->links_post('http://www.google.com','Best. Website. Ever.', $user);

Unknown said...

Thanks for the sample code. That's pretty much what I have, too. Even with variations I still can't make it work properly. I think I need a whole new approach to this...

Unknown said...

Hi to All.
Although, you all overcome the problem with the links_post. I am still facing the same problem - "Fatal error: Call to undefined method FacebookRestClient::links_post() in...".
My code is as following:
$url = "http://fliplop.com/";
$txt = "Fliplop - nice site to surf!";
$facebook->api_client->links_post($url, $txt, $target_ids);
Can someone advice me?
Thanks,
Doron

Tamim Shahriar said...

@Doron, please make sure that you are using the latest API from facebook (you can download it).

Unknown said...

Hi Subeen,
I am using the updated API. Where do I need to place 'facebook-platform' directory and it's sub-folders in my web server?

Tamim Shahriar said...

@Doron, put those in the same directory you keep you application.

Unknown said...

Hi Subben,
Thanks for your help. I think my problem now is with the extended permission.

my code is as following -
facebook.showPermissionDialog("publish_stream", function(grantedPermission) {
if(grantedPermission == "publish_stream") {
$url = "http://fliplop.com/"; $txt = "Just Flip It!"; $facebook->api_client->links_post($url, $txt, $target_ids);
}
});

I get an error. What is the solution?

Thanks,
Doron