ruby-on-rails - 使用 Ruby SDK/API 检索 Azure 中的访问 token

标签 ruby-on-rails ruby azure azure-sdk-ruby azure-sdk-for-ruby

我正在尝试使用 azure 应用程序客户端 id 和 客户端 key 检索访问 token 。最初我尝试使用以下 python 代码块

import adal
context = adal.AuthenticationContext(AUTHORITY)
token = context.acquire_token_with_client_credentials(
    "https://management.azure.com/",
    CLIENT_ID,
    CLIENT_SECRET)

这将返回 token ,没有任何问题。 我正在尝试按照 https://github.com/Azure/azure-sdk-for-ruby 中的内容使用 Azure Ruby SDK 执行相同操作但仍然无法获得任何样本可供遵循。 我是 ruby 的初学者,有人可以与我分享他们的经验吗?

从这里添加到我的帖子

嗨, 非常感谢您的支持 。 我按照您的代码编写了我的代码,如下所示:

require 'adal'

TENANT=<TENANT ID>
CLIENT_ID= <CLIENT_ID>
CLIENT_SECRET =<CLIENT_SECRET >
AUTHORITY = "https://login.windows.net"
auth_ctx = ADAL::AuthenticationContext.new(AUTHORITY, TENANT)
client_cred = ADAL::ClientCredential.new(CLIENT_ID, CLIENT_SECRET)
result = auth_ctx.acquire_token_for_client("https://management.azure.com/", client_cred)
puts result.access_token

但是我收到如下错误, check_host':错误组件(预期的主机组件)

在 Python 中它对我有用。

以下是完整的错误跟踪。

F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:593:in `check_host': bad component(expected host component): [https://login.windows.net] (URI::InvalidComponentError)
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:634:in `host='
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:668:in `hostname='
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:187:in `initialize'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:134:in `new'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:134:in `build'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/http.rb:62:in `build'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/authority.rb:95:in `token_endpoint'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:228:in `oauth_request'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:182:in `request_no_cache'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:171:in `request'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:84:in `get_for_client'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/authentication_context.rb:78:in `acquire_token_for_client'
    from F:/Selenium_Workspace_HSBC/dsi/azureadallogin.rb:9:in `<main>'

在我看来,AUTHORITY 常量有问题。有人可以在这里提供一些线索吗?

最佳答案

好吧,他是复制\粘贴:

# Create authentication objects
token_provider = MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, secret)
credentials = MsRest::TokenCredentials.new(token_provider)
# Create a client - a point of access to the API and set the subscription id
client = Azure::ARM::Resources::ResourceManagementClient.new(credentials)
client.subscription_id = subscription_id

https://github.com/Azure/azure-sdk-for-ruby/tree/master/management/azure_mgmt_resources

关于ruby-on-rails - 使用 Ruby SDK/API 检索 Azure 中的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773632/

相关文章:

ruby-on-rails - Rails 3 中不显眼的 RJS 文件被认为是个好主意吗?

ruby-on-rails - 如何使用 RSpec 测试 Pusher

javascript - 如何从 Ruby 参数中的 URL 获取电子邮件(具有 '+' )

java - com.microsoft.azure.documentdb.DocumentClientException 日期 header 不符合所需的格式

angular - 发现文档中的发行者无效,预期为 : angular-oauth2-oidc with Azure B2C

azure - 如何根据消息类型将事件中心消息路由到不同的 Azure 函数

ruby-on-rails - 向导式表单的验证组

ruby-on-rails - 预编译 Assets 失败且 rake 中止

python - 相当于 Ruby "is"

ruby-on-rails - 错误 : Error installing rails zeitwerk requires Ruby version >= 2. 4.4