Skip to content

Instantly share code, notes, and snippets.

@carols10cents
Created October 21, 2012 23:00
Show Gist options
  • Save carols10cents/3928840 to your computer and use it in GitHub Desktop.
Save carols10cents/3928840 to your computer and use it in GitHub Desktop.
Example usage of rstatus' oauth provider capabilities
require "./example"
run Sinatra::Application
require 'bundler/setup'
require 'sinatra'
require 'oauth2'
require 'net/http'
require 'net/https'
require 'uri'
# This example heavily borrows from the Railscast on Doorkeeper, from the
# parts about testing your doorkeeper install.
# http://railscasts.com/episodes/353-oauth-with-doorkeeper?view=asciicast
# First: go to https://rstatus-staging.herokuapp.com/oauth/applications
# and create a new application.
# Enter a name for your app and a callback URL; when running this sinatra
# app locally it will be http://localhost:9292/callback
# We can actually change it per request though, this is the one we'll
# be using:
callback = "http://localhost:9292/callback"
# Fill in the following variables with the values given to you on the next
# screen:
app_id = "ENTER YOUR APP ID TOKEN HERE"
secret = "ENTER YOUR SECRET TOKEN HERE"
# If you want to run this against a different domain running the rstat.us
# code, change the value here:
base_url = "https://rstatus-staging.herokuapp.com/"
client = OAuth2::Client.new(
app_id,
secret,
site: base_url
)
rstatus_auth_url = client.auth_code.authorize_url(
redirect_uri: callback,
scope: "read write"
)
enable :sessions
get '/' do
if session[:auth_token]
"<form action='/update' method='post'><textarea name='status'></textarea><input type='submit' /></form>"
else
"<a href='#{rstatus_auth_url}'>Log in with rstat.us</a>"
end
end
get '/callback' do
access = client.auth_code.get_token(params[:code], redirect_uri: callback)
session[:auth_token] = access.token
redirect '/'
end
post "/update" do
if session[:auth_token]
uri = URI.parse("#{base_url}api/statuses/update.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path)
request.set_form_data({
:status => params[:status],
:access_token => session[:auth_token]
})
response = http.request(request)
end
redirect '/'
end
source 'http://rubygems.org'
gem "sinatra", "~> 1.3.2"
gem "oauth2", "~> 0.5.0"
GEM
remote: http://rubygems.org/
specs:
faraday (0.8.4)
multipart-post (~> 1.1)
multi_json (1.3.6)
multipart-post (1.1.5)
oauth2 (0.5.2)
faraday (~> 0.7)
multi_json (~> 1.0)
rack (1.4.1)
rack-protection (1.2.0)
rack
sinatra (1.3.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
PLATFORMS
ruby
DEPENDENCIES
oauth2 (~> 0.5.0)
sinatra (~> 1.3.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment