Sample code for 30+ languages & platforms
Lua

Shopware 6 - Get OAuth2 Access Token using Client Credentials (Integration)

See more Shopware 6 Examples

Demonstrates how to get an OAuth2 access token with an integration.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

-- Sends the following POST

--     POST /api/oauth/token
--     {
--         "grant_type": "client_credentials",
--         "client_id": "<client-id>",
--         "client_secret": "<client-secret>"
--     }

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

-- The following JSON is sent in the request body.

local json = chilkat.JsonObject.new()
json:update_string("grant_type", "client_credentials")
json:update_string("client_id", "client-id")
json:update_string("client_secret", "client-secret")

local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://my-shopware-6-shop.de/api/oauth/token", json, "application/json", 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())

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

-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)

-- {
--   "token_type": "Bearer",
--   "expires_in": 600,
--   "access_token": "xxxxxxxxxxxxxx",
--   "refresh_token": "token"
-- }

-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

local token_type = jResp:string_of("token_type")
local expires_in = jResp:int_of("expires_in")
local access_token = jResp:string_of("access_token")
local refresh_token = jResp:string_of("refresh_token")

-- Save our JSON to a file (to be used in other examples..)
success = jResp:write_file("qa_data/tokens/shopware6.json")