Overview

This guide will show you how to make a request or call the Azure REST API.

 

Prerequisites

  • Access to the Azure portal
  • REST Client or command line to send a request

An example REST Client is Postman, for a guide on how to install Postman, see here.

 

Acquiring an OAuth2 Token

01. Acquire the following information from the Azure account before proceeding further.

  • Subscription ID
  • Tenant ID
  • Client ID
  • Client Secret
  • Resource Group ID (for the resource you will be accessing)

Refer to this guide on how to acquire the above credentials.

 

02. Make an API call to retrieve the OAuth2 access token.

URL https://login.microsoftonline.com/{{tenant_id}}/oauth2/token
HTTP Method POST
HTTP Headers Content-Type : application/x-www-form-urlencoded
Form Data grant_type : client_credentials

client_id : {{client_id}}

client_secret : {{client_secret}}

resource : https://management.azure.com

 

via Postman

 

03. It will return a similar response

{
  "token_type"     : "Bearer",
  "expires_in"     : "3600",
  "ext_expires_in" : "3600",
  "expires_on"     : "1567831530",
  "not_before"     : "1567827630",
  "resource"       : "https://management.azure.com",
  "access_token"   : "{{access_token}}"
}

 

Take note of the retrieved access token, this will be used to access the Azure resources.

 

Calling a REST API Endpoint

In the following example, we will be retrieving the list of Virtual Machines for a specific Resource Group.

01. Make an API call to retrieve the list of VM Instances.

The ouath2_token is the value retrieved from the https://login.microsoftonline.com/{{tenant_id}}/oauth2/token request.

URL https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resource_group_name}}/providers/Microsoft.Compute/virtualMachines?api-version=2018-06-01
HTTP Method GET
HTTP Headers Authorization : Bearer {{oauth2_token}}
Form Data — none —

 

via Postman

 

02. The following will be the sample response. Some details have been removed.

{
    "value": [
        {
            "name": "vm-geek-web-001",
            "id": "/subscriptions/.../resourceGroups/rg-geek/providers/Microsoft.Compute/virtualMachines/vm-geek-web-001",
            "type": "Microsoft.Compute/virtualMachines",
            "location": "southeastasia",
            "properties": {
                "vmId": "50711f51-1bcd-4bda-9cec-f1a28455f7fa",
                "hardwareProfile": {
                    "vmSize": "Standard_B1ls"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "Canonical",
                        "offer": "UbuntuServer",
                        "sku": "18.04-LTS",
                        "version": "latest"
                    },
                    "osDisk": {
                        "osType": "Linux",
                        "name": "vm-geek-web-001_OsDisk_1_2c20eb2db8ea4405975ad6e2eb2041f1",
                        "createOption": "FromImage",
                        "caching": "ReadWrite",
                        "managedDisk": {
                            "storageAccountType": "Standard_LRS",
                            "id": "/subscriptions/.../resourceGroups/rg-geek/providers/Microsoft.Compute/disks/vm-geek-web-001_OsDisk_1_2c20eb2db8ea4405975ad6e2eb2041f1"
                        },
                        "diskSizeGB": 30
                    },
                    "dataDisks": []
                },
                "osProfile": {
                    "computerName": "vm-geek-web-001",
                    "adminUsername": "devops",
                    "linuxConfiguration": {
                        "disablePasswordAuthentication": true,
                        "ssh": {
                            "publicKeys": [
                                {
                                    "path": "/home/devops/.ssh/authorized_keys",
                                    "keyData": "ssh-rsa ..."
                                }
                            ]
                        },
                        "provisionVMAgent": true
                    },
                    "secrets": [],
                    "allowExtensionOperations": true
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "/subscriptions/.../resourceGroups/rg-geek/providers/Microsoft.Network/networkInterfaces/vm-geek-web-001266"
                        }
                    ]
                },
                "diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true,
                        "storageUri": "https://rggeekdiag.blob.core.windows.net/"
                    }
                },
                "provisioningState": "Succeeded"
            }
        }
    ]
}

 

If the OAuth token is expired it will return the following.

{
  "error": 
  {
    "code": "ExpiredAuthenticationToken",
    "message": "The access token expiry UTC time '8/21/2019 9:13:16 AM' is earlier than current UTC time '9/7/2019 4:22:04 AM'."
  }
}
Previous How To: Postman, REST Client For Ubuntu - Installation
Next How To Install Flask