【发布时间】:2017-10-31 06:08:07
【问题描述】:
Ruby 版本:2.4.1p111
我有一个 ruby 脚本,我正在尝试通过复制 Bearer 令牌然后将请求传递给我想要访问的应用程序 API。这工作正常,现在我想通过取出我的不记名令牌黑客来增强脚本,而不是通过 oauth 获取令牌来访问 API。 API 作为应用服务托管在 Azure 上。我有以下信息: 1.client_id 2.client_secret 3.网站地址:https://login.microsoftonline.com/someapp.com 4.redirect_uri:https://someapp/login
我使用 gem 'oauth2' 并按照 gem wiki 页面上的示例进行操作。以下是我所做的: 宝石维基页面:https://github.com/intridea/oauth2
require 'oauth2'
#Initializing a client
client = OAuth2::Client.new('client_id', 'client_secret', :site
=>'https://login.microsoftonline.com/someapp.com')
#Creating a auth url
auth_url = client.auth_code.authorize_url(:redirect_uri
=>'https://someapp/login')
#After auth_url is created, I then send a request to access the token
token = client.auth_code.get_token('authorization_code_value', :redirect_uri
=> 'https://someapp/login', :headers => {'Authorization' => 'Basic
some_password'})
response = token.get('/api/resource', :params => { 'query_foo' => 'bar' })
- 我不明白的是令牌请求中的'authorization_code_value'参数是什么?
- 标题选项哈希显示“授权”=>“基本密码”。这将是什么以及放在这里的内容
- 我注意到的另一件事是,gem 在创建 auth_url 时会从站点地址中删除应用程序的名称,并创建以 https://login.microsoftonline.com/oauth/authorize 开头的 url。这是不正确的,因为当我在浏览器中复制粘贴此 url 时,它不会打开。但是正确的 url 应该是:https://login.microsoftonline.com/someapp.com/oauth2/authorize。当我在浏览器中使用它时,我会看到 microsoft 登录,在输入凭据后,用户将被重定向到我们的应用程序并且登录成功。
总之,我想做的是在我的脚本中获取一个令牌,然后使用该令牌向 API 发出请求。这是正确的宝石吗?或者更好/更简单的方法来实现这一点?
如果需要更多信息,请告诉我。期待你们的帮助指导。已经花了几天,我无法弄清楚。
【问题讨论】:
标签: ruby azure oauth oauth-2.0 azure-active-directory