首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Ruby /API检索Azure中的访问令牌

用Ruby /API检索Azure中的访问令牌
EN

Stack Overflow用户
提问于 2017-06-27 06:32:55
回答 2查看 533关注 0票数 2

我试图检索访问令牌使用天青应用程序客户端id和客户端机密。最初,我尝试使用以下python代码块

代码语言:javascript
运行
复制
import adal
context = adal.AuthenticationContext(AUTHORITY)
token = context.acquire_token_with_client_credentials(
    "https://management.azure.com/",
    CLIENT_ID,
    CLIENT_SECRET)

这是不带任何问题地返回令牌。我也试图使用Azure来跟踪https://github.com/Azure/azure-sdk-for-ruby中的内容,但仍然无法获得任何示例。我是红宝石的初学者,能不能请一些人与我分享这方面的经验?

从这里开始在上添加到我的帖子中

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

代码语言:javascript
运行
复制
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中,它对我起了作用。

下面是完整的错误跟踪。

代码语言:javascript
运行
复制
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>'

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

EN

回答 2

Stack Overflow用户

发布于 2017-06-27 08:41:56

韦尔普,他是复制者\粘贴:

代码语言:javascript
运行
复制
# 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

资源

票数 2
EN

Stack Overflow用户

发布于 2017-06-28 08:04:25

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

首先,通过adal安装gem install adal

然后,

  1. 按照adal CLIENT_SECRET使用acquire_token_for_client方法通过下面的代码获取访问令牌。 要求'adal‘AUTHORITY = 'login.windows.net’auth_ctx =ADAL::AuthenticationContext.new(权威,租户) client_cred = ADAL::ClientCredential.new(CLIENT_ID,CLIENT_SECRET)结果=ADAL::ClientCredential.new client_cred)
  2. 按照adal PASSWORD通过下面的代码获取访问令牌。 要求'adal‘AUTHORITY = 'login.windows.net’user_cred =ADAL::UserCredential.new(用户名、密码) ctx =user_cred=ctx.acquire_token_for_user(https://management.azure.com/"、CLIENT_ID、user_cred)放入result.access_token

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44773632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档