I use facebook sdk api with symfony, in the below $user_profile array he can retrieve every information but email and birthday.
require 'facebook.php';
$app_id = sfConfig::get('app_facebook_app_id');
$app_secret = sfConfig::get('app_facebook_secret_key');
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$location= $user_profile['location']['name'];
$gender = $user_profile['gender'];
$user_info = array(
'uid' => $user_profile['id'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
'email' => $user_profile['email'],
'birthday' => $user_profile['birthday'],
'gender' => $user_profile['gender'],
);
when I use print_r($user_profile) it return:
Array (
[uid] => 100004246655890
[first_name] => Ala
[last_name] => Hamad
[email] =>
[birthday] =>
[gender] => male
)
the email and birthday empty.
Nowhere in your code does it appear that you're requesting the Permission needed to access the user's email address or birthday
Check the permissions doc and make sure you're requesting the email and user_birthday permissions in your Authentication code.
If you don't receive these information, one of the cause is because the user configuration prohibits sharing them and Facebook don't send the warns.
Because some users, in the configuration, prohibits sharing the email or other information.
I searched for how to write that in code, then I found that I have to request email permission within the scope in the login url:
$params = array(
'redirect_uri' => url_for('sfGuardAuth/loginWithFB', true),'scope' => 'email,user_birthday,user_location'
);
$loginUrl = $facebook->getLoginUrl($params);
Related
Have been trying for weeks, but my App won't ask for extended permissions, obviously that means that it wont POST to the timeline afterwards either.
require_once('/wp-content/php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxx',
));
$user = $facebook->getUser();
if ($userID) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.theaandrdepartment.com/raw',
'message' => 'I just helped an Aussie band make it to
radio play! By voting, you can too.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else { $login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_stream'
));
header("Location: ".$login_url);
}
If anyone could help that would amazing.
I copied most of this code straight from the Facebook example, but to no avail.
My Code
require 'src/facebook.php';
$app_id = 'My App Id';
$app_secret = 'My App Secret Id';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
$user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}
if ($user) {
$params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_photos'
));
}
What I require
The above code only provides friends id and name but not birthdays. I have done some researching and was unsuccessful in finding a solution.
You probably need to request permission of the user to access the birthday information of their friends.
The Facebook permission you are looking for is 'friends_birthday'.
Request the user their access token with the above permission and set the access token.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
)
);
$facebook->setAccessToken($userAccessToken);
$user = $facebook->getUser();
if ($user) {
$user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}
if ($user) {
$params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_photos, friends_birthday'
));
}
Actually I think you just have to add friends_birthday to the scope.
I would like to know how to get the date of birthday of the user of facebook.
Here is the code which deal with facebook api :
<?php require 'src/facebook.php';
define("FB_APP_ID","***");
define("FB_SECRET","***");
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET,
'cookie' => true,
));
$currentUser = $facebook->getUser();
if($currentUser) {
try
{
$facebook_profile = $facebook->api('/me');
$friends_fields = $facebook->api('/me/friends');
}
catch (FacebookApiException $e)
{
print_r($e);
$user = null;
}
}
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'user_birthday, birthday, date_birthday, email, offline_access, publish_stream'
)
);
$logoutUrl = $facebook->getLogoutUrl();
?>
I tried $facebook_profile['user_birthday'] and $facebook_profile['birthday']
But it does not work.
I don't know how to do ? Can you help me please ?
The correct permission for birthday is user_birthday
Verify you have the permission
$permissions = $facebook->api('/me/permissions');
$permissions["data"][0]["user_birthday"];
Seeing that you have facebook_profile, you should just dump the contents of that response to see what you do have.
Also ensure the user did set his/her birthday...
There's probably more than one way to do it, but you should be able to get the user's birthday from an FQL call via the SDK api() method:
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT birthday_date FROM user WHERE uid = me()',
));
$birthday = $result[0]['birthday_date'];
You'll need to have user_birthday or friends_birthday permissions to read the birthday and birthday_date fields.
I have a script I'd like to run via a cron job:
<?
require_once 'fb_access.php';
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '********';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
However when I try to run it via the cron job, it doesn't work. I've figured out this has something to do with the offline_access token. But from what I've read, a user must manually login to get the token. As I'd like to run this via a cron job, that's not feasible. Any ideas?
UPDATE:
I tried this; is it completely wrong?
require_once 'fb_access.php';
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
if ($user) {
try {
$page_id = '**********';
$args = array(
'access_token' => $token,
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
UPDATE #2:
<?php
require_once 'fb_access.php';
$user = $facebook->getUser();
$token = '*******LONG_ACCESS_TOKEN*******';
$page_id = '**********';
$args = array(
'access_token' => $token,
'message' => 'This is the message',
'link' => 'http://www.thisisthelink.com',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
?>
Using this I get: Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in /base_facebook.php on line 1106
First of all, you do not need offline_access, if you're requiring publish_stream permission.
You need manage_pages permission if you want to login/manage/share as your page.
You need user access token in order to get page access token. You need to authenticate once manually
header("Location: $facebook->getLoginUrl(array("scope" => "publish_stream,manage_pages")));
After you get an access token, save it to the persistent place (file, for example)
When in need, get the access token from your file and use it (just add access_token to your arguments)
Following code worked to me:
<?php
include(dirname(__FILE__)."/fb/facebook.php");
define('PAGE_ID', '123456498798');
$facebook = new Facebook(array(
'appId' => '178645249555182',
'secret' => 'csdf64sd65f4sd6f54f1c',
));
$a = array(
'access_token' => 'werf564s6d1cr98f965d6gf49w8sd49f87w9ed5c16d5f49s8f74w9e8rf74',
'message' => 'This is the message',
'caption' => 'This is the caption',
'description' => 'This is the description'
);
print_r($facebook->api('/'.PAGE_ID.'/feed', 'post', $a));
If it's a cron job, there's no-one there to do authorization with. You cannot do what you want. You'll need to look for an alternative.
An alternative like taking a valid auth token from one of the page admins that you've requested the extended token for (offline_access is being quickly deprecated see https://developers.facebook.com/docs/offline-access-deprecation/).
This 60 day token will need to be put into some config that the cron job can access.
You will need to manually update the 60 day token every couple of months.
You can get valid access tokens from the https://developers.facebook.com/tools/explorer.
I am doing a registration on my website via facebook.
When the user logs in via facebook the $user array returned is not exactly what i want.
I have gone through the user parameters that are accessible via facebook, i have tried implementing them also but it is not working.
This is a sample of what i have
require_once "Database_Connect.php";
if (!isset($_POST['choosepassword']))
{
# We require the library
$user=array();
require("facebook.php");
# my error tracker
$error=0;
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
$user = $facebook->api('/me');
} catch (Exception $e){}
if(!empty($user)){
# User info ok? Let's print it (Here we will be adding the login and registering routines)
print_r($user);
***At this point what is retrieved is not exactly what i want*****
$ue=$user['email'];$ui=$user['id'];
$query = mysql_query("select * from members where email = '$ue' or (oauth_provider = 'facebook' AND oauth_uid = '$ui')", $link);
$result = mysql_fetch_array($query);
# If not, let's add it to the database
if(!empty($result)){
$error = 2; //record already in database
require_once "facebook_error.php";
die();
}
} else {
# For testing purposes, if there was an error, let's kill the script
$error = 1; //we were unable to retrieve info frm facebook
require_once "facebook_error.php";
die();
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
*** I tried specifying what i want returned here, but it doesnt seem to work*****
$url = $facebook->getLoginUrl(array(
'req_perms' => 'uid, first_name, last_name, name, email, current_location, user_website, user_likes, user_interests, user_birthday, pic_big',
'next' => 'http://www.zzzzzzz.com/facebook_register.php',
'cancel_url' => 'http://www.zzzzzzz.com'
));
header("Location: ".$login_url);
}
What am i not doing right?
Thank You
Update
I am using FQL to select user info from facebook now,
$fql = "select uid, first_name, last_name, name, sex, email, current_location, website, interests, birthday, pic_big from user where uid=me()";
$param = array('method' => 'fql.query', 'query' => $fql, 'callback' => '');
$user = $facebook->api($param);
It retrieves all the data except the birthday and the email
How can i select the email and birthday?
Thanks
Your application needs the user_birthday and email permissions for this, else it will not return that information. You only need to supply parameters that need a permission, not what fields you want in the req_perms parameter, so it should look like this:
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email, user_birthday',
'next' => 'http://www.zzzzzzz.com/facebook_register.php',
'cancel_url' => 'http://www.zzzzzzz.com'
));