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 - accept_nested_attributes_for :allow_destroy, :_destroy 不工作

ruby-on-rails - 炼油厂 CMS : generating migrations for existing models within an engine

ruby-on-rails - Rails 哈希树到数组

ruby-on-rails - 我需要什么基本 gem ?

sql - 使用 Web 角色在 Azure 云服务托管上使用 SQL Server CE 3.5

c# - 更新 Azure 服务总线队列上已有的代理消息

ruby-on-rails - Rspec、Paperclip、Fabrication、不保存到文件系统的有效对象

ruby-on-rails - POW/Rails 错误:启动 applicationBundler::GemNotFound 时出错:在任何来源中都找不到 minitest-4.7.5

ruby-on-rails - Rail3 rake 测试任务不运行测试

c# - 当我尝试从部署 Azure Web 角色的计算机打开站点时出现 404