Lua
Lua
Shopware 6 - Get OAuth2 Access Token using Username and Password
See more Shopware 6 Examples
Demonstrates how to get an OAuth2 access token using your username and password.Chilkat Lua Downloads
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
-- {
-- "client_id": "administration",
-- "grant_type": "password",
-- "scopes": "write",
-- "username": "<user-username>",
-- "password": "<user-password>"
-- }
-- 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("client_id", "administration")
json:update_string("grant_type", "password")
json:update_string("scopes", "write")
json:update_string("username", "<user-username>")
json:update_string("password", "<user-password>")
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")