Facebook Php API: RSVP users to public event - facebook

I'm trying to use the facebook php api to rsvp users to a public event.
So far I can only get it to work for users who have already been invited.
I've tried:
$path = $event_id.'/attending';
$method = 'POST';
try
{
$this->facebook->api($path, $method);
}
catch(FacebookApiException $e){}
Which does nothing.
My code as it stands is:
function rsvpEvent($event_id)
{
$fb_config = array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
);
$this->load->library('facebook', $fb_config);
$me = $this->facebook->api('/me/');
$user_id = $me['id'];
$path = $event_id.'/invited/'.$user_id;
$status = $this->facebook->api($path, 'GET');
$status = $status['data'][0]['rsvp_status'];
if($status === 'not_replied')
{
$path = $event_id.'/attending';
$method = 'POST';
try
{
$this->facebook->api($path, $method);
}
catch(FacebookApiException $e){}
}
}
Has anyone got any ideas how I can get this to work?

$path = $event_id.'/attending?access_token='.$SOME_ACCESS_TOKEN;
You're not using the access token in your code, so I am uncertain of whether you skipped it when you copied it, If it's not in the code, then it may be that.
I retrieved the information here

Related

Facebook OAuth Issue with CakePHP 2.9

Hi Guys first of all let me tell you guys that I am a newbie with CakePHP and MVC also but one of my old client requested me some favor which I took as a challenge. So If anyone can help me I will appreciate a lot.
I have a CakePHP 2.9 application where OAuth is not working properly following is the code for app/Controller/UsersController.php
public function facebookLogin(){
$this->layout = false;
App::import('Vendor', 'Facebook/facebook');
/*$facebook = new Facebook(array(
'appId' => '1867587306812489', // Facebook App ID
'secret' => '9fb41a07d3da3406dbab5861c3498764', // Facebook App Secret
'cookie' => false,
)
); */
$facebook = new Facebook(array(
'appId' => '1887387108164606', // Facebook App ID
'secret' => '8d733aa6a4501ce27fde9626429baab9', // Facebook App Secret
'cookie' => false,
)
);
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=first_name,last_name,email');
$usersArr = $this->User->find('first',array('conditions'=>array('User.email'=>$user_profile['email'],'User.social_id'=>$user_profile['id'],'User.role_id'=>0)));
$user_count = $this->User->find('first',array('conditions'=>array(),'order'=>'User.id DESC','limit'=>1));
if(empty($usersArr)){
$saveUserArr['User']['username'] = trim($user_profile['first_name']).' '.trim($user_profile['last_name']);
$saveUserArr['User']['firstname'] = $user_profile['first_name'];
$saveUserArr['User']['lastname'] = $user_profile['last_name'];
/* $user_n = $saveUserArr['User']['first_name'].'.'.$saveUserArr['User']['last_name'];
$user_cnt = $user_count['User']['id'] + 1;
$user_n = str_replace(' ','_',$user_n).'.'.$user_cnt;
$saveUserArr['User']['user_unique_id'] = $user_n; */
$saveUserArr['User']['email'] = $user_profile['email'];
$saveUserArr['User']['password'] = '123456';
$saveUserArr['User']['social_id'] = $user_profile['id'];
$saveUserArr['User']['login_from'] = 'facebook';
$saveUserArr['User']['role_id'] = 0;
$this->User->save($saveUserArr['User'],false);
}else{
$this->User->id = $usersArr['User']['id'];
$this->User->saveField('social_id', $user_profile['id']);
$this->User->saveField('login_from', 'facebook');
}
$this->request->data['User']['email'] = $user_profile['email'];
$this->request->data['User']['login_from'] = 'facebook';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
return $this->redirect($loginUrl);
}
}
private function deleteFacebookSession(){
Configure::write('debug', 0);
if(!$this->Session->check('Auth.User')){
foreach( $this->Session->read() as $key => $value ) {
if( strpos( $key, 'fb_' ) === 0 ) {
$this->Session->delete($key);
}
}
}
}
It throws following error:
The oauth script url is not properly configured in your fb app.
You may go to your fb app settings and look for APP DOMAIN.
Include your domain there:
If you are developing in your localhost, you may want to create a server.local rec in your hosts file so you can access your localserver as server.local.
Also, you can create test versions of the main fb app, and allows you to have a copy of your fb app that works in other environments, it is called "create test app"

Codeigniter login with facebook, getUser always return 0

I searched here and in the web for my problem, but i didn't found any soluation, this make me crazy, i don't know whats the proplem.
the propble is, getUser() function always return 0.
here the my library:
include(APPPATH.'libraries/facebook.php');
class Fbconnect extends Facebook{
public $user_id = null;
public $user = null;
function Fbconnect(){
$ci =& get_instance();
$ci->load->config('facebook',true);
$config = $ci->config->item('facebook');
parent::__construct($config);
$this->user_id = $this->getUser();
$me = null;
if($this->user_id){
try{
$me = $this->api('/me');
$this->user = $me;
}catch(FacebookApiException $e){
error_log($e);
}
}
}
}
and this is the controller
function facebookRequest(){
$this->load->library('fbconnect');
$data = array(
'redirect_uri' => base_url().'home/handleFacebookRequest',
'scope' => 'email'
);
redirect($this->fbconnect->getLoginUrl($data));
}
function handleFacebookRequest(){
$this->load->library('fbconnect');
if($this->fbconnect->user){
echo '<pre>';
print_r($this->fbconnect->user);
}else{
echo "Login operation faild, try again later";
}
}
and i used a config file to store the appId and secrect
$config = array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxx',
'cookie' => true
);
In your plugin APPPATH.'libraries/facebook.php' (that i believe you downloaded from facebook) there is a function errorLog($msg), this function logs the errors in php error log (or apache error log rather) If you don't want to hassle, with rewrting that to CI error logging, you can just echo that $msg and see if there are any errors. Debug accordingly.
If you haven't changed ANYTHING in the core files you downloaded, my bet is that your curl request in function makeRequest(args[..]) needs a CURLOPT_SSL_VERIFYPEER set to false, or rather this line: $opts[CURLOPT_SSL_VERIFYPEER] = false; needs to be added

Facebook connect works fine but troubles inside Facebook iframe

I actually tried to find a solution but nothing and I hope I will find a solution here !
I use facebook connect in my website, everything works fine
when I am logged on facebook, I am automatically logged into my site through facebook connect, and vice versa.
Now i tried to make it a facebook application but I have the following problem:
I try to run inside the facebook iframe but unfortunately I'm not automatically logged in. sometimes it works, sometimes nothing happens , Sometimes I get this message: (Could not get the Facebook session. Your server may not Be Able Securely Facebook to connect to the user to retrieve information.)
some code im using :
include 'includes/facebook.php';
$facebook = new Facebook(array(
'appId' => $setting['facebook_appid'],
'secret' => $setting['facebook_secret'],
'cookie' => true, // enable optional cookie support
));
if ($facebook->getUser()) {
$facebook_session = 1;
try {
$fb_user = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie($setting['facebook_appid'], $setting['facebook_secret']);
$open = #file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']);
if ($open != FALSE) {
$fbdata = json_decode($open);
$fb_user = array();
foreach($fbdata as $key => $fbdata2) {
$fb_user[$key] = $fbdata2;
}
}
}
else {
$facebook_session = 0;
}
ps: im using of course a Secure Canvas URL
Please help !
Thanks in advance
My Facebook Connect login worked fine, but not inside the Facebook Canvas/iFrame it didn't work at all. I had to change my login method (now that facebook users have to be able to autologin in your application) and what basically worked for me was this: How to authorize Facebook app using redirect in canvas?
This is what I had before (I use CakePHP):
$params = array(
'scope' => 'email'
);
$loginUrl = $facebook->getLoginUrl($params);
$this->redirect($loginUrl);
And I changed it to this which worked quite well:
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'publish_stream'
)
);
echo '<script>top.location="'.$loginUrl.'";</script>';
I'm not sure if that really helps you as your app seems a bit more complicated than mine, but maybe you can solve your issue with this answer or the post I linked to.

Facebook photo tagging app not tagging people

I have a facebook photo tagging app that used to work a few months ago but no longer does. The user authenticates the app and it uploads a picture on their facebook account and tags their friends. The app uploads the picture but stopped tagging for some reason.
require_once('facebook.php');
$_SESSION['init'] = true; $current_date=date('m/d/Y'); $facebook =
new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$facebook->setFileUploadSupport(true);
$session = $facebook->getSession(); $tokenorig =
$facebook->getAccessToken();
$friends =
file_get_contents("https://graph.facebook.com/me/friends?access_token="
. $tokenorig);
$friends = json_decode($friends, true);
$friends = $friends['data'];
foreach($friends as $friend) {
$uids[] = $friend['id'];
}
function makeTagArray($userId) {
$x=1; $y=1;
foreach($userId as $id) {
$tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y);
$x+=1;
$y+=1;
}
$tags = json_encode($tags);
return $tags;
}
$arguments = array(
'message' => 'hi guys ',
'tags' => makeTagArray($uids),
'source' => '#' .realpath('pic2.jpg'),
);
$alb = "13378";
uploadPhoto(
$facebook,
$alb,
$arguments,
$tokenorig);
function uploadPhoto($facebook,$albId,$arguments,$tokenorig) {
//https://graph.facebook.com/me/photos
try {
$fbUpload =
$facebook->api('/'.$albId.'/photos?access_token='.$tokenorig,'post',
$arguments);
return $fbUpload;
} catch(FacebookApiException $e) {
echo "eror";
echo $e;
// var_dump($e);
return false;
}
}
//////////end
First off all,
You need user's to authorize your application to upload photos and tag on behalf of them and
you need publish_stream and user_photos permissions to tag user's friends

How do I check if a given user ID has authenticated my app?

I'm using the Facebook Graph API and want to check if a user has authenticated my Facebook app by user ID. How do I do this?
You use:
SELECT is_app_user FROM user WHERE uid=USER_ID
This should return:
[
{
"is_app_user": true
}
]
If the user has logged in to your application.
Expanding on ifaour's answer, in PHP this query would look something like this:
<?php
$facebook = new Facebook(
'appID' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET
);
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT is_app_user FROM user WHERE uid=$user_id"
));
$is_installed = $result[0]['is_app_user'];
Here you can batch multiple requests together and avoid using FQL.
Assuming you have already logged into facebook and set the access token to the application access token, you can do this:
$batch = array();
foreach($friendArray AS $friend) {
$batch[] = array(
'method' => 'GET',
'relative_url' => '/' . $friend . '?fields=installed'
);
}
FB()->useApplicationAccessToken();
$batchResponse = FB()->facebook()->api('?batch='.json_encode($batch), 'POST');
Then you can process the batch response with code like this:
$installedUsers = array();
$notInstalledUsers = array();
foreach ($batchResponse AS $response) {
$body = json_decode($response['body'], true);
if (!isset($body['id']))
continue;
$id = $body['id'];
if (isset($body['installed']))
$installedUsers[] = $id;
else
$notInstalledUsers[] = $id;
}