?

Log in

No account? Create an account
Azure and client connections - Testing 1,2,3... [entries|archive|friends|userinfo]
djberg96

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

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

Azure and client connections [Jun. 18th, 2015|08:52 am]
djberg96
[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(
  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
linkReply