PHP OAuth Twitter
问题描述:
我已经创建了一个Twitter应用程序,我用它来发布推文。我无法解决的问题是每次必须允许访问我的应用程序。PHP OAuth Twitter
因此可以说我需要推送三条消息,所以所有三次我必须允许访问我的应用程序。
我只是需要一旦用户允许访问我的应用程序,下次他应该只被要求允许访问是当他/她重新登录时。
这里是我的代码,我使用
Share content on twitter"; include 'lib/EpiCurl.php'; include 'lib/EpiOAuth.php'; include 'lib/EpiTwitter.php'; include 'lib/secret.php'; $twitterObj = new EpiTwitter($consumer_key, $consumer_secret); $oauth_token = $_GET['oauth_token']; if($oauth_token == '') { $url = $twitterObj->getAuthorizationUrl(); echo ""; echo "Sign In with Twitter"; echo ""; } else { $twitterObj->setToken($_GET['oauth_token']); $token = $twitterObj->getAccessToken(); $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret); $_SESSION['ot'] = $token->oauth_token; $_SESSION['ots'] = $token->oauth_token_secret; $twitterInfo= $twitterObj->get_accountVerify_credentials(); $twitterInfo->response; $username = $twitterInfo->screen_name; $profilepic = $twitterInfo->profile_image_url; include 'update.php'; } if(isset($_POST['submit'])) { $msg = $_REQUEST['tweet']; $twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']); $update_status = $twitterObj->post_statusesUpdate(array('status' => $msg)); $temp = $update_status->response; header("Location: MessageStatus.html"); exit(); } ?>
答
Share content on twitter"; include 'lib/EpiCurl.php'; include 'lib/EpiOAuth.php'; include 'lib/EpiTwitter.php'; include 'lib/secret.php'; $twitterObj = new EpiTwitter($consumer_key, $consumer_secret); $oauth_token = empty($_SESSION['ot']) ? $_GET['oauth_token']:$_SESSION['ot']; if($oauth_token == '') { $url = $twitterObj->getAuthorizationUrl(); echo ""; echo "Sign In with Twitter"; echo ""; } else { if(empty($_SESSION['ot'])) { $twitterObj->setToken($_GET['oauth_token']); $token = $twitterObj->getAccessToken(); $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret); $_SESSION['ot'] = $token->oauth_token; $_SESSION['ots'] = $token->oauth_token_secret; } else { $twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']); } $twitterInfo= $twitterObj->get_accountVerify_credentials(); $twitterInfo->response; $username = $twitterInfo->screen_name; $profilepic = $twitterInfo->profile_image_url; include 'update.php'; } if(isset($_POST['submit'])) { $msg = $_REQUEST['tweet']; try { $twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']); $update_status = $twitterObj->post_statusesUpdate(array('status' => $msg)); $temp = $update_status->response; } catch (Exception $e) { } } ?>