我整天都在努力实现 Twitter 身份验证(使用 OAuth),但我已经绝望了,我仍然收到错误消息无法验证你。,它正在成为我的噩梦。
这是我用于身份验证的两个文件:
requestOauth.php
session_start();
$basedir=dirname(__FILE__)."";
include($basedir.'/config.php');
include_once( $basedir.'/twitteroauth/twitteroauth.php' );
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
//$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
//$_SESSION['oauth_token_secret'] = $secret = $request_token['oauth_token_secret'];
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
switch ($connection->http_code) {
case 200:
$url = $connection->getAuthorizeURL($_SESSION['oauth_token'], FALSE);
header('Location: ' . $url);
break;
default:
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
索引.php
if(!isset($_GET['oauth_verifier'])){
$link_redirect = '<a href="requestOauth.php">Login With Twitter</a>';
}
else
{
if(isset($_SESSION['access_token'])){
$oauth_token = $_SESSION['access_token']['oauth_token'];
$oauth_token_secret = $_SESSION['access_token']['oauth_token_secret'];
$twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
} else {
$twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$accessToken = $twitterRequest->getAccessToken($_GET['oauth_verifier']);
$_SESSION['access_token'] = $accessToken;
echo 'ACCESS TOKEN2: '.var_dump($_SESSION['access_token']);
}
$user_info = $twitterRequest->get('account/verify_credentials');
if(isset($user_info->error))
{
echo $user_info->error;
//header("location: requestOauth.php");
} else {
var_dump($_GET);
}
我仍然收到错误无法对您进行身份验证。并且已经不知道,哪里可能有问题......
你能给我任何帮助吗,如何解决这个问题?
谢谢