I've been successfully accessing the LinkedIn API through my CodeIngiter application. I moved to a nearly identical server and implemented i18n library and it's stopped working.
After the user authenticates on LinkedIn it returns to the correct URL, but generates a series of errors beginning with Undefined index: oauth_verifier
After using an i18n library my URLs now have two letter language codes in the 1st segment like 'en' or 'br'.
EDIT: This is the Linkedin library I'm using.
I believe this is causing routing issues with the setting of $_REQUEST['oath_verifier']
Any help on this is greatly appreciated.
Excerpt from controller:
class LinkLogin extends MY_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('profile_model');
$this->load->model('generic_model');
include_once (APPPATH.'libraries/Linkedin.php');
}
function index(){
}
function initiate(){
session_start();
$this->load->helper('url');
$config['linkedin_access'] = "***";
$config['linkedin_secret'] = "***";
$config['base_url'] = "http://www.youinapage.com/linklogin/initiate/";
if ($this->uri->segment(4) == 'profile') {
$config['callback_url'] = "http://www.youinapage.com/linklogin/get_profile_linkedin/";
}
if ($this->uri->segment(4) == 'resume') {
$config['callback_url'] = "http://www.youinapage.com/linklogin/get_resume_linkedin/";
}
function get_resume_linkedin() {
session_start();
$this->load->library('format');
$config['linkedin_access'] = "***";
$config['linkedin_secret'] = "***";
$config['base_url'] = "http://www.youinapage.com/linklogin/initiate/";
$config['callback_url'] = "http://www.youinapage.com/linklogin/get_resume_linkedin/";
# First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url'] );
//$linkedin->debug = true;
# First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url'] );
//$linkedin->debug = true;
if (isset($_REQUEST['oauth_verifier'])){
$_SESSION['oauth_verifier'] = $_REQUEST['oauth_verifier'];
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
header("Location: " . $config['callback_url']);
exit;
}
else{
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier']; // ERROR: Undefined index: oauth_verifier
$linkedin->access_token = unserialize($_SESSION['oauth_access_token']);
}
Based on your comments and post, I'm guessing you are trying to use the same method twice in a row, so you get logged in. Seems weird but whatever.
The i18n you use will however change your routes to encorporate the languages so you should update your url's which you are setting inside your to controller to also use that language or a language.
In codeigniter, you should always try to set url's by the use of site_url(). This way you can easily port your application to other domainnames/locations. In this case, the localisation-library would also have changed the url's for you.
You should change all references to urls as follows:
$config['base_url'] = site_url("linklogin/initiate/");
$config['callback_url'] = site_url("linklogin/get_resume_linkedin/");
To use site_url(), you will need the URL Helper. You should include that helper before trying to use site_url(). But you already include it in your constructor, so no problems there.
You should also replace the use of header(...); exit; with redirect();. If you die after sending the header, codeigniter will not fully run and your logs will not be fully completed.
redirect($config['callback_url']); // Replaces: header($config['callback_url']);exit;
redirect('linklogin/get_resume_linkedin/'); // Alternative to above statement
I would also advice you to check out the manual to check out the build-in session class and the input class.
Related
I've lost a day and a half now trying to figure out why Yii is deleting all of the session data after I go to Twitter's OAuth page and back to my redirect.
Here is the main SiteController, where I go to Twitter. Here I am trying to save the oauth_token and token_secret values, so I can use them on the redirect controller.
function actionTwitter()
{
$consumerKey = "";
$consumerSecret = "";
$connection = new TwitterOAuth($consumerKey, $consumerSecret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://127.0.0.1/yii/?r=redirect&type=twitter"));
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
Yii::app()->session['token'] = $oauth_token; // This doesn't save!!
Yii::app()->session['token_secret'] = $token_secret; // This does not save!!
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));
$this->redirect($url);
exit(); // some people have said I need to exit the session first after I redirect, but it doesn't help at all.
}
Here is my RedirectController, which is a separate controller and not in the main SiteController:
public function actionIndex()
{
$type = $_GET['type'];
if ($type == "twitter")
{
$token = Yii::app()->session['token'];
print($token);
}
}
I also have the session autostart set to true in my config file.
Thoughts on why it isn't working / stuff I have read about:
Twitter's site is HTTPS, and I am on localhost (which isn't HTTPS). For some reason that I forget this will make the session lose data when I redirect. If this is the case, how do I fix it without using HTTPS?
When I create new CHttpCookies they do not save either, I can't retrieve the value
I have tried Yii::app()->user->setState instead, which isn't working either.
I found the solution. It did not work because I was using 127.0.0.1 for the redirect, instead of the standard localhost. I changed that and all is working now.
I am using jquery throughout a php project, so all pages load dynamically into the main page, I am trying to make links only visible to a certain user, so if they goto the main page and append the URL, i.e ?mode=55rt67 this gets stored as a variable and can be checked throughout the app. I am using the below, but it doesnt work. any suggestions?
if (empty($_GET)) {
$mode = "user";
}else{
define ('$mode', '($_GET['mode']);
}
define is used to declare constants, you want to use a variable, not a constant.
UPDATED (use session to store mode variable):
if (empty($_GET)) {
$_SESSION['mode'] = "user";
}else{
$_SESSION['mode'] = $_GET['mode'];
}
And don't forget to use session_start on every page
Change your code with below code.
// Try this
if(isset($_GET['mode']) ){
define ('mode',$_GET['mode']);
}else{
define ('mode',"user");
}
echo mode;
//OR
if(isset($_GET['mode']) ){
global $mode = $_GET['mode'];
}else{
global $mode = "user";
}
echo $mode;
if (empty($_GET)) {
$_SESSION['mode'] = "user";
}else{
$_SESSION['mode'] = $_GET['mode'];
}
The above solution worked perfectly, added session_start to the index page, and it now carries through the rest of the pages.
Awesome, thanks all
I have been searching the web for a few days now, but can't find anything that i can understand. I am looking for a way to login to OpenERP from a website i created. I want to fill the fields with the user data, and then redirect to OpenERP and login with that data.
Basically, i want to login to OpenERP from another webpage. I know this needs to be done with XML-RPC calls. But i don't know how to do it.
I need someone to explain this to me. How do i program the login using Xml-rpc?
I have checked the link: https://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html/
But this didn't help me.
Thanks
basically, send an XML-RPC message containing parameters like the username and password to OpenERP's 'common' web service address.
Here is the sample login function from PHP xmlrpc i used to use:
<?php
include("xmlrpc.inc");
function login(){
$user = "admin";
$password = "1234";
$dbname = "devel";
$server_url= "http://127.0.0.1:8069/xmlrpc/";
$conn = new xmlrpc_client($server_url . 'common');
$msg = new xmlrpcmsg('login');
$msg->addParam( new xmlrpcval($dbname, "string") );
$msg->addParam( new xmlrpcval($user, "string") );
$msg->addParam( new xmlrpcval($password, "string") );
$resp = $conn->send( $msg );
$val = $resp->value();
$this->id = $val->scalarval();
return $this->id>0 ? $this->id : -1 ;
}
It will return -1 if login failed, otherwise the user ID from OpenERP.
XML-RPC interfacing to OpenERP is complicated and not very well documented. But, I've wrote a detailed e-book about using xmlrpc, search in Google Play for "Advanced PHP and OpenERP Interfacing Using PHP XML-RPC Library".
Regards
I am not sure in which language you want this, I am familiar with the PHP call so will explain it to you in that context.
Technically, there is no real login function. You send the API the parameters and it will respond with a user_id. And this user_id is then used when you make your next calls. Hence if you receive a user_id, 'login' was successful and you can use the user_id to proceed. If the login details were incorrect, you will receive a response stating so.
include ("../xmlrpc-3.0.0.beta/lib/xmlrpc.inc"); //the PHP XML RPC library
//GLOBAL VARIABLES
$dbname = 'database_name'; //Name of the DB you want to access
$user = 'admin'; //The user you want to 'login'
$pwd = 'admin'; //The user's password
$url = 'localhost:8069'; //The server of OpenERP
$sock = new xmlrpc_client($url."/xmlrpc/common"); //Sock files location on server
$client = new xmlrpc_client($url."/xmlrpc/object"); //client files location on server
//THE CALL
$sock_msg = new xmlrpcmsg('login'); //Type of message (all other cases use 'execute')
$sock_msg->addParam(new xmlrpcval($dbname, "string")); //1st param - Database
$sock_msg->addParam(new xmlrpcval($user, "string")); //2nd param - Username
$sock_msg->addParam(new xmlrpcval($pwd, "string")); //3rd param - password
$sock_resp = $sock->send($sock_msg); //Sends the message with parrams
if ($sock_resp->errno != 0)
{
//if there is an error
echo 'error.<br>';
}
// if your have successfully logged in, the errno == 0
elseif ($sock_resp->errno == 0)
{
//a user_id will be returned
$sock_val = $sock_resp->value();
$user_id = $sock_val->scalarval();
//YOUR NEXT CALL HERE
}
Does anybody try zf2? I can not understand new mechanism of using sessions in zf2. How can I write and read to/from the session in new zend framework?
Also I can not find any examples in the internet.
Some examples of zf2 sessions usage:
Session creation:
use Zend\Session\Container;
$session = new Container('base');
Check that key exists in session:
$session->offsetExists('email')
Getting value from the session by key:
$email = $session->offsetGet('email');
Setting value in session:
$session->offsetSet('email', $email);
Unsetting value in session:
$session->offsetUnset('email');
And other easy way to use session are:
$session = new Container('foo');
// these are all equivalent means to the same end
$session['bar'] = 'foobar';
$session->bar = 'foobar';
$session->offsetSet('bar', 'foobar');
Definitely yes, you should use Zend\Session\Container
Container extends of ArrayObject and instantiates with ARRAY_AS_PROPS flag that means you can easily iterate through properties and read/write them, e.g.
use Zend\Session\Container as SessionContainer;
$this->session = new SessionContainer('post_supply');
$this->session->ex = true;
var_dump($this->session->ex);
First argument is session namespace and second — Manager. Manager is a facade for Storage and SaveHandler and it's configured with ConfigInterface in order to save your session data in DB or Memcache server.
I'm currently working with zf2. I found usage of Sessions in:
Zend\Authentication\Storage\Session.php
Maybe you can find your answer there.
If you are trying to use session in your login action, you can use: "Zend\Authentication\AuthenticationService". It Authenticates the user and store session as well.
getStorage()->write($contents) will store the session.
well here is the brief example. i have implemented regarding maintaining session on successful authentication of user.
<?php
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user');
$authAdapter->setIdentityColumn("user_name");
$authAdapter->setCredentialColumn("user_password");
//get values
$username = $request->getParam('username');
$password = $request->getParam('password');
//set values
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$auth = Zend_Auth::getInstance();
//to store in session
$auth->setStorage(new Zend_Auth_Storage_Session('front'));
$authResult = $auth->authenticate($authAdapter);
if ($authResult->isValid()) {
$authAdap = $authAdapter->getResultRowObject(null, "Password");
$auth->getStorage()->write($authAdap);
$this->_redirect('/login/controlpannel');
} else {
$this->_redirect('/login/login');
}
?>
getting values or check data stored in session related to user
<?php
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('front'));
if($auth->hasIdentity()){
$data = $auth->getStorage()->read();
print_r($data);
}else{
$this->_redirect('/login/login');
}
?>
hope this could help someone
use Zend\Session\Container;
public function createAction(){
$session = new Container('name');
$session->offsetSet('session_variable', $value);
}
//the above codes are used for create session.
public function take_valuesAction(){
$session = new Container('name');
echo $value = $session->offsetGet('session_variable');
}
//the above codes are used for take values from session.
public function destroyAction(){
$session = new Container('name');
$session->getManager()->destroy();
}
//the above codes are used for destroy the session.
To start a session you need to use
zend\session\container
I made session using zend authentication it works good but my problem is I want to change some property of it from another action in other controller my code is:
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) {
$blogId = new model_blog request;
$auth->getIdentity()->user_current_blog = $blogId;
print "Current Blog";
print_r($auth->getIdentity()->user_current_blog);
}
in this action user_current_blog change but in other action it not works!!!
where I made a mistake???
$identity = $auth->getIdentity();
$identity->user_current_blog = $blogId;
$authStorage = $auth->getStorage();
$authStorage->write($identity);
http://framework.zend.com/manual/en/zend.auth.adapter.dbtable.html#zend.auth.adapter.dbtable.advanced.storing_result_row