DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vJson
Handle hoJson
Variant vResp
Handle hoResp
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoJResp
Integer iRespStatusCode
String sToken_type
Integer iExpires_in
String sAccess_token
String sRefresh_token
String sTemp1
Move False To iSuccess
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "client_id" "administration" To iSuccess
Get ComUpdateString Of hoJson "grant_type" "password" To iSuccess
Get ComUpdateString Of hoJson "scopes" "write" To iSuccess
Get ComUpdateString Of hoJson "username" "<user-username>" To iSuccess
Get ComUpdateString Of hoJson "password" "<user-password>" To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" "https://my-shopware-6-shop.de/api/oauth/token" vJson "application/json" vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
If (Not(IsComObjectCreated(hoJResp))) Begin
Send CreateComObject of hoJResp
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
Set ComEmitCompact Of hoJResp To False
Showln "Response Body:"
Get ComEmit Of hoJResp To sTemp1
Showln sTemp1
Get ComStatusCode Of hoResp To iRespStatusCode
Showln "Response Status Code = " iRespStatusCode
If (iRespStatusCode >= 400) Begin
Showln "Response Header:"
Get ComHeader Of hoResp To sTemp1
Showln sTemp1
Showln "Failed."
Procedure_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
Get ComStringOf Of hoJResp "token_type" To sToken_type
Get ComIntOf Of hoJResp "expires_in" To iExpires_in
Get ComStringOf Of hoJResp "access_token" To sAccess_token
Get ComStringOf Of hoJResp "refresh_token" To sRefresh_token
// Save our JSON to a file (to be used in other examples..)
Get ComWriteFile Of hoJResp "qa_data/tokens/shopware6.json" To iSuccess
End_Procedure