twitteroauth icon indicating copy to clipboard operation
twitteroauth copied to clipboard

Version 2 API returning null on connection

Open joshembling opened this issue 3 years ago • 4 comments

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);

joshembling avatar Apr 05 '22 19:04 joshembling

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)
  // ...
}

abraham avatar Apr 07 '22 19:04 abraham

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

RoHodson avatar Jun 15 '22 15:06 RoHodson

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.

joshembling avatar Jul 07 '22 18:07 joshembling

For the forbidden error checkout https://github.com/abraham/twitteroauth/issues/1090#issuecomment-1175526469. I think your access tokens don't permit write access.

abraham avatar Jul 07 '22 18:07 abraham

I think this is fixed with permission changes. Please let me know otherwise.

abraham avatar Aug 18 '22 23:08 abraham