I am having trouble using the PHP SDK for authentication. The effect I am trying to get is whene a user visits the site if they are loged in with FB they see "Logout" which loggs them out when clicked but if they are not logged in when they arrive they should see "You need to log in with FB" which loggs them in. The effect I am currently getting is that the site displays the "You need to log in with FB" even if the user is already logged in, whene this is clicked the user is taken to facebook.com with an error message displayed reading "An error occurred. Please try later". Im sure I must be missing something in my code but cant figure out what, I am fairly new to FB development. Please see my code below. Any help much appriciated.
<?php
require_once("facebook.php");
$user = $facebook->getUser();
$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';
$facebook = new Facebook($config);
$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);
session_start();
$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params);
if(!$user)
{
echo "<P>You need to log into FB</p>\n";
}
else
{
echo "<p style=\"margin-bottom:20px;\"><a href=\"{$logoutUrl} \">Logout</p>\n";
}
?>
Try this out
<?php
require_once("facebook.php");
$user = $facebook->getUser();
$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';
$facebook = new Facebook($config);
$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);
$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params);
<?php if ($user) { ?>
<p>Logout</p>
<?php } else { ?>
<p>Click here to View Your Stalker of the Day</p>
<?php } ?>
You can not call $facebook before creating the instance, This code should work:
<?php
session_start();
require_once("facebook.php");
$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';
$facebook = new Facebook($config);
$user = $facebook->getUser();
$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);
$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params);
if(!$user)
{
echo "<P>You need to log into FB</p>\n";
}
else
{
echo "<p style=\"margin-bottom:20px;\"><a href=\"{$logoutUrl} }\">Logout</p>\n";
}
?>
$params = array( 'next' => 'xxx' );
is deprecated SDK, use
$params = array( 'redirect_uri' => 'xxx' );
instead.
Related
I got this error when i try to check using postman... u can see the error below
i got this massage
"code":400,"response":"Failed","message":"Your username is not registered. Please register a new user now!","data":null
public function loginAction()
{
$this->view->disable();
$credentials = $this->request->getJsonRawBody();
$response = new Response();
$username = $credentials->username;
$password = $credentials->password;
// $user = User::findFirst("user_username = '$username'");
$user = User::findFirst([
'conditions' => 'username = :username:',
'bind' => [
'username' => $username
]
]);
if($user !== NULL) {
$checkPassword = $this->security->checkHash($password, $user->password);
if($checkPassword === true) {
$this->session->set('username', [
'id' => $username->id,
'username' => $user->username,
]);
This is my view page
<?php echo $form->dropDownListRow($order,'area_id',Area::model()->getActiveAreaList(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/getdeliveryforarea'),
'update'=>'#pickup_time_slot', //selector to update
'data' => array('area_id' => 'js:this.value',),
)));
?>
<?php echo $form->dropDownListRow($order,'pickup_time_slot',array(),array('prompt'=>'Select time')); ?>
and in my controll the getdeliveryforarea is like
public function actionGetdeliveryforarea()
{
$data=Areatimeslot::model()->findAll('area_id=:area_id',
array(':area_id'=>(int) $_POST['id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
here my dependent dropdown is not working ,from getActiveAreaList i will get the area list in first dropdown and once i choose an area it must show corrosponding time list in second dropdown,i hope someone will help me out,thanks in advance
Use Juqery migrate plugin or try something like,
jQuery.browser = {};
(function () {
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
})();
I am attempting implement Oauth 2.0 for login, but the following code isn't producing a consistent response. why? Email is present "sometimes" but I cannot find anything here that would explain the varied response. The goal here is that I could use the email to locate users in my system.
<?php
// http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
require_once 'sites/all/modules/schoology_core/s_support/JWT.php';
// app info (see google api console for creds)
$google_client_id = '';
$google_secret = '';
$google_redirect_uri = '';
$google_is_auth_response = (isset($_GET['code']) || isset($_GET['error']));
// construct auth url used to redirect user to google login form
$auth_url = 'https://accounts.google.com/o/oauth2/auth?';
$auth_url .= 'client_id=' . $google_client_id . '&';
$auth_url .= 'response_type=code&';
$auth_url .= 'scope=openid%20email&';
$auth_url .= 'redirect_uri=' . $google_redirect_uri . '&';
$auth_url .= 'state=' . uniqid();
$auth_link = '<br>'. $auth_url .'';
// send user to google login form
if(!$google_is_auth_response) {
print $auth_link;
exit;
}
// handle response
if($_GET['error']) {
print 'Oauth2 response error=' . $_GET['error'];
}
else {
// exchange one-time authorization code for access token and ID token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => array(
'code' => $_GET['code'],
'client_id' => $google_client_id,
'client_secret' => $google_secret,
'redirect_uri' => $google_redirect_uri,
'grant_type' => 'authorization_code',
)
));
$response = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// decode response and id token to retrieve user's email
$response = json_decode($response);
$jwt_id_token = JWT::decode($response->id_token, NULL, FALSE);
$response_info = print_r(array(
'auth_code' => $_GET['code'],
'response_code' => $responseCode,
'response_decoded' => $response,
'id_token' => $jwt_id_token,
'email_is_not_preset' => $jwt_id_token->email ? 'Y' : 'N'
), TRUE);
print '<pre>' . $response_info . '</pre>';
}
I am having some trouble with authentication using PHP SDK. I have downloaded "facebook.php" and "base_facebook.php" from github.
Below is the code I am useing but cant figure out where I am going wrong (new to all this).
<?php
require 'facebook.php' ;
$fbconfig['appid' ] = xxx;
$fbconfig['secret'] = "xxxx";
$fbconfig['baseurl'] = "xxx";
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);
$loginUrl = $facebook->getLoginUrl($params);
$logoutUrl = $facebook->getLogoutUrl();
if(!$user)
{
echo "<P>You need to log into FB</p>\n";
exit();
}
else
{
echo "<p style=\"margin-bottom:20px;\"><a href=\"{$logoutUrl}\">Logout</p>\n";
}
?>
Any suggestions much appriciated :)
Based on this site, it looks like you need to explicitly construct your own Facebook object:
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
I had some really complicated code, and now I've made it ridiculously simple, and it doesn't work.
Currently it simply takes me back to the page with the login URL echoed out.
Code is here:
<?php
require 'facebook.php';
// Create our application instance
// (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'sd',
'secret' => 'sda',
));
// Get User ID
$user = $facebook->getUser();
if($user) {
echo $user;
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://www.facebook.com/pages/CharlesTestPage/225802194155435?sk=app_252946408094785','scope'=>'email'));
echo $loginUrl;
}
exit;
?>
Please. I have spent a whole paid work day on this now, and am at the point of crying, not only for myself but for my boss.
Cry.
Edit: OK, the weird thing is, if I have the redirect_uri set to the facebook tab, if it's not authenticated itself, then it constantly redirects in an infinite loop. However, if I remove the redire
This is what's working for me using PHP SDK 3.1.1. Try it and let us know if this works:
include('facebook.php');
session_start();
//facebook application
$config['appid' ] = "YOUR_APP_ID";
$config['secret'] = "YOUR_APP_SECRET";
$config['baseurl'] = "http://example.com/facebookappdirectory";
$config['appbaseurl'] = "http://apps.facebook.com/your-app-name";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $config['appid'],
'secret' => $config['secret'],
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email'
)
);
if ($user) {
try {
//get user basic description
$userInfo = $facebook->api("/$user");
$fb_access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
error_log('APP ERROR: '.$e);
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
if (isset($_GET['code'])){
header("Location: " . $config['appbaseurl']);
exit;
}
Check for
if(isset($_GET['code'])){
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
}
Facebook return userID only after the login and that code.