Skip to content

Instantly share code, notes, and snippets.

@mager
Created October 7, 2014 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mager/5135d9266e753b36bc2d to your computer and use it in GitHub Desktop.
Save mager/5135d9266e753b36bc2d to your computer and use it in GitHub Desktop.
Example SmartApp Server using OAuth 2
require './server'
run Sinatra::Application
source "https://rubygems.org"
gem 'sinatra'
gem 'oauth2'
gem 'json'
require 'bundler/setup'
require 'sinatra'
require 'oauth2'
require 'json'
client_id = '0e1e1aa7-8404-4c96-a1c3-cdad2671bb9a'
api_key = '1d041ba7-0e9b-4ad0-b5ca-5fb667d221af'
redirect_uri = 'http://smartappserver.herokuapp.com/oauth/callback'
options = {
site: 'https://graph.api.smartthings.com',
authorize_url: '/oauth/authorize',
token_url: '/oauth/token'
}
client = OAuth2::Client.new(client_id, api_key, options)
get '/' do
%(<a href="/authorize">Connect with SmartThings</a>)
end
get '/authorize' do
url = client.auth_code.authorize_url(redirect_uri: redirect_uri, scope: 'app')
redirect url
end
get '/oauth/callback' do
response = client.auth_code.get_token(params[:code], redirect_uri: redirect_uri, scope: 'app')
response.token
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment