Skip to content

Instantly share code, notes, and snippets.

@hinke
Last active December 14, 2015 10:18
Show Gist options
  • Save hinke/5070864 to your computer and use it in GitHub Desktop.
Save hinke/5070864 to your computer and use it in GitHub Desktop.
A template to get started with OAuth 2.0 authentication with Readmill
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