Facebook login showing not defined index: email only for gmail users - facebook

I've made App and facebook login for my site. But I've tested it and it works for another type of mail. I tried now with gmail and it's showing:
ErrorException [ Notice ]: Undefined index: email
My method for that is:
public function action_fbLogin(){
$facebook = new Facebook(array(
'appId' => 'MyAppId',
'secret' => 'MySecret',
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me', array('fields' => 'id,email,name,first_name,last_name,picture'));
$user_id = Model_UserFunctions::checkIfUserExist($user_profile['email']);
if($user_id > 0)
{
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
exit;
}
$values = array(
'email' => $user_profile['email'],
'username' => $user_profile['email'],
'password' => '12345678'
);
$user = ORM::factory ( 'User' );
$user->values($values);
try
{
if($user->save()){
$user_new_id = $user->as_array();
$user_new_id = $user_new_id['id'];
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_new_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
}
}
catch (ORM_Validation_Exception $e)
{
$result = $e->errors('models');
echo '<pre>';
print_r($result);
exit;
}
}
else
{
$this->redirect($facebook->getLoginUrl(array('id,email,name,first_name,last_name,picture')));
}
exit;
}
Are there other things that I should add to my code for gmail users? Thank you in advance!
Edited: my method for logut is:
public function action_logout(){
Session::instance()->delete('user');
$this->redirect('/');
}
Maybe problem is that it doesn't logout. I'm not sure. It shows me the same user data although I'm logged with different user.

Related

Unable to get getConfigurationAndPricing in PromoStandard

I am trying to getting Product getConfigurationAndPricing using Promostandard Soap API but for some reason I am unable to get those information. Here is my code snapit
$productId = 'TM17907'
$client = new \SoapClient("https://pspriceconfig100.pcna.online/psPriceConfig.svc?singleWsdl", array('trace' => 1));
$options = array(
'wsVersion' => '1.0.0',
'id' => $apiUsername,
'password' => $apiPassword,
'productId' => $productId,
'currency' => 'USD',
'fobId' => '1',
'priceType' => 'List',
'localizationCountry'=>'US',
'localizationLanguage' => 'en',
'configurationType' => 'Decorated',
'cache_wsdl' => WSDL_CACHE_MEMORY
);
try{
$result = $client->__soapCall('getConfigurationAndPricing',[$options]);
} catch (\Exception $e){
$output->writeln("<error>HTTP issue generate in product ID : ".$productId.", Row No.".$currentRow."</error>");
continue;
}
I am using to get PCNA products.

Error 100 schedule a post on Facebook Page

I've used the following code. It works fine without 'scheduled_publish_time', otherwise I get this error "(#100) You cannot specify a scheduled publish time on a published post".
I've previously registered my app with another piece of code. It's so weird.
include_once("inc/facebook.php"); //include facebook SDK
$appId = '21xxxxxxxxxxx'; //Facebook App ID
$appSecret = '6b8f4bxxxxxxxxxxxxxd56'; // Facebook App Secret
$return_url = 'http://localhost:8888/...'; //return url (url to script)
$homeurl = 'http://localhost:8888/...'; //return to home
$fbPermissions = 'publish_stream,manage_pages'; //Required facebook permissions
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true,
'fileUpload' => true
));
$accounts = $facebook->api('/me/accounts');
$PAGE_ID = get_option('fb_post_cron_page'); // it is an option saved in WordPress
foreach($accounts['data'] as $account){
if($account['id'] == $PAGE_ID){
$ACCESS_TOKEN = $account['access_token'];
}
}
$post_url = '/'.$PAGE_ID.'/photos';
$upload_dir = wp_upload_dir();
$upload_dir= $upload_dir['path'];
$timezone= 'Europe/Rome';
$date = new DateTime($dateStr, new DateTimeZone($timezone));
//posts message on page statues
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => true,
'scheduled_publish_time' => $date->getTimestamp()
);
try {
$postResult = $facebook->api($post_url, 'post', $args );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
you have to set 'published' to false
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => false,
'scheduled_publish_time' => $date->getTimestamp()
);

FuelPHP Sentry Insert Group on Regstration

I can't really find anything about this in the documentation.
I used Codeigniter a lot, and I used Ben Edmunds Ion auth mostly, and that was a bit easier for me, because there I was able to add a group insert function in the registration.
You can down vote this stupid question, but I can't find anything about this in the Sentry docs.
Here is my register code:
public function action_register()
{
$response = Response::forge();
// validation
$val = Validation::forge('registration');
$val->add_field('first_name', 'Username', 'required|trim|valid_string[alpha,spaces]');
//$val->add_field('first_name', 'Vezetéknév', 'required|');
$val->add_field('last_name', 'Keresztév', 'required');
$val->add_field('email', 'Email', 'required|valid_email');
$val->add_field('email_again', 'Email újra', 'required|valid_email|match_field[email]');
$val->add_field('password', 'Jelszó', 'required|min_length[5]');
$val->add_field('gender', 'Nemed', 'required');
$val->add_field('talent', 'Tehetséged', 'required');
$val->add_field('talent_level', 'Tehetségi szinted', 'required');
if($val->run()):
try
{
// register the user
$user = Sentry::user()->register(array(
'email' => Input::post('email'),
'password' => Input::post('password'),
'metadata' => array(
'first_name' => Input::post('first_name'),
'last_name' => Input::post('last_name'),
'gender' => Input::post('gender'),
'talent_level' => Input::post('talent_level'),
)
));
if($user):
//$email_data = array();
//echo Config::get('base_url'). "user/activate/". $user['hash'];
$email = Email::forge();
$email->from('my#email.me', Config::get('site_name'));
$email->to(Input::post('email'), Input::post('first_name'));
$email->subject('Regisztráció');
$email_data = array(
'name' => "Kedves " . Input::post('first_name'). "<br><br>",
'title' => "Üdvözöllek a ".Config::get('site_name')." oldalán" ."<br>",
'link' => 'Fiókod megerősítéséhez kérlek kattints ide'
);
$email->html_body(\View::forge('email/activation', array('email_data' => $email_data)));
$email->send();
$response->body(json_encode(array(
'status' => 'ok',
)));
else:
$data['errors'] = "error";
endif;
}
catch (SentryUserException $e)
{
$response->body(json_encode(array(
'status' => 'error',
'message' => array(
'email_taken' => $e->getMessage()
)
)));
}
else:
$response->body(json_encode(array(
'status' => 'error',
'message' => array(
'first_name' => $val->error('first_name') ? $val->error('first_name')->get_message() : null,
'last_name' => $val->error('last_name') ? $val->error('last_name')->get_message() : null,
'email' => $val->error('email') ? $val->error('email')->get_message() : null,
'email_again' => $val->error('email_again') ? $val->error('email_again')->get_message() : null,
'password' => $val->error('password') ? $val->error('password')->get_message() : null,
'gender' => $val->error('gender') ? $val->error('gender')->get_message() : null,
'talent' => $val->error('talent') ? $val->error('talent')->get_message() : null,
'talent_level' => $val->error('talent_level') ? $val->error('talent_level')->get_message() : null,
)
)));
endif;
return $response;
}
So my question is how can I make it save the user group selected?
Thank you.
This is taken from the Sentry Documentation; I think this is what you are after:
try
{
// option 1
$user->add_to_group(2);
// option 2
$user->add_to_group('editor');
}
catch (SentryUserException $e)
{
$errors = $e->getMessage(); // catch errors such as user already in group
}

Can't get Auth login to work with CakePHP 2.0

I'm trying to get a simple login form to work using CakePHP 2.0... just Auth, no ACLs for now.
I'm able to see the form and enter the email and password as they are in the database, but I just get returned to the form and the flash error message is displayed. Here is my code:
AppController:
class AppController extends Controller
{
function beforeFilter()
{
$this->Auth->userModel = 'Users';
$this->Auth->fields = array('username' => 'email', 'password' => 'password'); //have to put both, even if we're just changing one
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'hotels', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
}
}
login.ctp:
<?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
UsersController:
class UsersController extends AppController
{
var $name = 'Users';
var $helpers = array('Html','Form');
var $components = array('Auth','Session');
function beforeFilter()
{
$this->Auth->allow("logout");
parent::beforeFilter();
}
function index() { } //Redirects to login()
function login()
{
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
} else
{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
function logout()
{
$this->redirect($this->Auth->logout());
}
}
?>
I appreciate any help with this. Thanks!
The "Invalid username or password, try again" error is displayed after you hit login?
There are a few things you should check:
• Is the output of $this->Auth->login() identical to the information in your database? Put debug($this->Auth->login()) to see the output in your login method after the form is submitted.
• Are the passwords correctly hashed in the database?
• Try making the AuthComponent available to all your controllers not just the UsersController.
• Not sure if this makes a difference, but call parent::beforeFilter(); before anything else in your controller's beforeFilter method.
EDIT:
Is see that you're trying to validate based on email and password. As a default AuthComponent expects a username and password. You have to explicitly state that you want the email and password to be validated by $this->Auth->login(). This comes from the 2.0 documentation:
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
The fact that you're not seeing any SQL output is to be expected, I believe.
Also you must check if your field "password" in database is set to VARCHAR 50.
It happens to me that I was truncating the hashed password in DB and Auth never happened.
if you are not using defalut "username", "password" to auth, you cant get login
e.g., you use "email"
you should edit component declaration in your controller containing your login function:
$component = array('Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'mot_de_passe')
)
)
));
Becareful with cakephp's conventions. You should change this "$this->Auth->userModel = 'Users';" to "$this->Auth->userModel = 'User';" because User without plural is the Model's convention in cake. That worked for me and also becareful with the capital letters. it almost drived me crazy. Good luck.
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'Events',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'Users',
'action' => 'login',
'home'
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username','password' => 'password')
)
)
)
);
Editing the component declaration in AppController did the trick for me. If you have the fields named other than "username" and "password" you should always specify them. In your case it would be
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username' => 'email','password' => 'password')
)
)
)
);
There is a bug in the cakephp tutorial.
$this->Auth->login() should be changed to
$this->Auth->login($this->request->data)

Facebook OAuthException: Error validating application

I have a simple PHP page that is going to be used to post a message to my own wall.
I've obtained an offline_access token that has "read_stream" and "publish_stream" permissions.
define('FB_APIKEY', 'MY_APP_KEY');
define('FB_SECRET', 'MY_APP_SECRET');
define('FB_SESSION', 'MY_OFFLINE_TOKEN');
require_once('facebook.php');
try {
$facebook = new Facebook(FB_APIKEY, FB_SECRET);
$facebook->api_client->session_key = FB_SESSION;
$attachment = array(
'message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'google.com'
))
);
$result = $facebook->api('/me/feed?access_token=' . FB_SESSION,
'post',
$attachment);
var_dump($result);
} catch(Exception $e) {
echo $e;
}
When I run this, I get "OAuthException: Error validating application".
I confirmed that my offline token is good. When I go to https://graph.facebook.com/me?access_token=[MY_OFFLINE_TOKEN], it returns my public profile in JSON format correctly.
So, I'm thinking I'm doing something wrong with the API call somewhere but for the life of me I can't seem to figure it out. I've been wrestling with this issue for the last two days. Could someone please help! :(
You don't have APP ID here. And make calls like this:
require_once('facebook.php');
$facebook = new Facebook( array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true
));
try {
$attachment = array(
'message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'google.com'
))
);
$attachment['access_token'] = $offline_access_token; // add it to the array
$result = $facebook->api('/me/feed', 'POST', $attachment );
var_dump($result);
} catch(Exception $e) {
// error_log( $e ); // should use something like this when in production
echo $e;
}
Untested, but should work. Let me know if it doesn't.