Shopware 6 provides a REST API, which uses a token that expires every 10 minutes.
When running manual API tests using PhpStorm this is annoying. How can this be done more efficiently?
PhpStorm can run multiple requests. We first define the client id and client secret in the Shopware 6 admin panel. Next we use this to obtain the short live token and then execute our request.
Create a new test.http
file in PhpStorm
Insert the following:
POST {{baseurl}}/api/oauth/token
Content-Type: application/json
{
"grant_type": "client_credentials",
"client_id": "{{client_id}}",
"client_secret": "{{client_secret}}"
}
> {%
client.global.set("token", response.body.access_token);
%}
###
POST {{baseurl}}/api/the-api-you-want-to-check
Authorization: Bearer {{token}}
Accept: application/json
Content-Type: application/json
Under the menu "run with" define a new environment
Add you integration under System -> Integration in the Shopware Admin panel
Add the baseurl
, the client_id
and the client_secret
to the environment:
{
"dev": {
"baseurl": "https://example.com.local/",
"client_id": "SWIADNHZQMVJMKFTRDLUCEOXTA",
"client_secret": "MXRFbUxlMlcxRExvQmFFWXZnZjRXU1JqVVFRUWlES29yTldaQ1k",
}
}
Execute the request
By adding the variables to the environment, you can also commit your test.http
file to a dev
folder or similar in your version control system to share it with other developers.