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.

Reply

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

Reply

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.

Reply

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

Reply

@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.

Reply

@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

Reply

mannn thanks so much u saved me

Reply

@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.

Reply

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

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)

Reply

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,
}
X::Client.new(**x_credentials)
end

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

This works for me, I hope it helps.

Reply

Worked it, thanks Gustavo

Reply

Thank you for this!

Reply

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.

Reply

This works for me, I hope it helps.

Reply
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.