I'm wondering if someone would help me troubleshoot my test for stream.publish. I thought I had all the right pieces. Here's the code:
<?php
require_once 'facebook.php';
$appapikey = 'xxxxxxx';
$appsecret = 'xxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();
$message = "Will this status show up and allow me to dominate the world?!";
$uid = $user_id;
echo $uid;
$facebook->api_client->stream_publish($message,$uid);
What I'm expecting is my status to change to $message's content. What happens instead is that my UID is echo'd, and then it throws a 500 error. I've allowed publish_stream as well as offline_access (verified in my app settings, via my profile), the the API key hooks this small bit of code to my app. What other pieces do I need to make this simple example work? I'm finding the FB documentation a little hard to put together.
-- The include is the official PHP Facebook library
stream_publish() takes more than two arguments:
stream_publish($message, $attachment = null,
$action_links = null, $target_id = null,
$uid = null)
Where $target_id is the user or page you're publishing to and $uid is the user or page who is doing the publishing - and which defaults to your session id. To be completely explicit about this, I think you need to try
<?php
require_once 'facebook.php';
$appapikey = 'xxxxxxx';
$appsecret = 'xxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();
$message = "Will this status show up and allow me to dominate the world?!";
echo $user_id;
$facebook->api_client->stream_publish($message,null,null,$user_id,$user_id);
An alternate form might be:
$app_id = 'xxxxxxx';
$facebook->api_client->stream_publish($message,null,null,$user_id,$app_id);
This one works in 2011! I had the same problem. Most of the tuts seem to be out of date thanks to Facebook changes. I eventually found a way that worked and did a quick blog article about it here:
http://facebookanswers.co.uk/?p=214
There's also a screen shot to show you what the result is. Make sure you also see the blog post about authentication though.
Remove the $uid variable as it is not needed for publishing. Refer to this wiki entry for more info
$stream_post_id = $facebook->api_client->stream_publish($message);
//returns $post_id to use if you want to revert the creation.
If you are trying to use streamPublish with an iFrame application, here is an awesome step-by-step tutorial that doesn't have to use getAppPermissions:
http://thetechnicalexperience.blogspot.com/2010/02/how-to-use-fbconnectstreampublish.html
Related
IDEA: Get event information without user interaction from various facebook pages (clubs,bars,etc..). However some pages have set that info not to be public.
WORKING PRINCIPLE: I have the latest facebook PHP-SDK on my site and an APP on facebook, so usually i call
$facebook = new Facebook(array(
'appId' => '387262991341732',
'secret' => '09014d999f6e34d80ca3e62e331834cc',
));
$events = $facebook->api("$page_url/events");
PROBLEM:
When a page is not public I have to use an access_token.
1) This is an APP access-token, which is not permitted to view the events.
$app_token = $facebook->getAccessToken();
So I figure I need to get user access_token and add more code:
if (!$user) {
$args['scope'] = 'offline_access';`<br>`
$loginUrl = $facebook->getLoginUrl($args);`<br>`
}
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>`
I click the link by myself and use getAccessToken() to get the user access_token:
$user_token = $facebook->getAccessToken();// I store this in my database
I check this with:
file_get_contents('http://graph.facebook.com/privatepage/events?access_token=$user_token');
// Everything works, im very happy
2) Now I delete all cookies, etc and try to use $facebook->setAccessToken($user_token) and then $events = $facebook->api("$page_url/events"); and get nothing. I assure you one more time that AccessToken is valid.
After quite a long research im stuck with this.
???) *Any Ideas how use $facebook->api(...) and not file_get_contents?
I am working on a script to pull recent Facebook posts/status updates for a specific using the Facebook API. I downloaded the FB SDK and searched all over the FB developers site, SO, and other places and it seems like the code below should accomplish what I need. However, although I am not getting an error, I am getting a NULL result.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));
$fbApiGetPosts = $facebook->api('***My Name***/feed?limit=5');
var_dump($fbApiGetPosts["data"]);
I have tried using my two FB accounts,(my personal acct and a test account that I created just for this project) as well as one for my company and each time the response was NULL. When I misspelled the account name or used a name that did not exist I got an error, which seems to suggest the request is going somewhere.
In the code above, I also tried var_dump-ing $fbApiGetPosts and that result was also null
Can anybody see what the missing piece of the puzzle is?
I've not used the Facebook SDK, but here is how I retrieve Facebook posts:
$appId = 'XXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$posts = json_decode(
file_get_contents('https://graph.facebook.com/' . $appId . '/feed?
access_token=' . $token
)
);
var_dump($posts->data);
Hopefully this helps!
EDIT:
The token is just called an "Access Token". Here are the instructions for obtaining one. You might find the Access Token Tool helpful, but it's more for debugging existing tokens then creating new ones.
Just use the PHP SDK :
download link : https://developers.facebook.com/docs/php/gettingstarted/
Log your app :
$facebook = new Facebook(array('appId' => 'XXXXX','secret' => 'XXXXX'));
$facebook->setAccessToken('stored access token');
then :
$fbApiGetPosts = $facebook->api('/me/feed');
print_r($fbApiGetPosts);
Here is the Graph doc : https://developers.facebook.com/docs/graph-api/reference/
and the /feed ref : https://developers.facebook.com/docs/graph-api/reference/user/feed/
I am trying to use Facebook integration with Graph API
I registered with FaceBook, got my appID and Secret, downloaded the facebook-php-sdk-master and used this code:
require_once("facebook-php-sdk-master/src/facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$user = $facebook->getUser();
echo "user = ".$user;
Now when I try to use this code it always prints user = 0.
Have i Missed anything ?
ps: I have authorized the app too.
PPS: Could this because I am trying to test my code from localhost and facebook needs a website name for this to run ?
Thanks
You need to make sure that you have authorized the application. Just because you created the app does not mean the app knows who you are or can access your data.
Here is more info about logging in: https://developers.facebook.com/docs/technical-guides/login/
You first need to login and authorize the app.
I'm creating a web app that will use Twitter API's and OAuth so that my app can post to my users twitter accounts.
Here is where I'm at so far - I get to the twitter authorization page.
Authorize the app to be able to post to my twitter account, which sends me to my callback file.
Get the oath_token and oauth_verifier info.
That's where I'm stuck. I CAN NOT seem to get the access token. Here is my callback code:
include 'db.php';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$token = $Twitter->getAccessToken();
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
I've been researching and testing for about 5 hours and still nothing. I have tried to echo $token and it just comes out empty.
Am I missing something big here? seems like an easy task..? Thank you so much for any help :))))
Try this:
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$oauth_verifier = $_GET['oauth_verifier'];
$token = $Twitter->getAccessToken(array('oauth_verifier' => $oauth_verifier));
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
$creds = $Twitter->get('/account/verify_credentials.json');
Hope it helps.
I have an iPhone app which creates a facebook session and I would like to restore this session on my server to hand off some of the work. I have the iPhone app working perfectly fine, it's just that I am having problems restoring the session - the documentation is lacking, at best (from http://wiki.developers.facebook.com/index.php/Facebook_Connect_for_iPhone -"If you want to call the API from your servers, you just need to get the sessionKey and sessionSecret properties from the session and send them back to your servers", that's it).
I think I have a decent start from what docs I have found, and my php page looks like:
require_once 'facebook.php';
$appapikey = 'key';
$appsecret = 'secret';
$userid = 'id';
$sessionKey = 'key';
$facebook = new Facebook($appapikey, $appsecret);
$facebook->set_user($userid,$sessionKey);
However, when I try to login to this page I get the following error:
Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Session key invalid or no longer valid'
I know that the session is valid because I am still logged in on my iPhone app. Does anybody know how to restore a session that was started on Facebook Connect?
Thank you
i spent a lot of time to find answer but found it already:
$this->facebook = new Facebook($appapikey, $appsecret);
$this->facebook->set_user($fb_id, $sessionKey, null, $sessionSecret);
just post sessionSecret with sessionKey to your server and use it with set_user API method
works perfect for me :)