I understand that this question is asked a lot of times, but yet, I still couldn't fix my issue with all the different solutions out there.
I trying to make use of this PHP class: https://github.com/abraham/twitteroauth
I've been manipulating with my code for quite a while and I got nothing, except these errors:
Notice: Undefined index: oauth_token in C:\xampp\htdocs\mywebsite\components\SocialConnect\twitter\twitteroauth.php on line 80
Notice: Undefined index: oauth_token_secret in C:\xampp\htdocs\mywebsite\components\SocialConnect\twitter\twitteroauth.php on line 80
Array ( [Failed to validate oauth signature and token] => )
This is very strange. I don't see anything that I could have done wrong. This is the code I've been writing:
include($mywebsite_root_path . 'components/SocialConnect/twitter/twitteroauth.php');
define('TWITTER_CONSUMER_KEY', '123');
define('TWITTER_CONSUMER_SECRET', 'abc');
define('TWITTER_OAUTH_CALLBACK', 'http://localhost/mywebsite/user/connect/twitter');
$oauth_token = request_var('oauth_token', '');
$stored_token = $user->get_data('oauth_token');
if (!empty($oauth_token))
{
if ($oauth_token != $stored_token)
{
$user->set_data('oauth_status', 'oldtoken');
$user->quit(false);
redirect('user/connect/' . $service);
}
unset ($twitter);
$secret_token = $user->get_data('oauth_token_secret');
$twitter = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $stored_token, $secret_token);
/* Request access tokens from twitter */
$oauth_verifier = request_var('oauth_verifier', '');
$access_token = $twitter->getAccessToken($oauth_verifier);
/* Save the access tokens. Normally these would be saved in a database for future use. */
$user->set_data('access_token', $access_token);
/* Remove no longer needed request tokens */
unset($oauth_token, $secret_token);
$user->set_data('oauth_token', '');
$user->set_data('oauth_token_secret', '');
if (200 == $twitter->http_code)
{
$user->set_data('status', 'verified');
// Register here
throw_msg("Welcome. Your account has been successfully connected with Twitter", true, null, 3, 'success');
}
else
{
$user->quit(false);
throw_msg('Could not log into your Twitter account. Please try again.', true);
}
}
else
{
$twitter = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
/* Get temporary credentials. */
$request_token = $twitter->getRequestToken(TWITTER_OAUTH_CALLBACK);
print_r($request_token); exit;
/* Save temporary credentials to session. */
$token = $request_token['oauth_token'];
$user->set_data('oauth_token', $token);
$user->set_data('oauth_token_secret', $request_token['oauth_token_secret']);
switch ($twitter->http_code)
{
case 200:
$url = $twitter->getAuthorizeURL($token);
redirect($url, 'instant', null, true);
break;
default:
throw_msg('Could not log into your Twitter account. Please try again.', true);
break;
}
}
So as you see, I'm attempting to keep all my code in one page. The error I've shown above comes from the this line: $token = $request_token['oauth_token'];
. I've exited the script right before showing the whole error in order to print_r
the array and get the real cause of this problem, which is
Failed to validate oauth signature and token
I'm stuck with this and I don't know how to continue. Any clues would be highly appreciated.
request_var()
is equivalent to $_REQUEST
$user->get_data()
gets data from the $_SESSION
$user->set_data()
sets data to the $_SESSION
$user->quit(false)
destroys the session