Log in

Azure and client connections - Testing 1,2,3... [entries|archive|friends|userinfo]

[ website | Netrunner Tracker ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

[Links:| Ruby Home RubyForge RAA comp.lang.ruby Ruby Documentation ]

Azure and client connections [Jun. 18th, 2015|08:52 am]
[Tags|, ]

For anyone out there wondering how to do client side connections with Ruby and Azure, this should get you going. It took me a while to figure this out because the vast majority of the examples online assume you're doing this strictly within a web app and send the token to a reply-url, which is obviously unsuitable for scripting. So, here you go.

require 'rest-client'
require 'json'

token_url = "https://login.windows.net/" + tenant_id + "/oauth2/token"

resp = RestClient.post(
  :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(
:content_type => 'application/json',
:authorization => token

p resp.body