You’re not receiving notifications from this thread.
Rails for Beginners Part 25: Twitter Account Model Discussion
Hi! After setting the twitter_account model, before accessing Ruby console and running the "User.last.twitter_accounts" command, shouldn't we run 'rails db:migrate' to actually create the table in the database? I'm asking cause I was only able to get the query to complete after running the migrate command.
Hello friends! I connected to my twitter account using the connect button but I forgot to connect in the sign_in option.
So I received this kind of error " undefined method `twitter_accounts' for nil:NilClass" because I did not make this error bypass. How should I do to send a message to remmeber the user to connect via sign_in first? It is better to use just an alert or create a controller and all the rest if I got NilClass.
Why i don't have records of twitter accounts in the database? You can see it in the image
I don know why, but i managed to get the info creating a new account on the project. The old ones never get the twitter account
If you want to get it fixed, you may modify omniauth_callbacks_controller.rb and check if Current.user has any twitter accounts associated. If not, you have to create an entry in TwitterAccount table with Current.user[:id]. Looks like this:
class OmniauthCallbacksController < ApplicationController before_action :require_user_logged_in! def twitter if Current.user.twitter_accounts.any? twitter_account = Current.user.twitter_accounts.where(username: auth.info.nickname).first_or_initialize twitter_account.update( name:auth.info.name, username: auth.info.nickname, image: auth.info.image, token: auth.credentials.token, secret: auth.credentials.secret, ) redirect_to twitter_accounts_path, notice: "Conta do twitter conectada com sucesso!" else # If there is no twitter account associated TwitterAccount.create(user_id: Current.user[:id],username: auth.info.nickname, name:auth.info.name, image: auth.info.image, token: auth.credentials.token, secret: auth.credentials.secret, ) redirect_to twitter_accounts_path, notice: "Conta do twitter conectada com sucesso!" end end def auth request.env["omniauth.auth"] end end
Hi, I'm getting the following error "undefined method `twitter_accounts' for nil:NilClass". i can view the twitter_account model and in the user model i have also included the following "has_many :twitter_accounts". from the rails cons the hash i can also view the details to be insterted into the db. please assist.
Lol. Made the same mistake.
- Make sure to sign in the to application so there IS a Current.user.
- Then Connect your Twitter Account.
I've followed the updated instructions and I've run into some intermittent behavior where I get the
when (400..499) raise OAuth::Unauthorized, response
response myself. The best I can determine, if I start from the Connect Twitter button and get a Rails error partway through (say because I didn't call
redirect_to root_path in
OmniauthCallbacksController::twitter), then refreshing the page will lead to the above Unauthorized error.
So if you're getting this error, try fixing any underlying Rails errors and then reload the home page and click the button and see if it works!
I noticed when my twitter_account was being saved to the database that most of the fields were
Looking at the rails server logs, I noticed I received some information about elevated access.
It includes the message "You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you'll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve"
For some context -- on 4/21/2022
I created a brand new twitter account for this application.
I tied that new twitter account to a brand new developer account.
App Details: Name: @mytwitterusername Scheduled Tweets App Icon: Left it the default Description: GoRails.com Rails for Beginners Tutorial User authentication settings: OAuth 2.0: ON OAuth 1.0a: ON OAuth 2.0 Settings: Type of App = Web App OAuth 1.0a Settings: Request email from users: ON App Permissions: Read and Write Callback URI / Redirect URL: https://localhost:3000/auth/twitter/callback I had to request the new "Elevated Access" in order for my twitter_account object to contain all the requested information from the `auth` hash. To request Elevated access - https://developer.twitter.com/en/portal/products/elevated - Click the "Apply for Elevated" button. Disclaimer: I'm sharing what I did to continue onwards with the tutorial. This is by no means what you should put in your situation. I'm not responsible for your input in this request, also I'm NOT a lawyer and yada yada, essentially be honest and fill this stuff out according to your own use case. I went through their "wizard" and essentially said the following for most large paragraph entries: How will you use the Twitter API or Twitter Data? "I'm learning Ruby on Rails from Chris Oliver's Beginner Rails tutorial on GoRails.com and intend to use the Twitter API for this Scheduled Tweets application. I plan to schedule tweets to post on my own personal Twitter account for development purposes only." Are you planning to analyze Twitter Data: NO Will your App use Tweet, Retweet, Like, Follow, or Direct Message: YES *pasted explanation paragraph text from above* Do you plan to display Tweets or aggregate data about Twitter content outside Twitter? NO or YES If Yes, *pasted explanation paragraph text from above* Will your product, service, or analysis make Twitter content or derived information available to a government entity? NO NEXT, NEXT, Accept Developer Agreement. My Approval was Instant -- re-ran the process to create the twitter_account object and it successfully created. You can also verify you have Elevated access here: https://developer.twitter.com/en/portal/products
I had similar issue. I've just applied for elevated access (you need to fill the special form) and I got it. Now, I can see data.
I also requested and was granted elevated access. The process isn't very difficult and I was given elevated access immediately. Here's a link to make the request:
Had the same issue of the xref not populating on the table, Requested Elevated access on the app and it works. Looks like the problem is that on Twitter API version 2.0 unless you have elevated access you cannot retrieve data from the API. Wich means that rails would try to update a table with nil values.
I added a guard to make sure unauthenticated users could not trigger the OmniauthCallbacksController#twitter action
where require_signin is a private method I defined in application_controller
When I try to connect a Twitter User from the index page, it works fine. But, when I try from the Twitter Accounts page I get an error
"NoMethodError in OmniauthCallbacksController#twitter"
"undefined method `info' for nil:NilClass"
Rails says the error is on the line
" twitter_account = Current.user.twitter_accounts.where(username: auth.info.nickname).first_or_initialize"
What am I missing?
The OmniAuth hash doesn't return or give me any secret token. There is only the token that is sent back, so in my db the secret is always set to nil but the others values like name, image or token are saved. I have elevated access in the Twitter API, i have changed the client key et client secret multiple time,s changed the scope in omniauth.rb to add also tweet.write, but nothing of this worked. I have no idea how to solve this, does anyone encounters the same issue ?
Thanks for you help et great tutorial by the way !!