Hi folks,
I'm just learning oauth2 here, trying to interface with Windows Azure's
REST API, and having some trouble. It seems all of the examples assume an
auth code approach where you dig an authentication code out of a params
hash sent to a reply-url. For example, this approach works:
require 'oauth2'
# Actual values obfuscated to protect the innocent
CLIENT_ID = "XXXXXX"
CLIENT_KEY = "YYYYYY"
APP_ID_URI = "http://localhost:3000"
TENANT_ID = "ZZZZZZ"
AUTHORITY = "https://login.windows.net"
RESOURCE_ID = "https://management.azure.com/"
AUTHORIZE_URL = TENANT_ID + "/oauth2/authorize"
TOKEN_URL = TENANT_ID + "/oauth2/token"
SUBSCRIPTION_ID = "XXXXXXXX-YYYYY"
client = OAuth2::Client.new(
CLIENT_ID,
CLIENT_KEY,
:site => AUTHORITY,
:authorize_url => AUTHORIZE_URL,
:token_url => TOKEN_URL
)
# Get code if necessary
url = client.auth_code.authorize_url(
:response_mode => 'query',
:response_type => 'code',
:redirect_uri => APP_ID_URI,
:resource => RESOURCE_ID
)
# Code was sent to localhost:3000, thanks webrick
code = "XXXXXYYYYYY"
# Get token now that we have a code
token = client.auth_code.get_token(
code,
:redirect_uri => APP_ID_URI,
:expires_at => Time.now + (365 * 24 * 60 * 60)
)
# Now setup our azure connection
conn = Faraday.new(:url => 'https://management.azure.com') do |f|
f.request :url_encoded
f.adapter Faraday.default_adapter
end
# REST API
url = "/subscriptions/#{SUBSCRIPTION_ID}/resourceGroups"
url += "?api-version=2015-01-01"
resp = conn.get do |req|
req.url url
req.headers['Content-Type'] = 'application/json'
req.headers['Authorization'] = 'Bearer '+ token.token
end
# Huzzah, we have json!
p resp.body
But this is obviously unsuitable for client side scripting. I've been
looking at other approaches (client credentials, password, etc) but
can't seem to make them work.
I was hoping it was something as simple as:
token = client.client_credentials.get_token
But that gives me this error:
'client_assertion' or 'client_secret' is required for the
'client_credentials' grant type
Any guidance appreciated.
Regards,
Dan