djberg96
djberg96

Reputation: 1

Authenticating with pure Ruby for the Azure ARM API

I would like to use Ruby to interface with the Azure Microsoft.Compute Resource Provider API. It's a simple REST interface, e.g. to get a VM:

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

My question is, how do I authenticate first using pure Ruby? I've seen some other language samples, but I had difficulty following them, as they were usually library wrappers. Basically, I think I need the Ruby version of the C# code provided here:

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

Thanks!

Upvotes: 0

Views: 478

Answers (1)

djberg96
djberg96

Reputation: 1

For anyone following, I worked out the answer, or at least one approach using client credentials. In short, first you need to get a oauth2 token. Then you need to set that token in all future requests. You will need a client ID, client key, and tenant ID.

Here's an example using rest-client and 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

Upvotes: 0

Related Questions