我正在使用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. 无效的常量API ::与葡萄和葡萄实体
- 3. 为葡萄牙葡萄牙
- 4. Rails的葡萄API版本
- 5. 属性缺失(葡萄API)
- 6. Google Weather API - 葡萄牙语
- 7. 葡萄kaminari与葡萄建造者
- 8. 葡萄API和HTTP摘要验证
- 9. 在葡萄牙
- 10. 如何在没有葡萄api的情况下使用葡萄实体?
- 11. 葡萄api忽略PUT/POST参数
- 12. 权威政策_scope与葡萄API
- 13. 验证葡萄API与门卫
- 14. 葡萄rspec上传
- 15. 葡萄/ Rack :: Mount :: Route
- 16. groovy葡萄详细
- 17. Stubbing葡萄帮手
- 18. WordNet葡萄牙语
- 19. 主机和端口的葡萄实体
- 20. SQLite和葡萄牙文字符
- 21. 的Rails 3葡萄问题
- 22. 葡萄在一个文件
- 23. 安装葡萄依赖
- 24. 铁路葡萄路线
- 25. 葡萄分享rails activerecord-session_store
- 26. 选项葡萄申述
- 27. 招摇不摇葡萄
- 28. 葡萄:动态前缀?
- 29. 获取Groovy的葡萄!
- 30. 葡萄与动态路由