gpt4 book ai didi

php - TwitterOAuth 商店 token 供离线使用

转载 作者:行者123 更新时间:2023-11-29 07:02:06 25 4
gpt4 key购买 nike

嗨,抱歉我的英语不好,我写了一个简单的应用程序来更新 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”并且我无法更新状态...

最佳答案

$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。

关于php - TwitterOAuth 商店 token 供离线使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9640259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com