【问题标题】:Authenticating with pure Ruby for the Azure ARM API使用纯 Ruby 对 Azure ARM API 进行身份验证
【发布时间】:2015-07-27 16:01:34
【问题描述】:

我想使用 Ruby 与 Azure Microsoft.Compute Resource Provider API 进行交互。这是一个简单的 REST 接口,例如获取虚拟机:

https://management.azure.com/subscriptions/my_subscription_id/resourceGroups/my_resource_group/providers/Microsoft.Compute/virtualMachines/vm_name/InstanceView?api-version=2015-1-1

我的问题是,如何首先使用纯 Ruby 进行身份验证?我看过一些其他语言示例,但我很难理解它们,因为它们通常是库包装器。基本上,我想我需要这里提供的 C# 代码的 Ruby 版本:

https://msdn.microsoft.com/en-US/library/azure/dn790557.aspx

谢谢!

【问题讨论】:

    标签: ruby azure


    【解决方案1】:

    对于任何关注的人,我都想出了答案,或者至少是一种使用客户端凭据的方法。简而言之,首先您需要获得一个 oauth2 令牌。然后,您需要在以后的所有请求中设置该令牌。您将需要一个客户端 ID、客户端密钥和租户 ID。

    这是一个使用 rest-client 和 json 的示例:

    require 'rest-client'
    require 'json'
    
    token_url = "https://login.windows.net/" + tenant_id + "/oauth2/token"
    
    resp = RestClient.post(
       token_url,
       :grant_type    => 'client_credentials',
       :client_id     => 'xxxxx',
       :client_secret => 'yyyyy',
       :resource      => 'https://management.azure.com'
    )
    
    token = 'Bearer ' + JSON.parse(resp)['access_token']
    
    # Now attach that token to all future requests:
    
    resp = RestClient.get(
      "https://management.azure.com/providers?api-version=2015-01=01",
      :content_type => 'application/json',
      :authorization => token
    )
    
    p resp.body
    

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 2015-08-09
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多