Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SquareNetMedia's avatar

Laravel 10 Posting to Twitter Profile

Hi, hoping someone can help and point me in the right direction.

I am using Laravel 10 and Livewire 10 where I have created a blog. When the user submits a new blog post I am wanting to post to Twitter. I have set up the app in twitter developers panel and have all the keys etc but I cannot get my code to work. I keep getting an unathorised error when I DD.

I'm trying to avoid using a package, as I want to understand how to use twitters API and Laravel.

Here is the Submission Code from the Livewire Component.

'<?php

namespace App\Livewire;

use Exception; use Livewire\Component; use Illuminate\Support\Facades\Http; use GuzzleHttp\Client;

class TwitterPosts extends Component { public $title; public $content;

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'),
        ],
    ]);



        $response =  $client->post('https://api.twitter.com/2/tweets', [
            'text' => $status,
        ]);

        dd($response);


    }

public function render()
{
    return view('livewire.twitter-posts');
}

}'

0 likes
4 replies
LaryAI's avatar
Level 58

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:

  1. Make sure you have the correct permissions for your Twitter app to post tweets on behalf of a user.
  2. Ensure that you're using the correct authentication method (OAuth 1.0a or OAuth 2.0 Bearer Token) based on your app's permissions.
  3. 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.

SquareNetMedia's avatar

'Bearer ' . env('TWITTER_BEARER_TOKEN') doesn't allow posting to a user profile from what I understand. You need still need to use OAUTH1.0 to connect before you can then use the api https://api.twitter.com/2/tweets to post a tweet

Please or to participate in this conversation.