It looks like you're trying to post a tweet using the Twitter API v2. There are a few things to note when working with the Twitter API:
- Make sure you have the correct permissions for your Twitter app to post tweets on behalf of a user.
- Ensure that you're using the correct authentication method (OAuth 1.0a or OAuth 2.0 Bearer Token) based on your app's permissions.
- The body of the POST request should be JSON encoded.
Here's a revised version of your submit method that should work if you have the correct permissions and bearer token:
public function submit()
{
$status = $this->title . ' ' . $this->content;
$client = new Client([
'base_uri' => 'https://api.twitter.com/2/',
'headers' => [
'Authorization' => 'Bearer ' . env('TWITTER_BEARER_TOKEN'),
'Content-Type' => 'application/json',
],
]);
try {
$response = $client->post('tweets', [
'json' => ['text' => $status],
]);
$body = json_decode($response->getBody()->getContents(), true);
dd($body);
} catch (Exception $e) {
dd($e->getMessage());
}
}
Make sure to replace 'Bearer ' . env('TWITTER_BEARER_TOKEN') with the actual bearer token if you're not using environment variables.
Also, note that I've changed the endpoint from 'https://api.twitter.com/2/tweets' to 'tweets' since we've already set the base_uri in the Guzzle client.
If you're still getting an unauthorized error, double-check your bearer token and the permissions of your Twitter app. If you're using OAuth 1.0a, you'll need to sign your requests with an access token and access token secret, which is a more complex process. In that case, you might want to consider using a package like abraham/twitteroauth for handling the OAuth signing process.