Shopware 6 提供 REST API,它使用每 10 分钟过期的令牌。
使用 PhpStorm 运行手动 API 测试时,这很烦人。如何才能更有效地做到这一点?
PhpStorm 可以运行多个请求。我们首先在 Shopware 6 管理面板中定义客户端 ID 和客户端密钥。接下来我们用它来获取短期令牌,然后执行我们的请求。
在 PhpStorm 中创建一个新的
test.http
文件
插入以下内容:
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
在菜单“run with”下定义一个新环境
在 Shopware 管理面板中的系统 -> 集成下添加集成
将
baseurl
、client_id
和 client_secret
添加到环境中:
{
"dev": {
"baseurl": "https://example.com.local/",
"client_id": "SWIADNHZQMVJMKFTRDLUCEOXTA",
"client_secret": "MXRFbUxlMlcxRExvQmFFWXZnZjRXU1JqVVFRUWlES29yTldaQ1k",
}
}
执行请求
通过将变量添加到环境中,您还可以将
test.http
文件提交到版本控制系统中的 dev
文件夹或类似文件夹中,以便与其他开发人员共享。