I've been trying all day to figure this out, but apparently it does not work for me.
What i'm looking into doing is ... ask for facebook login, and then add that user to a group ( send an invite ). I've tried to follow the API docs here:
https://developers.facebook.com/docs/reference/api/group/#invite_member
No luck.
My code looks like:
<?php
require "fb/facebook.php";
$access_token = "app_token_here";
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secretBlabla',
'cookie' => true,
'allowSignedRequest' => false,
));
$params = array('scope' => 'read_friendlists,read_insights,user_about_me,user_birthday,user_groups,user_interests,user_likes,user_location,user_relationships,user_website');
$fuser = $facebook->getUser();
if ($fuser) {
try {
$user_profile = $facebook->api("/me");
} catch (FacebookApiException $e) {
error_log($e);
$fuser = null;
}
}
if ($fuser) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($params);
}
$page = "/XXXXXX/members/".$fuser;
$token=$facebook->getAccessToken();
$facebook->api($page, "POST", array("access_token"=>$access_token)); // i've tried with $token as well
Error i get:
PHP Fatal error: Uncaught OAuthException: (#3) Unknown method\n
Any idea what i'm doing wrong ?
Thank you
Related
when first time i try to login I get error message in exception:\
[message] => An active access token must be used to query information about the current user.
The following is my code:
Configure::load('facebook');
$appId = Configure::read('Facebook.appId');
$app_secret = Configure::read('Facebook.secret');
$facebook = new Facebook(array(
'appId' => $appId, // fb app id (mentioned in facebook.php)
'secret' => $app_secret, // fb secret key
'cookie' => true,
'oauth ' => true
));
//======================================
// to get loggedin user details
//======================================
$user = $facebook->getUser();
//$access_token = $facebook->getAccessToken();
if (!empty($user)) {
try {
$user_profile = $facebook->api('/me');
} catch catch (FacebookApiException $e) {
pr($e);
}
}
//===========================
// view
function facebookLogin(detectedTimeZone){
window.open('https://www.facebook.com/dialog/oauth?client_id=<?php echo $appId; ?>&display=popup&redirect_uri='+base_url+'/users/facebook_login/scope=email','','width=600, height=400');
}
<div style="float:left;"><a onclick="facebookLogin(detectedTimeZone);" href="javascript: void(0);" id="fb-auth"><img src="/images/fblogin.png"></a>
</div>
I have user login implemented on my website. This was working good until now. When i try to login with Facebook account i always get 0 from GetUser(). I tryed with different web browser, updated PHP SDK to latest version, but still not working. Any clue ?
$facebook = new Facebook(array(
'appId' => 'my APP ID',
'secret' => 'my Secret',
'cookie' => false, // enable optional cookie support
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$likes = $facebook->api("/me/likes/ID HIDDEN");
if (!isset($_SESSION['prijavljen'])) {
$_SESSION['prijavljen'] = True;
}
if (!empty($likes['data'])) $fblike = true; else $fblike = false;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
//$logoutUrl = $facebook->getLogoutUrl();
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'my URL'));
} else {
$params = array('scope' => 'email,user_birthday,user_education_history,user_hometown,user_likes', 'redirect_uri' => curPageURL());
$loginUrl = $facebook->getLoginUrl($params);
}
Was the PHP version on your server recently updated? You can try this fix and see if getUser() function starts working for you again.
with my app's administrator acount on facebook my app work normally, but with other account I get error: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
I had this problem before with other app (publish text on the user's wall), but fixed after i added
$user = $facebook->getUser(); What's wrong here? I have added offline_access permission... Help me, please if you can, Thank you very much.
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user_profile = $facebook->api('/me','GET');
$accessToken = $facebook->getAccessToken();
# Get User ID
$user = $facebook->getUser();
$facebook->setAccessToken($accessToken);
$facebook->api(456080124457246);
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
In this case it's not even getting to your $facebook->getUser() call -- it's throwing an exception as soon as it reaches this line:
$user_profile = $facebook->api('/me','GET');
$facebook->api() is a bit of a tricky thing to work with because it throws an exception immediately if it doesn't know who "/me" is...even if you try to fix it later.
The trick, I think, is to wrap the entire thing in a try...catch block. Like so:
<?php
require_once('images/facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxx',
));
try {
$user_profile = $facebook->api('/me','GET');
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
} catch (Exception $e) {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
That'll redirect the user to the login url pretty much immediately, then come back with everything in tow. You don't have to set the accessToken with this setup.
This may actually unnecessarily repeat some functionality, but hopefully it's something to start with.
By the way, for what it's worth the offline_access permission is being phased out.
I stopped using "me" keyword to get the logged in user's profile.
instead of $facebook->api('/me','GET'), I changed to $facebook->api('/the facebook ID of the user','GET');
this reduce the need to do second try catch
If you do
if (!empty($user)) {}
That should help...
Before a minute ago after successful login it redirected to site.com/home, but now the link is site.com/home?state={long-code}6&code={long_code}
And this is ugly. Any ideas?
Update
This is the code i use :
session_start();
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '***',
'secret' => '***',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => 'site.con/home'
)
);
$logoutUrl = $facebook->getLogoutUrl();
//session_destroy();
// Show user data
print_r($user_profile);
Update the sdk files you are using.this error comes from this file base_facebook.php this is now rectified in the new file.
Use this link for getting the latest SDK link
I am following this tutoril. Though the user is logged in into facebook, but the session is showing him not logged in(NULL session). Please someone help me out. My code-segment is as follows-
$facebook = new Facebook(
array(
'appId' => $app_config['app_id'],
'secret' => $app_config['app_secret'],
'cookie' => true,)
);
$session = $facebook->getSession();
var_dump($session);
$fbme = null;
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
I had the same situation, changed to iFrame instead of FBML, it worked.