I am having a wierd problem, things that worked before stopped working today, maybe it was bad before but now after the oAUTH 2 change, I am having troubles with a near production app
this is what I try
$params = array('method'=>'fql.query','query' => 'SELECT uid2 FROM friend WHERE uid1 = me()');
$result = $facebook->api($params);
I get:
Exception: 104: Requires valid signature
or more elaborated :
$config = array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
if ($uid){
try {
$access_t = $facebook->getAccessToken();
$fql = 'SELECT uid2 FROM friend WHERE uid1 = '.$uid;
$params = array('method' => 'fql.query', 'query' => 'SELECT uid2 FROM friend WHERE uid1 = '.$uid);
$result = $facebook->api($params);
echo $result;
$friends = $facebook->api(array('method' => 'fql.query', 'query' => $fql, 'access_token' => $access_t));
var_dump($friends);
} catch (FacebookApiException $e) {
echo $e;
}
this is the code I am using to validate the user and get the login info and permissions allowed:
$canvas_base_url = "https://apps.facebook.com/myapp/index.php?from=allow";
$params = array('scope' => 'publish_stream,email,offline_access,user_status,friends_status,friends_photos,user_photos,xmpp_login,user_online_presence,friends_online_presence',
'redirect_uri' => $canvas_base_url
);
$loginUrl = $facebook->getLoginUrl($params);
what am I doing wrong ?
This means the access token you are using is invalid. It has probably expired.
Here's a quote from the docs at http://developers.facebook.com/docs/authentication/:
In addition to the access token (the access_token parameter), the
response contains the number of seconds until the token expires (the
expires parameter). Once the token expires, you will need to re-run
the steps above to generate a new code and access_token, although if
the user has already authorized your app, they will not be prompted to
do so again. If your app needs an access token with an infinite expiry
time (perhaps to take actions on the user's behalf after they are not
using your app), you can request the offline_access permission.
So you should re-run the steps to generate an access token, or require the offline_access permission.
Related
I am trying to post a status update on the wall of a group which I am a member of. Here is the code I am using
<?php
require 'facebook-php-sdk/src/facebook.php';
$appId = 'xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxx';
$extended_access_token = 'xxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $appSecret));
$msg_body = array(
'message' => 'Good evening',
'type' => 'status',
'access_token' => $extended_access_token,
);
$groups = array(
'Group name' => '1234567',
);
foreach($groups as $group_name => $group_id){
try {
$post_url = "/$id/feed";
$postResult = $facebook->api($post_url, 'post', $msg_body );
print_r($postResult);
} catch (FacebookApiException $e) {
echo $e;
}
}
?>
If I login to fb via browser and hit this page in a new tab, the message is getting posted to the group wall. But If I am not logged into facebook, then if I hit this page, no message is getting posted and I am getting an error message
OAuthException: (#200) The user hasn't authorized the application to perform this action
How can I post to this group via offline mode? Searched a lot for this, could not find any useful info.
you need to have the permissions from the group owner
Change $id to $group_id and you'll be fine.
you ned to get apps this permession : publish_actions,publish_stream,user_groups,
I am having trouble getting all the facebook posts, message, photos etc... that show up on my wall via the Facebook API. Basically, many items are missing in the result set. I tried the following with FQL:
$facebook = new Facebook(array(
'appId' => FB_APPID,
'secret' => FB_SECRET,
'cookie' => true
));
$fql = "SELECT post_id, actor_id, target_id, message FROM stream WHERE source_id =me() ORDER BY updated_time DESC";
//Create Query
$params = array(
'method' => 'fql.query',
'query' => $fql,
);
//Run Query
$result = $facebook->api($params);
But this feed seems to be missing some items, and i don't know why.
I also tried with reading the graph feed at http://graph.facebook.com/me/feed?access_token=<Access+token> and again, i also don't get all the feed items.
What is the problem? Why am I missing some items?
use facebook->getLoginUrl with Permissions, see https://developers.facebook.com/docs/reference/login/#permissions
$applicationurl = 'http://{yourwebsite}/facebook.php';
// Get User ID
$user = $facebook->getUser();
if(empty($user))
{
$params = array(
'scope' => 'email',
'redirect_uri' => $applicationurl
);
$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}
Use this code before your query.
You can use $user in your query like uid = '.$user.
Set your permissions in the scope part of your request, email in the example above.
EDIT OP says use the scope 'read_stream', and it works
I'm experimenting with Facebook's long-live access token and I found that the long-lived access token is usable in different users. Is this normal?
I'm using the Facebook PHP SDK v3.2.2 and Yii. Here's my code to generate the long-lived access token from App_User_1
Yii::import('application.vendors.facebook.Facebook');
$facebook = new Facebook(
array(
'appId' => Yii::app()->params['facebookApi'],
'secret' => Yii::app()->params['facebookSecretCode'],
'cookie' => true,
)
);
$loginUrl = $facebook->getLoginUrl(array('display' => 'touch', 'scope' => 'publish_actions'));
$fbUser = $facebook->getUser();
if(!empty($fbUser))
{
$facebook->setExtendedAccessToken(); //long-live access_token 60 days
$access_token = $facebook->getAccessToken();
exit(print_r($access_token));
}
Here's my code to test posting to App_User_1's Facebook wall.
$message = "A message";
$link = "http://www.alink.com";
$picture = "http://www.alink/image.png";
$sendTo = "`**App_User_1**`";
$access_token = "xxxxxxxxxx";
Yii::import('application.vendors.facebook.Facebook');
$facebook = new Facebook(
array(
'appId' => Yii::app()->params['facebookApi'],
'secret' => Yii::app()->params['facebookSecretCode'],
'cookie' => true,
)
);
$attachment = array('message' => $message, 'link' => $link, 'picture' => $picture );
$api = "/$sendTo/feed/?access_token='.$access_token,";
$result = $facebook->api($api,'post', $attachment);
I have 2 test app user. If I substitute the App_User_1's access token, I can also post into App_User_2's Facebook Wall. Is this normal?
I think I may have been doing this the wrong way. Using the PHP SDK, the api method does not recognize the access_token as part of its path parameter. (e.g. /me/feed/?access_token=blah)
Instead, the access token should be specified under optional parameters in an array format.
Here's how I got mine to work.
$facebook_uid = '1234567890';
//Initialize array for the params parameter
$fb_data = array();
$fb_data['access_token'] = 'xxxxxxxxxx';
$fb_data['message'] = 'A test message';
$facebook->api('/'.$facebook_uid.'/feed/', 'post', $fb_data);
Tried swapping the access token with other app user's access token and not specifying access_token at all. Both will result in error (#200) The user hasn't authorized the application to perform this action.
I would also recommend using this tool by Facebook to verify that the access_token is generated correctly by your app. https://developers.facebook.com/tools/debug
I'm trying to post an image to a facebook page from a PHP app. I check many resources, the documentation, some demo codes and several questions in this site, but cannot finish a working app.
Here is my code:
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
$fbuser = $facebook->getUser();
$facebook->setFileUploadSupport(true);
$post_url = '/443513955707619/photos';
$msg_body = array(
'source' => '#/www/www.gbart.hu/public_html/facebook/megosztos_app/img/winner/winner_'.(int)$round[0].'.jpg',
//'image' => 'http://www.gbart.hu/facebook/megosztos_app/img/winner/winner_'.(int)$round[0].'.jpg',
'message' => 'http://www.facebook.com/WangMesterKinaiKonyhaja/app_322145727882829',
'access_token' => $access_token
);
try {
$postResult = $facebook->api($post_url, 'post', $msg_body);
}
catch (FacebookApiException $e) {
echo $e->getMessage();
}
Here are my permissions for the app:
$fbPermissions = 'email, publish_actions, publish_stream, photo_upload, manage_pages';
I have the appID, app_secret, access_token parameters as required (the other parts of the app is working).
I got some different error messages, like invalid album id or invalid access tokens. I solved these, and now there isn't any error messages, but the photo does not appear anywhere.
In a previous version of this code I tried to post the image to the page walls not to the album, but that made a weird result: posted the image to my user profile's wall.
Try to post something like below
$url = "https://graph.facebook.com/443513955707619/photos?access_token=".$token."&message=hello&url=http://imagetopost.com/hi.jpg&method=post";
$response = file_get_contents($url);
I want to update the status of a FAN-PAGE by PHP with the help of the Facebook graph api. google says: doesn't work.
Now I want to update my own user status by PHP. My main problem is how to login my own user to the graph api (with PHP), without using a browser and without funny PHP workarounds.
In both cases you need to get publish_stream permission http://developers.facebook.com/docs/authentication/permissions
This can be done with FB.login()
More information: http://developers.facebook.com/docs/authentication
After that you can just update status with graph api post: http://developers.facebook.com/docs/reference/api/post
my main problem is how to login my own
user to the graph api (with php),
without using a browser and without
funny php workarounds.
There's no way for you to act on behalf of a user (even your own user) without interacting with him through a browser at least once to get the offline_access.
How to get the offline_access permission and how to use it from there onward is explained in this answer.
EDIT:
Please read the comments! thanks #zerkms!
You need several things to update your facebook profile or a page's feed: a facebook application (client_id, client_secret), profile_id, and access_token (publish_stream, manage_pages, offline_access permissions)
You need offline_access because if not, then the access token will expire. If you've read that you don't need offline_access if you already have publish_stream specified, they just meant that you don't need it always.
To publish a post is easy:
$data = array(
'access_token' => $access_token,
'message' => 'status message',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/{$profile_id}/feed");
Now how to get the profile_id and access_token, you can use my app post panda, or make your own script. I'll include it here:
# arvin castro
# http://codecri.me/
# January 16, 2011
$client_id = ''; # application id
$client_secret = ''; # application secret
$callbackURL = 'http://'; # the URL of this script
$extendedPermissions = 'publish_stream,manage_pages,offline_access';
session_name('facebookoauth');
session_start();
if(isset($_GET['logout']) and $_SESSION['loggedin']) {
$_SESSION = array();
session_destroy();
}
if(isset($_GET['signin'])) {
# STEP 1: Redirect user to Facebook, to grant permission for our application
$url = 'https://graph.facebook.com/oauth/authorize?' . xhttp::toQueryString(array(
'client_id' => $client_id,
'redirect_uri' => $callbackURL,
'scope' => $extendedPermissions,
));
header("Location: $url", 303);
die();
}
if(isset($_GET['code'])) {
# STEP 2: Exchange the code that we have for an access token
$data = array();
$data['get'] = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $_GET['code'],
'redirect_uri' => $callbackURL,
);
$response = xhttp::fetch('https://graph.facebook.com/oauth/access_token', $data);
if($response['successful']) {
$var = xhttp::toQueryArray($response['body']);
$_SESSION['access_token'] = $var['access_token'];
$_SESSION['loggedin'] = true;
} else {
print_r($response['body']);
}
}
if($_SESSION['loggedin']) {
// Get Profile ID
$data = array();
$data['get'] = array(
'access_token' => $_SESSION['access_token'],
'fields' => 'id,name,accounts',
);
$response = xhttp::fetch('https://graph.facebook.com/me', $data);
echo '<pre>';
print_r(json_decode($response['body'], true));
echo '</pre>';
} else {
echo 'Sign in with Facebook';
}
?>
I'm using my cURL wrapper class, xhttp