Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/token"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("grant_type", "password")
req:add_param("username", "your_username")
req:add_param("password", "your_password")

local tokenEndpoint = "https://your_api.azurewebsites.net/token"

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

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

-- Sample JSON response:

-- {
--   "access_token": "NQGHn ... xTS",
--   "token_type": "bearer",
--   "expires_in": 1209599,
--   "userName": "your_username",
--   ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
--   ".expires": "Mon, 11 May 2020 23:49:35 GMT"
-- }

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- ----------------------------------
-- Use the OAuth2 token in a request.
-- For example...

local sbXml = chilkat.StringBuilder.new()
success = sbXml:load_file("c:/someDir/someXmlFile.xml", "utf-8")
if not success then
    print("Failed to load the XML file.")
    return
end

-- Get the OAuth2 token and use it for authentication
http.auth_token = jResp:string_of("token")

local destUrl = "https://your_api.azurewebsites.net/destinationUrl"
success = http:http_sb("POST", destUrl, sbXml, "utf-8", "application/xml", resp)
if not success then
    print(http.last_error_text)
    return
end

respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- Examine the response body
print(resp.body_str)