通过PHP访问Twitter API
我正在用OAuth而不是基本的Auth来制作脚本网格,我被卡住了。到目前为止,我只是在进行身份验证,但我无法让它正常工作。此代码:通过PHP访问Twitter API,php,twitter,oauth,Php,Twitter,Oauth,我正在用OAuth而不是基本的Auth来制作脚本网格,我被卡住了。到目前为止,我只是在进行身份验证,但我无法让它正常工作。此代码: <?php include 'config.php'; include 'twitteroauth/twitteroauth.php'; // Use config.php credentials $conn = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET); // Use applic
<?php
include 'config.php';
include 'twitteroauth/twitteroauth.php';
// Use config.php credentials
$conn = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET);
// Use application's registered callback URL
// And get temporary credentials using made connection
$tempCred = $conn->getRequestToken();
// Use 'Sign in with Twitter'
// for Redirect URL
$rURL = $conn->getAuthorizeURL($tempCred);
echo '<a href="'.$rURL.'">1. Click me first!</a><br />';
是的,config.php是正确的。检查
CONSUMER\u KEY
和CONSUMER\u SECRET
是否与twitter上应用程序页面中的值相同
从该方法返回的授权URL是什么?我使用了该API,它对我来说运行良好。 正如伊恩·奎格利(Ian Quigley)所说,除了真正检查消费者密钥和消费者秘密之外,没有其他事情会出现在脑海中 因为您说您的$\u会话['oauth\u token']是空的,不应该是空的。其为空的原因可能是CONSUMER\u KEY和CONSUMER\u SECRET中的值错误 如果你给Twitter发送了错误的密钥,它不会给你需要的会话变量
只需检查(应用程序数据)和config.php文件中的值是否相同。默认情况下,config.php包含错误的值,您需要填写自己的值。我已将此api用于twitter基于pin的oauth身份验证。我使用了php简单的oauth库 这是代码,如果你想看的话
class TwitterPinBasedOauth{
private static $requestTokenUrl = 'http://twitter.com/oauth/request_token';
private static $accessTokenUrl = 'http://twitter.com/oauth/access_token';
private static $authorizeUrl = 'http://twitter.com/oauth/authorize';
private static $updateUrl = 'http://twitter.com/statuses/update.json';
private $twitterOauth;
public function __construct(){
$this->twitterOauth = new OAuth(ConsumerToken::$CONSUMER_KEY,
ConsumerToken::$CONSUMER_SECRET,
OAUTH_SIG_METHOD_HMACSHA1,
OAUTH_AUTH_TYPE_AUTHORIZATION);
}
public function getAndStoreRequestToken(){
$callbackUrl = "oob";
$response = $this->twitterOauth->getRequestToken(self::$requestTokenUrl, $callbackUrl);
print_r("REQUEST TOKEN:\n");
print_r($response);
print_r(PHP_EOL);
file_put_contents(Constants::$oauth_request_file, serialize($response));
echo "AUTH URL:\n".self::$authorizeUrl."?oauth_token=".$response['oauth_token'].PHP_EOL;
}
public function getAcessToken($pin){
$request_tokens = unserialize(file_get_contents(Constants::$oauth_request_file));
$this->twitterOauth->setToken($request_tokens["oauth_token"],$request_tokens["oauth_token_secret"]);
$response = $this->twitterOauth->getAccessToken(self::$accessTokenUrl, NULL, $pin);
file_put_contents(Constants::$oauth_access_file, serialize($response));
print_r("ACESS TOKEN:\n");
print_r($response);
print_r(PHP_EOL);
}
public function updateStatus($status){
try{
$access_tokens = unserialize(file_get_contents(Constants::$oauth_access_file));
$this->twitterOauth->setToken($access_tokens["oauth_token"],$access_tokens["oauth_token_secret"]);
$this->twitterOauth->fetch(self::$updateUrl,
array('status' => $status),
OAUTH_HTTP_METHOD_POST);
}
catch(OAuthException $e){
error_log($e->getMessage().PHP_EOL);
return intval($e->getCode());
}
}
}
你能发布函数
getAccessToken()
的源代码吗?我使用的API库是Abraham的,是Twitter API Wiki库部分PHP下的第一个链接。对不起,那个函数不是我自己写的。不过我还是把它放上去了。99&确定它是开源的。你能验证那些会话变量的内容吗?+1我会给它一个机会,并用出现的内容进行更新。
function getAccessToken($oauth_verifier = FALSE) {
$parameters = array();
if (!empty($oauth_verifier)) {
$parameters['oauth_verifier'] = $oauth_verifier;
}
$request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
$token = OAuthUtil::parse_parameters($request);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}
class TwitterPinBasedOauth{
private static $requestTokenUrl = 'http://twitter.com/oauth/request_token';
private static $accessTokenUrl = 'http://twitter.com/oauth/access_token';
private static $authorizeUrl = 'http://twitter.com/oauth/authorize';
private static $updateUrl = 'http://twitter.com/statuses/update.json';
private $twitterOauth;
public function __construct(){
$this->twitterOauth = new OAuth(ConsumerToken::$CONSUMER_KEY,
ConsumerToken::$CONSUMER_SECRET,
OAUTH_SIG_METHOD_HMACSHA1,
OAUTH_AUTH_TYPE_AUTHORIZATION);
}
public function getAndStoreRequestToken(){
$callbackUrl = "oob";
$response = $this->twitterOauth->getRequestToken(self::$requestTokenUrl, $callbackUrl);
print_r("REQUEST TOKEN:\n");
print_r($response);
print_r(PHP_EOL);
file_put_contents(Constants::$oauth_request_file, serialize($response));
echo "AUTH URL:\n".self::$authorizeUrl."?oauth_token=".$response['oauth_token'].PHP_EOL;
}
public function getAcessToken($pin){
$request_tokens = unserialize(file_get_contents(Constants::$oauth_request_file));
$this->twitterOauth->setToken($request_tokens["oauth_token"],$request_tokens["oauth_token_secret"]);
$response = $this->twitterOauth->getAccessToken(self::$accessTokenUrl, NULL, $pin);
file_put_contents(Constants::$oauth_access_file, serialize($response));
print_r("ACESS TOKEN:\n");
print_r($response);
print_r(PHP_EOL);
}
public function updateStatus($status){
try{
$access_tokens = unserialize(file_get_contents(Constants::$oauth_access_file));
$this->twitterOauth->setToken($access_tokens["oauth_token"],$access_tokens["oauth_token_secret"]);
$this->twitterOauth->fetch(self::$updateUrl,
array('status' => $status),
OAUTH_HTTP_METHOD_POST);
}
catch(OAuthException $e){
error_log($e->getMessage().PHP_EOL);
return intval($e->getCode());
}
}
}