TwitterOAuth商店令牌供离线使用

时间:2012-03-09 20:06:22

标签: php mysql api twitter oauth

对不起我的英文不好,我已经在twitter帐户中写了简单的app更新状态。 Twitter登录:

    require("twitteroauth.php");
    session_start();
    $twitteroauth = new TwitterOAuth('*******', '***********');
    $request_token = $twitteroauth->getRequestToken('http://127.0.0.1/twitter_oauth.php');
    $_SESSION['oauth_token'] = $request_token['oauth_token'];
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
    if($twitteroauth->http_code==200)
    {
        $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
        header('Location: '. $url);
    } 
    else 
    {
        die('error');
    }

twitter_oauth.php:

require("twitteroauth.php");
session_start();
if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret']))
{
    require("config.php");
    $twitteroauth = new TwitterOAuth('****', '********', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    $access_token = $twitteroauth->getAccessToken();
    $_SESSION['access_token'] = $access_token;
    $user_info = $twitteroauth->get('account/verify_credentials');
    $username= $user_info->screen_name;
    $followerInit= $user_info->followers_count;
    $oauth_token=$_SESSION['oauth_token'];
    $oauth_token_secret=$_SESSION['oauth_token_secret'];
    $update=query("SELECT 1 FROM connect WHERE id_user=(SELECT id FROM user WHERE user='$username' )");
    if(mysql_num_rows($update)>0)
    {
        query("UPDATE connect set oauth_token='$oauth_token',  oauth_token_secret='$oauth_token_secret' WHERE id_user=(SELECT id FROM user WHERE user='$username' ) ");
    }   
    else
    {
        query("INSERT INTO connect(id_user,followerInit,oauth_token,oauth_token_secret) VALUES((SELECT id FROM user WHERE user='$username' ),$followerInit,'$oauth_token','$oauth_token_secret' )  ");
    }
    header('Location: index.php');
} 
else 
{
    header('Location: twitter_login.php');
}

我保存oauth_token,oauth_token_secret和index.php,当用户有oauth_token,oauth_token_secret我想要更新状态:

    session_start();
    require("twitterbot/twitterbot/twitteroauth.php");
    require("twitterbot/config.php");
    $query=query("SELECT oauth_token,oauth_token_secret FROM connect WHERE id_user=8");
    if(mysql_num_rows($query)>0)
    {
        $row=mysql_fetch_row($query);
    }
    else
    {
        header('Location: twitter_login.php');
    }
    $twitteroauth = new TwitterOAuth('bSMnfSsk3CTcSV85jKWz1w', 'HeNAG1v2sgxhkq1QyfZDd029JUDdRNGAaO85Nsg', $row[0], $row[1]);
    print_r($twitteroauth);
    print_r($twitteroauth->OAuthRequest('https://twitter.com/statuses/update.xml',
array('status' => "Hey! I'm posting via #OAuth!"), 'POST'));

但在index.php中我有错误,“无法验证您./statuses/update.xml”,我无法更新状态...

1 个答案:

答案 0 :(得分:2)

$access_token = $twitteroauth->getAccessToken();
$_SESSION['access_token'] = $access_token;
$user_info = $twitteroauth->get('account/verify_credentials');
$username= $user_info->screen_name;
$followerInit= $user_info->followers_count;
$oauth_token=$_SESSION['oauth_token'];
$oauth_token_secret=$_SESSION['oauth_token_secret'];

在此代码中,您将获得访问oauth_token,但在数据库中保存临时请求oauth_token。您需要保存access_token。