aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
  • Tools
  • About
  • DevOps
  • Platforms

How To Call Azure REST APIs

  • root
  • September 25, 2019
  • 1 minute read

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'."
  }
}
Read More  To Kubernetes Or Not Kubernetes: What Lies Beneath The Question?
root

Related Topics
  • API
  • Azure
  • Azure REST API
  • REST
You May Also Like
View Post
  • Computing
  • Platforms

Kubernetes K8s.gcr.io Redirect: What You Need To Know As An Anthos Or GKE User

  • March 30, 2023
View Post
  • DevOps
  • Engineering
  • People

2022 State Of DevOps Report Data Deep Dive: Good Team Culture

  • March 29, 2023
View Post
  • Data
  • Platforms

Google Data Cloud & AI Summit : In Less Than 12 Hours From Now

  • March 29, 2023
View Post
  • DevOps
  • Engineering

Verify POST Endpoint Availability With Uptime Checks

  • March 24, 2023
View Post
  • Platforms
  • Technology

Introducing GPT-4 In Azure OpenAI Service

  • March 21, 2023
View Post
  • Platforms
  • Solutions
  • Technology

Cloudflare Takes On Online Fraud Detection Market

  • March 15, 2023
View Post
  • People
  • Platforms
  • Technology

Women’s History Month: Celebrating The Success Of Women Founders: Schoolio

  • March 14, 2023
View Post
  • Platforms
  • Technology

Cloudflare Integrates With Atlassian, Microsoft, And Sumo Logic To Make Zero Trust Security Easy For Businesses

  • March 14, 2023

Stay Connected!
LATEST
  • 1
    Kubernetes K8s.gcr.io Redirect: What You Need To Know As An Anthos Or GKE User
    • March 30, 2023
  • 2
    Oracle Helidon Taps Virtual Threads For ‘Pure Performance’
    • March 29, 2023
  • 3
    2022 State Of DevOps Report Data Deep Dive: Good Team Culture
    • March 29, 2023
  • 4
    Google Data Cloud & AI Summit : In Less Than 12 Hours From Now
    • March 29, 2023
  • 5
    A 5-Minute Tour Of The Fediverse
    • March 28, 2023
  • 6
    Bringing Observability To Cloud Security
    • March 28, 2023
  • 7
    How AI Can Improve Digital Security
    • March 27, 2023
  • 8
    Docker’s Bad Week
    • March 27, 2023
  • 9
    My First Pull Request At Age 14
    • March 24, 2023
  • 10
    AWS Chatbot Now Integrated Into Microsoft Teams
    • March 24, 2023
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Introducing GPT-4 In Azure OpenAI Service
    • March 21, 2023
  • 2
    IBM And Fundación Ikerbasque Partner To Launch Groundbreaking Quantum Computational Center
    • March 24, 2023
  • 3
    Cleveland Clinic And IBM Unveil First Quantum Computer Dedicated To Healthcare Research
    • March 20, 2023
  • 4
    Verify POST Endpoint Availability With Uptime Checks
    • March 24, 2023
  • 5
    Oracle Cloud Infrastructure to Increase the Reliability, Efficiency, and Simplicity of Large-Scale Kubernetes Environments at Reduced Costs
    • March 20, 2023
  • /
  • Platforms
  • Architecture
  • Engineering
  • Programming
  • Tools
  • About

Input your search keywords and press Enter.