Retrieve access token in Azure with Ruby SDK/APIs

637 Views Asked by At

I am trying to retrieve access token using azure app client id and client secret . Initially I tried with the following python code block

import adal
context = adal.AuthenticationContext(AUTHORITY)
token = context.acquire_token_with_client_credentials(
    "https://management.azure.com/",
    CLIENT_ID,
    CLIENT_SECRET)

This is returning the token without any issue . I am trying to do the same using Azure Ruby SDK following the contents in https://github.com/Azure/azure-sdk-for-ruby but still not able to get any sample to follow . I am a beginner in ruby ,can some body please share their experience with me on this ?

Added to my post from here on

Hi , Many thanks for your support . I followed you code and written my code like the below one following your code

require 'adal'

TENANT=<TENANT ID>
CLIENT_ID= <CLIENT_ID>
CLIENT_SECRET =<CLIENT_SECRET >
AUTHORITY = "https://login.windows.net"
auth_ctx = ADAL::AuthenticationContext.new(AUTHORITY, TENANT)
client_cred = ADAL::ClientCredential.new(CLIENT_ID, CLIENT_SECRET)
result = auth_ctx.acquire_token_for_client("https://management.azure.com/", client_cred)
puts result.access_token

But I am getting an error like the following , check_host': bad component(expected host component)

In Python it worked for me though .

Following is the full error trace .

F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:593:in `check_host': bad component(expected host component): [https://login.windows.net] (URI::InvalidComponentError)
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:634:in `host='
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:668:in `hostname='
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:187:in `initialize'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:134:in `new'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/generic.rb:134:in `build'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/2.2.0/uri/http.rb:62:in `build'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/authority.rb:95:in `token_endpoint'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:228:in `oauth_request'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:182:in `request_no_cache'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:171:in `request'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/token_request.rb:84:in `get_for_client'
    from F:/All_Ruby_On_Rails/ruby-2.2.6-x64-mingw32/lib/ruby/gems/2.2.0/gems/adal-1.0.0/lib/adal/authentication_context.rb:78:in `acquire_token_for_client'
    from F:/Selenium_Workspace_HSBC/dsi/azureadallogin.rb:9:in `<main>'

It looks to me the AUTHORITY constant has the issue .Can anybody provide some clue here ?

2

There are 2 best solutions below

2
4c74356b41 On

Welp, he's the copy\paste:

# Create authentication objects
token_provider = MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, secret)
credentials = MsRest::TokenCredentials.new(token_provider)
# Create a client - a point of access to the API and set the subscription id
client = Azure::ARM::Resources::ResourceManagementClient.new(credentials)
client.subscription_id = subscription_id

https://github.com/Azure/azure-sdk-for-ruby/tree/master/management/azure_mgmt_resources

2
Peter Pan On

Otherwise, you can use the ADAL for Ruby library to get the access token like using Python ADAL as the code you post.

First of all, install adal via gem install adal.

Then,

  1. Follow the adal sample with CLIENT_ID & CLIENT_SECRET to get the access token via the code below using the method acquire_token_for_client.

    require 'adal'
    AUTHORITY = 'login.windows.net'
    auth_ctx = ADAL::AuthenticationContext.new(AUTHORITY, TENANT)
    client_cred = ADAL::ClientCredential.new(CLIENT_ID, CLIENT_SECRET)
    result = auth_ctx.acquire_token_for_client("https://management.azure.com/", client_cred)
    puts result.access_token
    
  2. Follow the adal sample with USERNAME & PASSWORD to get the access token via the code below.

    require 'adal'
    AUTHORITY = 'login.windows.net'
    user_cred = ADAL::UserCredential.new(username, password)
    ctx = ADAL::AuthenticationContext.new(AUTHORITY_HOST, TENANT)
    result = ctx.acquire_token_for_user("https://management.azure.com/", CLIENT_ID, user_cred)
    puts result.access_token
    

Hope it helps.