我为网站创建了一个Twitter应用程序。我想整合登录/注册Twitter的可能性。我使用php文件作为帮助程序,分别命名为TwitterOAuth.php和OAuth.php。当用户点击Twitter按钮时,他/她被重定向到名为twitter.php的页面,其中包含以下源代码:
<?php
/* Build TwitterOAuth object with client credentials. */
$connection = Common::twitter();
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(App::env()->get('url'));
/* Save temporary credentials to session. */
$token = $request_token['oauth_token'];
$_SESSION['twitter'] = array('id' => $request_token['oauth_token'], 'token' => $request_token['oauth_token_secret']);
/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
case 200:
/* Build authorize URL and redirect user to Twitter. */
$url = $connection->getAuthorizeURL($token);
header('Location: ' . $url);
break;
default:
/* Show notification if something went wrong. */
var_dump($request_token);
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
Common::twitter()
功能如下:
/**
* Returns the twitter OAuth service.
*
* @return TwitterOAuth
*/
public static function twitter() {
if (!self::$tw) {
if ((User::isLoggedIn()) && (User::current()->hasTwitterAccount())) {
self::$tw = new TwitterOAuth(
App::env()->get('twitter', 'consumerKey'),
App::env()->get('twitter', 'consumerSecret'),
App::CurrentUser()->getTwitterId(),
App::CurrentUser()->getTwitterUserAccessToken()
);
} else {
self::$tw = new TwitterOAuth(
App::env()->get('twitter', 'consumerKey'),
App::env()->get('twitter', 'consumerSecret')
);
}
}
return self::$tw;
}
在我正在测试的场景中,执行了else分支。但是,我得到一个例外:
异常'PHPErrorException',消息'Notice [8]未定义 index:oauth_token文件中的第81行错误... \ lib \ TwitterOAuth.php
出现问题的功能如下:
/**
* Get a request_token from Twitter
*
* @returns a key/value array containing oauth_token and oauth_token_secret
*/
function getRequestToken($oauth_callback) {
$parameters = array();
$parameters['oauth_callback'] = $oauth_callback;
$request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
$token = OAuthUtil::parse_parameters($request);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}
问题是OAuthUtil::parse_parameters($request)
返回一个空数组。这种情况正在发生,因为$request
为false
,但$this->requestTokenURL
为https://api.twitter.com/oauth/request_token,$parameters
有oauth_callback
,其中包含定义的回调网址在Twitter应用程序中。可能是造成这个问题的原因是什么?
编辑:
Source of `$this->oAuthRequest`:
/**
* Format and sign an OAuth / API request
*/
function oAuthRequest($url, $method, $parameters) {
if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
$url = "{$this->host}{$url}.{$this->format}";
}
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
$request->sign_request($this->sha1_method, $this->consumer, $this->token);
switch ($method) {
case 'GET':
return $this->http($request->to_url(), 'GET');
default:
return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
}
}
此方法位于TwitterOAuth.php内。
答案 0 :(得分:0)
在OAuth.php中,get_normalized_http_url
方法中有一个代码块,即:
$port = @$parts['port'];
这导致了一些错误,所以我修复了这个:
$port = (array_key_exists('port', $parts) ? $parts['port'] : 80);
然而,显然端口号80是问题,在我将块改为此后:
$port = (array_key_exists('port', $parts) ? $parts['port'] : 443);
它就像一个咒语。