In my app a user can login or register with Facebook, Twitter or Instagram. Fetching and validating access token are done in application (Android and iOS). Now when they send a request for registration, I need to validate the access tokens. Do I have to validate each access token manually like:
facebook_data = HTTParty.get("https://graph.facebook.com/me", query: {
access_token: params[:access_token]
}).parsed_response
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(facebook_data)
For Facebook I would recommend Koala: https://github.com/arsduo/koala
For Twitter: https://github.com/sferik/twitter
For Instagram this is the official Library: https://github.com/Instagram/instagram-ruby-gem
I had to write custom method,(In case any one needs it):
# Validates fb token
def self.validate_facebook_token token
status = false
begin
@graph = Koala::Facebook::API.new(token)
status = true if @graph.get_object("me")
ensure
return status
end
end
# Validates fb twitter
def self.validate_twitter_token token, secret
status = false
client = TwitterOAuth::Client.new(
consumer_key: APP_CONFIG["TWITTER_CUSTOMER_KEY"],
consumer_secret: APP_CONFIG["TWITTER_CUSTOMER_SECRET"],
token: token,
secret: secret
)
status = true if client.authorized?
return status
end
# validates instagram token
def self.validate_instagram_token token, provider_id
status = false
instagram_reponse = HTTParty.get("#{APP_CONFIG['INSTAGRAM_URL']}/users/#{provider_id}", query: {
access_token: token
}).parsed_response
status = true if instagram_reponse['meta'].present? && instagram_reponse['meta']['code'] == 200
return status
end
Gems used:
gem 'koala'
gem 'twitter_oauth'
gem 'httparty'
Do let me know if you have a better one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.