How get user info from twitter via API?

2019-08-22 17:33发布

问题:

I'm developing API to integrate rails app with twitter.

I receive from client only:


{
  provider:     'twitter',
  user_id:      'USER_ID',
  access_token: 'ACCESS_TOKEN'
}

But I need create user and save it into database if user is not registered etc. I have models user and authentication (user has many authentications):


class User
  validate :first_name, :last_name, :email, 
           presense: true 

  has_many :providers
endd

class Authentication
  validate :provider, :user_id, :access_token, :refresh_token, :expires_at, 
           presense: true

  belongs_to :user
end

Twitter app is already created and all permissions are set up correctly.

Problem:

To save user with authentication into DB - I need to have all fields.

Question:

If I have provider, user_id and access_token, how can I get additional fields: email, first_name, last_name, refresh_token and expires_at.

Which API request should I make?

P.S. I found that with gem "twitter" I can get user information:


@client = Twitter::REST::Client.new do |config|
  config.consumer_key    = "TWITTER_API_KEY"
  config.consumer_secret = "TWITTER_API_SECRET"
end
user_info = @client.user 'USER_ID'

It returns an object with user info, which contain first_name and last_names. But I still don't know how to get email, refresh_token and expires_at fields. :(

回答1:

Hmm... I think you're trying to create a logging for your app, is it?

There is not API request for this exactly, but you can use the twitter gem for this. Hope it works for you.

https://github.com/sferik/twitter