I have implemented the OAuth request in my system. In brief the system that we are developing is a Social Media Manager System, the idea is the Community Managers add their Twitter accounts in our system. But when I try to add an account sometimes works and other times not works.
First of all I create the login url
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$twitter->setTimeouts(10, 10);
$request_token = $twitter->oauth('oauth/request_token', array('oauth_callback' => 'CALLBACK URL'));
$url = $twitter->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
$this->view->assign('urlLoginTwitter', $url]); //This is for send to view the link
//Save the temporal tokens, it will use in next steps
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: ' . $my_file);
$data = $request_token['oauth_token'] . '&' . $request_token['oauth_token_secret'];
fwrite($handle, $data);
fclose($handle);
Later the user click the url, authenticate in twitter and the moment of callback
$currentUrl = $this->uriBuilder->getRequest()->getRequestUri(); //Get the url of browser with oauth_token and oauth_verifier
$urlParts = explode('&', $currentUrl);
$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle, filesize($my_file));
fclose($handle);
$request_token = explode('&', $data);
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token[0], $request_token[1]);
$oauthVerifier = explode('=', $urlParts[2]);
try {
$accessTokensThisPerfil = $twitter->oauth('oauth/access_token', array("oauth_verifier" => "$oauthVerifier[1]"));
echo"WORKS";
} catch (\Exception $exception) {
echo"FAIL";
$logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
$logger->error('TWITTER add', array('data' => $exception->getMessage()));
}
How I say, sometimes the “echo” is “WORKS” and others “FAIL”. When I write in the log, the error is
Error processing your OAuth request: Invalid oauth_verifier parameter
I understand the problem is de outh_verified that I get from the URL in callback. But I do not know why works and not works randomly.
Please guide me or illustrate me what may be happening