1

I am trying to collect azure metrics using REST api. I have a free subscription to azure account.

I am using the following wget to get the json message.

wget https://management.azure.com/subscriptions/XXXXXXX/resourceGroups/RG_SOUTH_INDIA/providers/Microsoft.Compute/virtualMachineScaleSets/linuxscal/metrics?api-version=2014-04-01

XXXXXXX- is my subscription id.

I get the following Error message.

Resolving management.azure.com... 13.67.231.219
Connecting to management.azure.com|13.67.231.219|:443... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Authorization failed.

What is wrong with my subscription/Authorization?!!

Thanks in Advance for your help guys!! Am Stuck!!

Nilotpal
  • 3,237
  • 4
  • 34
  • 56

2 Answers2

2

You need to include an Authorization header with a Bearer token in your call:

GET /subscriptions?api-version=2015-01-01 HTTP/1.1
Host: management.azure.com
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json

Take a look at armclient, since you really don't want to do all this by hand (or by curl): https://www.npmjs.com/package/armclient

// ES6
import ArmClient, { clientCredentials } from 'armclient';

const client = ArmClient({ 
  subscriptionId: '111111-2222-3333333',
  auth: clientCredentials({
    tenantId: '444444-555555-666666666',
    clientId: '777777-888888-999999999',
    clientSecret: 'aaaabbbbbccccc' // or servicePrincipalPassword
  })
});

Your /metrics call becomes:

client.get('/resourceGroups/RG_SOUTH_INDIA/providers/Microsoft.Compute/virtualMachineScaleSets/linuxscal/metrics', { 'api-version': '2014-04-01' })
  .then((res) => {
    console.log(res.body);
    console.log(res.headers);
  })
  .catch((err) => {
    console.log(err);
  });
evilSnobu
  • 24,582
  • 8
  • 41
  • 71
  • Thanks @evilSnobu!! Thats true, i anyways wont using wget for it. I shall be using Java. When i am calling the REST API using Java, i am still getting the same error ie.Authentication error. It would be helpful if i get a java code for it!...Thanks again!! – Nilotpal Nov 14 '16 at 04:57
0

By specifying the option --user and --ask-password wget will ask for the credentials. Below is an example. Change the username and link to your needs

wget --user=username --ask-password https://YOUR_URL
Umais Gillani
  • 608
  • 4
  • 9
  • 1
    That's only good for HTTP Basic authentication - won't work with Azure ARM which expects a token: `Authorization: Bearer ToKeN=` – evilSnobu Nov 13 '16 at 12:05