All threads / Rails for Beginners Part 33: Twitter API Discussion
Ask A Question

Notifications

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 https://developer.twitter.com/en/portal/dashboard, 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) https://rdoc.info/gems/twitter/Twitter/Error#Unauthorized-constant 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)
and
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 (https://gorails.com/episodes/rails-for-beginners-part-39-dependent-destroy-model-associations)

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

with:
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!

Reply

If you get

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

Or

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

Reply

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.

Reply
Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.