Created
December 22, 2017 08:00
-
-
Save nbulaj/ff3716d1043143c01b2237c4fa34517a to your computer and use it in GitHub Desktop.
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
# Gemfile | |
source 'https://rubygems.org' | |
gem 'grape_oauth2', git: 'https://github.com/nbulaj/grape_oauth2.git' | |
gem 'grape', '~> 1.0' | |
gem 'rack-oauth2' | |
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] | |
# config.ru | |
require 'bundler/setup' | |
Bundler.setup | |
require_relative 'api' | |
run API | |
# api.rb | |
require 'grape_oauth2' | |
Grape::OAuth2.configure do |config| | |
config.client_class_name = 'Application' | |
config.access_token_class_name = 'AccessToken' | |
config.resource_owner_class_name = 'User' | |
config.access_grant_class_name = 'AccessCode' | |
config.realm = 'Custom Realm' | |
config.allowed_grant_types << 'refresh_token' | |
end | |
class Comments < Grape::API | |
resources :comments do | |
get do | |
{ value: 'test' } | |
end | |
get :protected do | |
{ value: 'works '} | |
end | |
end | |
end | |
class Posts < Grape::API | |
before { access_token_required! } | |
resources :posts do | |
get do | |
{ value: 'test' } | |
end | |
get :protected do | |
{ value: 'works '} | |
end | |
end | |
end | |
class API < Grape::API | |
version 'v2', using: :path | |
format :json | |
include Grape::OAuth2.api | |
mount Comments | |
mount Posts | |
end | |
# run application | |
$ rackup config.ru | |
# [2017-12-22 10:52:20] INFO WEBrick 1.3.1 | |
# [2017-12-22 10:52:20] INFO ruby 2.4.1 (2017-03-22) [x86_64-linux] | |
# [2017-12-22 10:52:20] INFO WEBrick::HTTPServer#start: pid=2766 port=9292 | |
$ curl -GET 'http://localhost:9292/v2/comments.json' | |
# {"value":"test"} | |
$ curl -GET 'http://localhost:9292/v2/posts.json' | |
# {"error":"unauthorized"} | |
$ curl -GET 'http://localhost:9292/v2/posts.json?access_token=123123' | |
# ... | |
# NameError at /v2/posts.json | |
# uninitialized constant AccessToken | |
# 127.0.0.1 - - [22/Dec/2017:10:52:24 +0300] "GET /v2/comment HTTP/1.1" 404 - 0.0313 | |
# 127.0.0.1 - - [22/Dec/2017:10:52:27 +0300] "GET /v2/comments HTTP/1.1" 200 16 0.0133 | |
# 127.0.0.1 - - [22/Dec/2017:10:52:32 +0300] "GET /v2/posts HTTP/1.1" 401 24 0.0442 | |
# 127.0.0.1 - - [22/Dec/2017:10:52:42 +0300] "GET /v2/posts.json HTTP/1.1" 401 24 0.0008 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment