Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

magento 2 Rest APi如何使用默认方法

Magento 2 Rest API有许多默认方法。我想使用那些方法和显示数据。

为此,我创建了一个新用户并分配了完全管理员权限。

尝试使用默认方法获取 SKU的产品信息: GET / V1 / products /:此处 给出的sku

如何访问默认方法并显示输出?我需要 curl 吗?

对于标准练习和MobileAPP输出,使用CURL到达那里是否合适?

是否有必要编写完整的令牌代码以获得响应?

使用令牌需要时间来回应。有没有更好的方法?

这种解决方案有两种方式 -

1)首先让我们谈谈遇到问题的方式

  • 您创建了管理员用户并提供了所有访问权限但仍无法获取产品详细信息。

所以,这是解决方案,它在我的Magento 2.2.4版中运行良好

我创建了一个管理员用户。

在此输入图像描述
  • 现在我需要为该管理员用户获取令牌,所以我使用 POSTMAN进行了操作

网址请求类型 POST

<host>/rest/V1/integration/admin/token?username=aditya&password=aditya@123

返回令牌。

"xcph3thmoaiyt0ylm58lf2dn150qlkfr" 这样的事情。


现在使用此令牌,我们将通过SKU为您的URL获取产品。

网址请求类型 GET

http://127.0.0.1/M224/index.php/rest/V1/products/240-LV09

有效载荷 - 标题

Authorization Bearer xcph3thmoaiyt0ylm58lf2dn150qlkfr

您需要在标头中传递令牌,即在Magento中检查用户访问权限的身份验证过程。

作为回应,您将获得产品数据


解决方案2

magento目录API的这条路径

vendor/magento/module-catalog/etc/webapi.xml

使用SKU获取产品。

<route url="/V1/products/:sku" method="GET">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get"/>
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>

在这里,定义了资源。它表示"Magento_Catalog::products"只有那些用户可以访问此API才能访问。

<resource ref="Magento_Catalog::products" />

因此,如果您想将此API用于其他用户(如客户),则可以将其更改resourceself

同样,它将需要相同的令牌,我们当然不会妥协安全性

<route url="/V1/products/:sku" method="GET">
            <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="get"/>
            <resources>
                <resource ref="self" />
            </resources>
        </route>

现在,这意味着我们可以访问它。

  • 使用管理员令牌
  • 使用管理员用户的令牌
  • 使用客户的令牌。

是的,您需要在模块中覆盖此文件

/vendor/magento/module-catalog/etc/webapi.xml

然后你也可以从客户的令牌访问它。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论