Unable to connect to twitter stream api, receiving 403 forbidden

I’m trying to connect to Twitter’s stream endpoint https://api.twitter.com/2/tweets/search/stream (docs) using WebSockex but it does not work.


  @url "https://api.twitter.com/2/tweets/search/stream"
  @headers [
    {"Authorization", "Bearer REDACTED"}

  use WebSockex

  def start_link(state) do
    WebSockex.start_link(@url, __MODULE__, state, extra_headers: @headers)

  def handle_frame({type, msg}, state) do
    IO.puts("Received Message - Type: #{inspect(type)} -- Message: #{inspect(msg)}")
    {:ok, state}

  def handle_cast({:send, {type, msg} = frame}, state) do
    IO.puts("Sending #{type} frame with payload: #{msg}")
    {:reply, frame, state}

The socket fails to connect and returns %WebSockex.RequestError{code: 403, message: "Forbidden"}. I know my bearer token is valid because I am able to connect using curl e.g. curl https://api.twitter.com/2/tweets/search/stream -H "Authorization: Bearer $ACCESS_TOKEN".

For reference here are Ruby, Python & Javascript examples of connection to this endpoint`

I’m open to using any library to do this but WebSockex seems the most promising. I’ve tried quite a few other libraries as well.


1 Like

Welcome to elixir community.

Twitter streaming api v2 supports HTTP. I did not find any documentation that it supports Websocket.

You can try elixir library - extwitter | Hex

You can check these libraries and implement same with http client in elixir.


Hi there, welcome to the forum!

For future reference when posting code, you need 3 backticks ``` at the start and end of your code.


Thanks for the feedback! To be honest I wasn’t clear if it was a socket or not but what you said makes sense so thanks for clearing that up. Ill take another look at ExTwitter but it doesn’t support using bearer tokens for auth, but I can probably learn from their implementation. In general would you say this guide is a relevant to what I’m trying to do? Download Large Files with HTTPoison Async Requests

1 Like

You can try using HTTPoison/hackney. Alternatively you can use Mint.

You can look at this - twitter_stream/twitter_stream.ex at v0.2.6 · thekeele/twitter_stream · GitHub

You have to be looking at consuming streaming responses in library you will be using.

Skimmed through it - looks like it might be useful for you.

1 Like

thanks for the awesome information.

1 Like