我正在使用Grape框架爲我的rails應用程序創建API。我正在嘗試不同的認證可能性。有人可以舉一個簡單的使用OAuth進行身份驗證的例子嗎?這裏葡萄API和OAuth
2
A
回答
1
0
更實際的例子,你可以在GrapeOAuth2 gem找到。您所需要的只是創建3個模型,代表您的客戶,令牌和資源所有者,安裝默認端點並保護您的API。
因此,創建3種型號的使用ORM和安裝默認的OAuth2令牌端點到您的API:
module Twitter
class API < Grape::API
version 'v1', using: :path
format :json
prefix :api
helpers GrapeOAuth2::Helpers::AccessTokenHelpers
# What to do if somebody will request an API with access_token
# Authenticate token and raise an error in case of authentication error
use Rack::OAuth2::Server::Resource::Bearer, 'OAuth API' do |request|
AccessToken.authenticate(request.access_token) || request.invalid_token!
end
# Mount default Grape OAuth2 Token endpoint
mount GrapeOAuth2::Endpoints::Token
# ...
end
end
可用路線:
POST /oauth/token
POST /oauth/revoke
然後保護所需的端點與access_token_required!
方法:
module Twitter
module Resources
class Status < Grape::API
before do
access_token_required!
end
resources :status do
get do
{ current_user: current_resource_owner.username }
end
end
end
end
end
查看README查看更詳細的示例(s實現一個和可定製的)。
相關問題
- 1. 導軌 - 葡萄API
- 2. 爲葡萄牙葡萄牙
- 3. 屬性缺失(葡萄API)
- 4. 葡萄kaminari與葡萄建造者
- 5. 葡萄API和HTTP摘要驗證
- 6. 在葡萄牙
- 7. 如何在沒有葡萄api的情況下使用葡萄實體?
- 8. 驗證葡萄API與門衛
- 9. 權威政策_scope與葡萄API
- 10. 葡萄rspec上傳
- 11. 葡萄/ Rack :: Mount :: Route
- 12. WordNet葡萄牙語
- 13. Stubbing葡萄幫手
- 14. groovy葡萄詳細
- 15. SQLite和葡萄牙文字符
- 16. 葡萄分享rails activerecord-session_store
- 17. 獲取Groovy的葡萄!
- 18. 葡萄與動態路由
- 19. 葡萄在一個文件
- 20. 鐵路葡萄路線
- 21. 葡萄:動態前綴?
- 22. 招搖不搖葡萄
- 23. 獲取葡萄牙語 - Android
- 24. 葡萄酒評論網站數據庫設計策略與前10名列表和特色葡萄酒
- 25. 如何在葡萄api應用程序中分割東西?
- 26. 葡萄API(swagger doc)'desc'的全局配置
- 27. 如何添加規格測試葡萄api在軌道2.3?
- 28. 如何寫Rspec的一個紅寶石API使用葡萄
- 29. SImplecov - 不正確的葡萄API測試覆蓋率
- 30. 如何通過葡萄API獲得路線