Last active
December 14, 2015 10:18
-
-
Save hinke/5070864 to your computer and use it in GitHub Desktop.
A template to get started with OAuth 2.0 authentication with Readmill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :readmill_client_id, "xxx" | |
set :readmill_client_secret, "xxx" | |
get '/auth' do | |
callback = "http://myapp.com/callback" | |
redirect "https://readmill.com/oauth/authorize?response_type=code&client_id=#{settings.readmill_client_id}&redirect_uri=#{callback}&scope=non-expiring" | |
end | |
get '/callback' do | |
callback = "http://myapp.com/callback" | |
token_params = { | |
:grant_type => 'authorization_code', | |
:client_id => settings.readmill_client_id, | |
:client_secret => settings.readmill_client_secret, | |
:redirect_uri => callback, | |
:code => params[:code], | |
:scope => 'non-expiring' | |
} | |
resp = JSON.parse(RestClient.post("https://readmill.com/oauth/token.json", token_params).to_str) rescue nil | |
data = fetch_and_parse("https://api.readmill.com/v2/me.json", resp['access_token']) | |
#data is now the authenticated user | |
end | |
def fetch_and_parse(uri, token) | |
url = "#{uri}?client_id=#{settings.readmill_client_id}" | |
url = "#{url}&access_token=#{token}" if token | |
content = RestClient.get(url, :accept => :json).to_str | |
JSON.parse(content) rescue nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment