0

I'm trying to fetch tweets using the Twitter API v2 within my Ruby on Rails application, but I'm encountering issues with the implementation. I've followed the provided guides and examples, but the code isn't working as expected. I've verified my Twitter API credentials, but I'm still facing problems. Can someone help me troubleshoot the issue?

TwitterClient = Twitter::REST::Client.new do |config|
  config.consumer_key        = ENV['TWITTER_CONSUMER_KEY']
  config.consumer_secret     = ENV['TWITTER_CONSUMER_SECRET']
  config.access_token        = ENV['TWITTER_ACCESS_TOKEN']
  config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end

I'm consistently getting an error message "Could not authenticate you" when I attempt to fetch tweets from a specific username. Here's the line of code that's causing the issue:

tweets = TwitterClient.user_timeline('username')
@tweets = tweets.map { |tweet| tweet.text }
respond_to do |format|
  format.xml { render xml: @tweets }
  format.json { render json: @tweets }
end

Also when I am running it directly on rails console I am getting this error:

Twitter::Error::Forbidden (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: https://developer.twitter.com/en/portal/product)

Additional Information:

  • I've verified that my Twitter API credentials are correctly set up as environment variables.
  • I've ensured that the twitter gem is installed in my application.
  • The code seems to be connecting to the Twitter API, but I'm not getting the expected results.

I'm hoping to receive assistance with troubleshooting the issue I'm facing while trying to fetch tweets using the Twitter API v2 in my Ruby on Rails application.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Demo
  • 1
  • 1

0 Answers0