Search code examples
javascriptphptwitteroauthhttp-redirect

javascript redirect for twitter oauth causing invalid oauth_verifier parameter


Im attempting to do a twitter oauth and when i directly access the login file on the browser everything works but if i do a javascript window.location.href to the login file an exception is being thrown that says Error processing your OAuth request: Invalid oauth_verifier parameter

Here's my code for the login.php file:

ini_set('session.save_path','/xxx/socialmedia/session');
session_start();
$twitterKey = 'xxx';
$twitterSecret = 'xxx';
$callback = "https://xxx/socialmedia/twittercallback";
require_once("/xxx/twitteroauth/vendor/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
$twitter = new TwitterOAuth($twitterKey,$twitterSecret);
$result = $twitter->oauth('oauth/request_token', ['oauth_callback' => $callback]);
$_SESSION['oauth_token'] = $result['oauth_token'];
$_SESSION['oauth_token_secret'] = $result['oauth_token_secret'];
session_write_close();
$url = $twitter->url('oauth/authorize', array('oauth_token' => $result['oauth_token']));
header("Location: $url");

And here's my code for the callback:

ini_set('session.save_path','/xxx/socialmedia/session');
session_start();
$twitterKey = 'xxx';
$twitterSecret = 'xxx';
require_once("/xxx/twitteroauth/vendor/autoload.php");
$twitter = new \Abraham\TwitterOAuth\TwitterOAuth($twitterKey,$twitterSecret,$_SESSION['oauth_token'],$_SESSION['oauth_token_secret']);
try {
  $access_token = $twitter->oauth("oauth/access_token", ["oauth_verifier" => $_REQUEST['oauth_verifier']]);
}catch(\Exception $e){
  print_r($e);
}

Not quite sure what's wrong here but it works when I directly access the login file on the browser but if i go to the login file from a javascript redirect I get the error.


Solution

  • I was redirecting as //xxx.com/socialmedia/twittercallback when it needed to be /socialmedia/twittercallback