TwitterOAuth from Abraham - Could not authenticate

2019-07-22 20:33发布

问题:

I am trying to use the TwitterOAuth solution from Abraham.

I've done everything as described in his documentation, but I still get that error:

{"errors":[{"code":32,"message":"Could not authenticate you."}]}

That's my source code:

    require "twitteroauth/autoload.php";
    use Abraham\TwitterOAuth\TwitterOAuth;

    $consumerkey = 'xx';
    $consumersecret = 'xx';
    $accesstoken = 'xx';
    $accesstokensecret = 'xx';

    $connection = new TwitterOAuth($consumerkey,$consumersecret,$accesstoken,$accesstokensecret);

    $tweets = $connection->get("search/tweets.json?q=superbowl");    
    echo json_encode($tweets);

All the keys are correct. I have no clue, why this still happens. The App Permission are Read only.

What I want to do is get tweets based on a search query.

Do you have any idea how to fix this? Let me know, if you need some more information.

回答1:

Hope this will fix your problem....

<form action='' method='get' ><input type='text' name='q'> <button type='submit' >click</button></form>


<?php

require "autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$consumerKey = "youconsumerkey";
$consumerSecret = "yourconsumersecretkey";
$oauthToken = "youoauthtoken";
$oauthTokenSecret = "youroauthtokensecret";

$twitter = new TwitterOAuth($consumerKey,$consumerSecret,$oauthToken,$oauthTokenSecret);

$search = isset($_GET['q']);

if(isset($_GET['q'])){

$tweets = $twitter->get('search/tweets',array('q'=>'%23'.$_GET['q'],'result_type'=>'recent','count'=>'10'));

//var_dump($tweets);

foreach($tweets->statuses as $value){

echo $value->user->name; echo "<br/>";

 //echo json_encode($tweets);
}
 }
?>
#

Actually in my case the reason behind ]Could not authenticate you; Error: 32]

was this line of code that i had tested for my project:

// $tweets = $twitter->get('search/tweets.json?q=mytest&result_type=recent&count=10');

I am Using TwitterOAuth PHP Library for the Twitter REST API, https://twitteroauth.com/ .

HTTP GET https://api.twitter.com/1.1/search/tweets.json

TwitterOAuth

$tweets= $twitter->get("search/tweets.json", array("result_type" => 'recent', "count" => 10)); //should be like this

// $tweets= $twitter->get('search/tweets.json?q=mi_twitter_test&result_type=recent&count=10');  //throws authentication error