Ask A Question


You’re not receiving notifications from this thread.

Rails for Beginners Part 33: Twitter API Discussion

I may have missed something during the initial configuration but I had only set read permissions for the app so the .update(body) method was obvisously returning Twitter::Error::Unauthorized (Read-only application cannot POST.). If someone encouters the same issue, go to, find your app, in "settings" switch the app permissions to "Read, Write, and Direct Messages", then in "keys and tokens", revoke access token & secret for your test account and then disconnect and reconnect the account through your rails app running locally.


I just ran into this as well, twitter apps are new to me, though I have plans to use them.


You saved me! I ran into as well and was scratching my head.

Edit Actually it looks like I am having a different problem. I am getting Unauthorized (Could not authenticate you) back when trying to use the twitter gem. So I am doing something wrong with saving out credentials or configuring the client.


Hi Cris, my problem is: If I want to disconect it is giving me an error.

ActiveRecord::InvalidForeignKey in TwitterAccountsController#destroy
SQLite3::ConstraintException: FOREIGN KEY constraint failed

I think it is for the update tweet_id: tweet_id


@David, check whether there's a comma at the end of
Rails.application.credentials.dig(:twitter, :api_key)
Rails.application.credentials.dig(:twitter, :api_secret)

I had the commas at the end, because of copy paste so I was getting Unauthorized error.


@Adalberto, @Konstantinos Gallis awnser your question in another video. Just do:

Checkout the video 39 (

Go in the file twitter_account.rb and replace:
has_many :tweets

has_many :tweets, dependent: :destroy


mannn thanks so much u saved me


@Paulo, Thank you! After regenerating my keys a ton of times, enabling "read and write" in the OAuth settings checking so many other things for the "Read-only application cannot POST. (Twitter::Error::Unauthorized)" error I was getting, your comment was the key to getting this working!


If you get

Twitter::Error::Unauthorized (Read-only application cannot POST.)


Could not authorize you

Go to twitter developer setting and under App permissions enable Read, Write and Direct Messages. It was on Read Only for me before.

And then regenerate all the keys/tokens + update them again in your app.

Also helps to restart the servers/console again. Fixed the problem for me.


I tried your suggestion and still get the same error


Hello, I am getting this issue when I try to publish the tweet running this command: Tweet.last.publish_to_twitter!

[["id", 12], ["LIMIT", 1]]
/home/fherrerao/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/twitter-7.0.0/lib/twitter/rest/request.rb:97:in `fail_or_return_response_body': Your credentials do not allow access to this resource. (Twitter::Error::Forbidden)

I have read/write/Direct messages permissions.

If someone can help me I will be grateful.


Hey Chris,

I guess we need to update to Gem x, from the same developer.

I'm getting this error: `fail_or_return_response_body': You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: (Twitter::Error::Forbidden)

It sounds the the twitter deprecated the V1.1 (that Gem Twitter is calling)


Guilherme Domingos as you say we need to move to gem x, after installing the gem ( bundle add x ), try this:

// twitter_account.rb
def client
x_credentials = {
api_key: Rails.application.credentials.dig(:twitter, :api_key),
api_key_secret: Rails.application.credentials.dig(:twitter, :api_secret_key),
access_token: token,
access_token_secret: secret,

// tweet.rb
def publish_to_twitter!
tweet ="tweets", "{\"text\":\"#{body}\"}")
update(tweet_id: tweet["data"]["id"])

This works for me, I hope it helps.


Worked it, thanks Gustavo


Thank you for this!


Works for me thanks Gustavo!
And I needed to go back to the X Developer portal, create a project and associate it with my stand alone app.


This works for me, I hope it helps.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 84,256+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.