This is the simplest application that can be made:
<?php
include_once "inc/fb/facebook.php";
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'yyy'
));
$user = $facebook->getUser();
var_dump( $facebook->api("/me/likes/") );
?>`
In return I should get an array of pages, that user have already liked. The problem is, that it doesnt work for some users.
Somehow in a process, I have found that error:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in (...)/fb/base_facebook.php on line 1039
I have tryed to force the get token with:
$access_token = $facebook->getAccessToken();
and it gests the token for every user, but still no table about what user likes.
Any help would be appreciated.
The problem was quite simple:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream,user_about_me',
'redirect_uri' => $mySettings->app_url,
));
echo "<script> top.location.href='" . $loginUrl . "'</script>";
I have forgoten about user_likes permission in scope. But what is strange to me - why did it worked for most of the users?
Related
I am trying to write a script that will post an update to a facebook page timeline. I created an app and created a key for the app. I am using the code below:
require_once HOME_PATH . '/include/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'scope' => 'publish_stream',
));
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$attachment = array(
'access_token' => $access_token,
'message' => 'this is my message',
'name' => 'name',
'link' => ROOT_URL . $blog_data['url'] . '.htm',
'caption' => $blog_data['title'],
'description' => $blog_data['title'],
);
if ($image = get_first_image($blog_data['body'])) {
$attachment['picture'] = $image;
}
$facebook->api(FACEBOOK_BLOG_PAGE . '/feed', 'post', $attachment);
i think this key is connected to the app and not to my user, so it sounds like i would have to grant the permission within facebook, but i have seen some other posts telling me that i need to do this in code. The examples aren't very clear.
I know similar questions have been asked but i haven't seen any clear answers yet. Can anyone please clarify this?
If you want to get user_id by this piece of code: $user_id = $facebook->getUser();
The first thing you need, is to login an user through a URL returned by $facebook->getLoginUrl(array('scope' => array('perm', 'perm2'))).
Also, notice, where I put permissions, which I require. Not into the Facebook class constructor, but into the method getLoginUrl.
And lastly, for posting into a page, you need publish_actions permission (not publish_stream).
I have the following code, which are working partly
<?php
require_once 'facebook.php';
$app_id = 'MY_APP_ID';
$app_secret = 'MY_APP_SECRET';
$app_url = 'http://mysite.com/index.php/';
$scope = 'user_about_me';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
$user_profile = $facebook->api('/me');
echo "id: " . $user_profile['id'];
?>
I get the correct id printed out on this page, but on several other pages i go into the if(!$user).
But there is an error in the oauth url:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
I have found similar problems here on stackoverflow, but can't seem to fix it :/
Under my settings I have the same url and $app_url
Website with facebook -> site url -> https://www.facebook.com/pages/myappname/373671679372127/
EDIT: I edit my app_url, so it is now the same as my canvas url under app settings: http://mysite.com/index.php/
Now I don't get an error. I just get a blank window :(
EDIT: Importent to clear cookies once a while when testing. Else some very random errors somewhere
Check if you set the correct URL for "Website with Facebook Login" in the dev settings. This cannot be a page on facebook.com, it has to be a Link to YOUR domain.
Also, i would enclose the last 2 rows of your code in an "else" block. And the JavaScript code is wrong. This would be correct:
print('<script> top.location.href="' . $loginUrl . '";</script>');
(easier to understand with double quotes, you missed a semicolon. even if it´s not always needed, you better use it)
Try this code:
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\';</script>');
}
else {
echo 'User ID: ' . $user;
}
Also, get rid of the Slash after "index.php". That´s not i folder (i hope/guess).
I am using the PHP SDK to handle my Facebook connects. Everything was working fine for some time but my script suddenly stopped working. Here is what happens:
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$user = $facebook->getUser();
if ($user) {
$permissions = $facebook->api('/me/permissions');
} else {
$params = array();
$params['scope'] = 'publish_actions,user_photos';
header('Location: ' . $facebook->getLoginUrl($params));
exit();
}
Returns: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
The error is caused by the line
$permissions = $facebook->api('/me/permissions');
Any suggestions?
Always check exceptions! Here's a quick example: https://developers.facebook.com/docs/reference/php/facebook-api/
$user returns a user ID for the current user, however, that doesn't guarantee that the user didn't log out meanwhile or that his token is still active. So instead of just relying on this because you got a user_id, check for exceptions. You can either ask the user to click Login again or simply redirect the user to the login page automatically if an exception occurs.
why don't you try
$permissions = $facebook->api(
'/me/permissions ',
'GET',
array(
'access_token' => $_SESSION['active']['access_token']
)
);
I try to post on my wall using facebook api, and offline access tokens.
And every time I has one mistake:
Uncaught OAuthException: (#200) The user hasn't authorized the
application to perform this action
Here is my code:
require 'api/facebook.php';
$facebook = new Facebook(array(
'appId' => "app_id",
'secret' => "app_sec",
"cookie" => true,
'fileUpload' => true
));
$facebook->setFileUploadSupport(true);
$access_token = $facebook->getAccessToken();
$user_id = $facebook->getUser();
$result = mysql_query("UPDATE users SET user_id_facebook='".$user_id."' WHERE id='".$myrow2['id']."'",$db);
$result = mysql_query("UPDATE users SET access_token_facebook='".$access_token."' WHERE id='".$myrow2['id']."'",$db);
if($user_id == 0 || $user_id == "")
{
$login_url = $facebook->getLoginUrl(array(
'redirect_uri' => "http://apps.facebook.com/rapid-apps/",
'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos,offline_access"));
echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
exit();
}
$post = array(
'access_token' => $access_token,
'message' => 'This message is posted with access token - '
);
$res = $facebook->api('/me/feed', 'POST', $post);
as of may 2nd the offline_access permission will be deprecated and should not be used anymore
in the meantime you have an option to disable this deprecation through the developers site for your application ( https://developers.facebook.com/apps/330955886953999 )...
for further information about the removal see:
http://developers.facebook.com/roadmap/offline-access-removal/
You need to adjust the permissions in your initial app authorization request sent to the user. By default you only gain read-only access to their basic information.
See http://developers.facebook.com/docs/authentication/permissions/
I am trying to figure out the id of facebook user currently logged in.
I used this code but I'm getting error.
$facebook = new Facebook_php_sdk(array(
'appId' => '226568227411676',
'secret' => '68bdfeb63d01e8122ed0a46d8fd5dfc1'
));
$rr = $facebook->getAccessToken();
$token = array(
'access_token' => $rr
);
$user_profile = $facebook->api('/me','GET',$token);
on this I am getting error saying "Uncaught OAuthException: An active access token must be used to query information about the current user".
This error occured because you trying to access user's information, using Application access token. Details here