abraham / twitteroauth

The most popular PHP library for use with the Twitter OAuth REST API.
https://twitteroauth.com
MIT License
4.29k stars 1.71k forks source link

Every time redirects to authorize page #530

Closed RizwanSFDC closed 7 years ago

RizwanSFDC commented 7 years ago

i want to test the twitteroauth as follows, if i enter the tweet message in any text box and then after submitting the page,it should appear in Twitter Account.

whenever i submit the htmt page the page redirects to authorize page,

could you please guide me,how can i accomplish this.

for that i make files as follows :

index.html

<html>
<body bgcolor="#E6E6FA" >

<form  name="tweet" action="Tweet.php" method="get">

  <input type="hidden" name="source" value="tweetbutton">
<textarea rows="4" cols="50" name="tweet"  placeholder="Type Your Message here..." maxlength="150"></textarea>
<br>
  <input type="submit" value="Tweet This" />
</form>
</body>
</html>

Tweet.php

<?php

session_start();
require_once("autoload.php");

use Abraham\TwitterOAuth\TwitterOAuth;

$msg = $_REQUEST['tweet'];
define('CONSUMER_KEY','*******************************');
define('CONSUMER_SECRET','**********************************************');
define('OAUTH_CALLBACK','http://serveraddress/twitteroauth/tests/callback.php');

if(!isset($_SESSION['access_token'])){

$connection = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token']));
echo $url;
}
else{

$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET,$access_token['oauth_token'],$access_token['oauth_token_secret']);
$user = $connection->get("account/verify_credentials");
$statuses = $connection->get("search/tweets", ["q" => "twitterapi"]);
$statues = $connection->post("statuses/update", ["status" => "$msg"]);
echo("Tweet Message:". $statues->text ."<br>"."posted successfully!!!");
}
abraham commented 7 years ago

You have if(!isset($_SESSION['access_token'])) as the control to see if the user is authenticated. That session variable must not be getting set. Walk through your code that finishes authentication to make sure you are getting a user's authentication correctly.

You can see the entire authentication flow live starting at https://twitteroauth.com/redirect.php