Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cToken_type
LOCAL nExpires_in
LOCAL cAccess_token
LOCAL cRefresh_token
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
// 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.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("client_id", "administration")
oJson:UpdateString("grant_type", "password")
oJson:UpdateString("scopes", "write")
oJson:UpdateString("username", "<user-username>")
oJson:UpdateString("password", "<user-password>")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://my-shopware-6-shop.de/api/oauth/token", oJson, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oJson:destroy()
oResp:destroy()
RETURN
ENDIF
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0
? "Response Body:"
? oJResp:Emit()
nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
? "Response Header:"
? oResp:Header
? "Failed."
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
RETURN
ENDIF
// 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
cToken_type := oJResp:StringOf("token_type")
nExpires_in := oJResp:IntOf("expires_in")
cAccess_token := oJResp:StringOf("access_token")
cRefresh_token := oJResp:StringOf("refresh_token")
// Save our JSON to a file (to be used in other examples..)
nSuccess := oJResp:WriteFile("qa_data/tokens/shopware6.json")
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()