Created
October 7, 2014 16:40
-
-
Save mager/5135d9266e753b36bc2d to your computer and use it in GitHub Desktop.
Example SmartApp Server using OAuth 2
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
require './server' | |
run Sinatra::Application |
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
source "https://rubygems.org" | |
gem 'sinatra' | |
gem 'oauth2' | |
gem 'json' |
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
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