Sample code for 30+ languages & platforms
Lua

Microsoft Graph -- List Users

See more Microsoft Graph Examples

Retrieve a list of Microsoft Graph user objects.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- ------------------------------------------------------------------------------------------------------
-- See an easier way to do OAuth2 client credentials:
-- Example using Automatic OAuth2 Client Credentials
-- ------------------------------------------------------------------------------------------------------

-- Get an access token with the required scope using client credentials...
-- See How to Create Microsoft Graph App (in Azure Portal) for Client Credentials Authentication
local http = chilkat.Http.new()
local req = chilkat.HttpRequest.new()
req:add_param("client_secret", "CLIENT_SECRET")
req:add_param("client_id", "CLIENT_ID")
req:add_param("scope", "https://graph.microsoft.com/.default")
req:add_param("grant_type", "client_credentials")

-- Use your own tenant ID, for example 4d8fdd66-66d1-43b0-ae5c-e31b4b7de5cd
local url = "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token"

req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"

local resp = chilkat.HttpResponse.new()
local success = http:http_req(url, req, resp)
if not success then
    print(http.last_error_text)
    return
end

local statusCode = resp.status_code
print("Response status code = " .. statusCode)

local json = chilkat.JsonObject.new()
json:load(resp.body_str)

-- -----------------------------------------------------
-- Use the access token obtained from above.
-- Note: We don't need to re-fetch a new access token every time.  An access token is valid
-- for some amount of time, typically an hour (3600 seconds)

-- Use your previously obtained access token here:
http.auth_token = json:string_of("access_token")

print("access token: " .. http.auth_token)

local sbResponse = chilkat.StringBuilder.new()

success = http:quick_get_sb("https://graph.microsoft.com/v1.0/users", sbResponse)
if not success then
    print(http.last_error_text)
    return
end

json:load_sb(sbResponse)
json.emit_compact = false

print("Status code = " .. http.last_status)
if http.last_status ~= 200 then
    print(json:emit())
    print("Failed.")
end

print(json:emit())

-- Sample output
-- {
--   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
--   "value": [
--     {
--       "@odata.id": "https://graph.microsoft.com/v2/6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/directoryObjects/fca490d8-5918-4201-8079-c5dcbeafcdc9/Microsoft.DirectoryServices.User",
--       "businessPhones": [
--       ],
--       "displayName": "Joe Sample",
--       "givenName": "Joe",
--       "jobTitle": null,
--       "mail": null,
--       "mobilePhone": null,
--       "officeLocation": null,
--       "preferredLanguage": null,
--       "surname": "Sample",
--       "userPrincipalName": "admin_chilkatsoft.com#EXT#@adminchilkatsoft.onmicrosoft.com",
--       "id": "fca490d8-5918-4201-8079-c5dcbeafcdc9"
--     }
--   ]
-- }

-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

local odata_id = ""
local displayName = ""
local givenName = ""
local jobTitle = ""
local mail = ""
local mobilePhone = ""
local officeLocation = ""
local preferredLanguage = ""
local surname = ""
local userPrincipalName = ""
local id = ""
local j = 0
local count_j = 0

local odata_context = json:string_of("\"@odata.context\"")
local i = 0
local count_i = json:size_of_array("value")
while i < count_i do
    json.i = i
    odata_id = json:string_of("value[i].\"@odata.id\"")
    displayName = json:string_of("value[i].displayName")
    givenName = json:string_of("value[i].givenName")
    jobTitle = json:string_of("value[i].jobTitle")
    mail = json:string_of("value[i].mail")
    mobilePhone = json:string_of("value[i].mobilePhone")
    officeLocation = json:string_of("value[i].officeLocation")
    preferredLanguage = json:string_of("value[i].preferredLanguage")
    surname = json:string_of("value[i].surname")
    userPrincipalName = json:string_of("value[i].userPrincipalName")
    id = json:string_of("value[i].id")
    j = 0
    count_j = json:size_of_array("value[i].businessPhones")
    while j < count_j do
        json.j = j
        j = j + 1
    end

    i = i + 1
end