如何使用 PhpStorm REST API 客户端手动测试 Shopware 6 API

问题描述 投票:0回答:1

Shopware 6 提供 REST API,它使用每 10 分钟过期的令牌。

使用 PhpStorm 运行手动 API 测试时,这很烦人。如何才能更有效地做到这一点?

rest phpstorm shopware6 shopware6-api
1个回答
2
投票

PhpStorm 可以运行多个请求。我们首先在 Shopware 6 管理面板中定义客户端 ID 和客户端密钥。接下来我们用它来获取短期令牌,然后执行我们的请求。

  1. 在 PhpStorm 中创建一个新的

    test.http
    文件

  2. 插入以下内容:

    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
    
  3. 在菜单“run with”下定义一个新环境

  4. 在 Shopware 管理面板中的系统 -> 集成下添加集成

  5. baseurl
    client_id
    client_secret
    添加到环境中:

    {
      "dev": {
        "baseurl": "https://example.com.local/",
        "client_id": "SWIADNHZQMVJMKFTRDLUCEOXTA",
        "client_secret": "MXRFbUxlMlcxRExvQmFFWXZnZjRXU1JqVVFRUWlES29yTldaQ1k",
      }
    }
    
  6. 执行请求

通过将变量添加到环境中,您还可以将

test.http
文件提交到版本控制系统中的
dev
文件夹或类似文件夹中,以便与其他开发人员共享。

© www.soinside.com 2019 - 2024. All rights reserved.