使用 Ruby SDK/API 在 Azure 中检索访问令牌



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

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

这是返回令牌没有任何问题。 我正在尝试按照 https://github.com/Azure/azure-sdk-for-ruby 中的内容使用 Azure Ruby SDK 做同样的事情,但仍然无法获得任何要遵循的示例。 我是红宝石的初学者,可以请一些机构与我分享他们的经验吗?

从这里添加到我的帖子

你好 非常感谢您的支持。 我遵循了您的代码并编写了我的代码,如下所示,遵循您的代码

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>'

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

Welp,他是复制\粘贴:

# 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

否则,可以使用 ADAL for Ruby 库来获取访问令牌,就像使用 Python ADAL 作为发布的代码一样。

首先,通过gem install adal安装adal

然后

  1. 按照带有CLIENT_ID&CLIENT_SECRETadal示例,使用方法acquire_token_for_client通过下面的代码获取访问令牌。

    require 'adal'
    AUTHORITY = '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
    
  2. 按照USERNAME&PASSWORDadal示例进行操作,通过以下代码获取访问令牌。

    require 'adal'
    AUTHORITY = 'login.windows.net'
    user_cred = ADAL::UserCredential.new(username, password)
    ctx = ADAL::AuthenticationContext.new(AUTHORITY_HOST, TENANT)
    result = ctx.acquire_token_for_user("https://management.azure.com/", CLIENT_ID, user_cred)
    puts result.access_token
    

希望对您有所帮助。

最新更新