twitteroauth
twitteroauth copied to clipboard
Version 2 API returning null on connection
I'm using laravel 8.8 and trying to connect to my Twitter app. I've installed via composer and imported the TwitterOAuth class.
VS Code is throwing a red line under new TwitterOAuth, so any reason why this is not working as it should be?
I've quadruple checked my keys and tokens.
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', '**');
define('CONSUMER_SECRET', '**');
define('ACCESS_TOKEN', '**');
define('ACCESS_TOKEN_SECRET', '**');
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$connection->setApiVersion('2');
var_dump($connection);
Maybe TwitterOAuth isn't installed fully? var_dump($connection); should show something like this:
object(Abraham\TwitterOAuth\TwitterOAuth)#2 (16) {
["timeout":protected]=>
int(5)
["connectionTimeout":protected]=>
int(5)
["maxRetries":protected]=>
int(0)
["retriesDelay":protected]=>
int(1)
["apiVersion":protected]=>
string(1) "2"
["decodeJsonAsArray":protected]=>
bool(false)
["userAgent":protected]=>
string(40) "TwitterOAuth (+https://twitteroauth.com)"
["proxy":protected]=>
array(0) {
}
["gzipEncoding":protected]=>
bool(true)
["chunkSize":protected]=>
int(250000)
// ...
}
I get that. But
` $connection->setApiVersion('2'); //$content = $connection->get("account/verify_credentials");
$status = 'This is a test tweet.';
$result = $connection->post("statuses/update", ["status" => $status], true); if ($connection->getLastHttpCode() == 200) { echo "Your Tweet posted successfully."; } else { var_dump($result); echo 'error: ' . $result->errors[0]->message; }
`
Prints NULL. I can't seem to get tweet posting to work
And I get "object(stdClass)#6076 (4) { ["title"]=> string(9) "Forbidden" ["detail"]=> string(9) "Forbidden" ["type"]=> string(11) "about:blank" ["status"]=> int(403) }"
When I try
`
$connection->setApiVersion('2');
//$content = $connection->get("account/verify_credentials");
$status = 'This is a test tweet.';
$result = $connection->post("tweets", ["status" => $status], true); if ($connection->getLastHttpCode() == 200) { echo "Your Tweet posted successfully."; } else { var_dump($result); echo 'error: ' . $result->errors[0]->message; } `
removing $connection->setApiVersion('2'); gets NULL again
Maybe TwitterOAuth isn't installed fully?
var_dump($connection);should show something like this:object(Abraham\TwitterOAuth\TwitterOAuth)#2 (16) { ["timeout":protected]=> int(5) ["connectionTimeout":protected]=> int(5) ["maxRetries":protected]=> int(0) ["retriesDelay":protected]=> int(1) ["apiVersion":protected]=> string(1) "2" ["decodeJsonAsArray":protected]=> bool(false) ["userAgent":protected]=> string(40) "TwitterOAuth (+https://twitteroauth.com)" ["proxy":protected]=> array(0) { } ["gzipEncoding":protected]=> bool(true) ["chunkSize":protected]=> int(250000) // ... }
I can get that far but as soon as I try what RoHudson did above I also get NULL.
Also anytime I post a tweet using e.g.
$connection = new TwitterOAuth(...details here); $connection->setApiVersion('2'); $response = $connection->post('tweets', ['text' => 'Hello Twitter'], true);
I get the following returned:
object(stdClass)#297 (4) { ["title"]=> string(9) "Forbidden" ["detail"]=> string(9) "Forbidden" ["type"]=> string(11) "about:blank" ["status"]=> int(403) }
Why is this? I cannot follow the docs or maybe the docs are suited much to v2. I've been back and fore on this for 3 months.
For the forbidden error checkout https://github.com/abraham/twitteroauth/issues/1090#issuecomment-1175526469. I think your access tokens don't permit write access.
I think this is fixed with permission changes. Please let me know otherwise.