在 magento 中使用 REST api 时出错 [英] Error while using REST api in magento

查看:36
本文介绍了在 magento 中使用 REST api 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 XAMPP 在我的系统中本地设置了 magento

I have set up magento locally in my system using XAMPP

我在根目录下创建了一个名为 dm.php 的文件,内容是

I have created a file in root directory named dm.php with the contents

<?php
/**
* Example of products list retrieve using Customer account via Magento REST API. OAuth authorization is used
*/
$callbackUrl = "http://localhost/dm.php";
$temporaryCredentialsRequestUrl = "http://localhost/mage2/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://localhost/mage2/oauth/authorize';
$accessTokenRequestUrl = 'http://localhost/mage2/oauth/token';
$apiUrl = 'http://localhost/mage2/api/rest';
$consumerKey = 'enhksf7u33p3snubewb6zcq0z9c63bvv';
$consumerSecret = 'p7e835cdcxofokeep749jgzz4l1e306p';

session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
    $_SESSION['state'] = 0;
}
try {
    $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
    $oauthClient->enableDebug();

    if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
        $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
        exit;
    } else if ($_SESSION['state'] == 1) {
        $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
        $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
        $_SESSION['state'] = 2;
        $_SESSION['token'] = $accessToken['oauth_token'];
        $_SESSION['secret'] = $accessToken['oauth_token_secret'];
        header('Location: ' . $callbackUrl);
        exit;
    } else {
        $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
        $resourceUrl = "$apiUrl/products";
        $oauthClient->fetch($resourceUrl);
        $productsList = json_decode($oauthClient->getLastResponse());
        print_r($productsList);
    }
} catch (OAuthException $e) {
    print_r($e);
}

但这给了我以下错误

Fatal error: Class 'OAuth' not found in D:\Webserver\xampp\htdocs\dm.php on line 19

任何人都可以对此有所了解吗谢谢

Can anybody shed some light on this Thanks

由于无法在 xampp 窗口中安装 oauth,我将 dm.php 文件的内容更改为此.

Since oauth is not possible to install in xampp windows i changed the contents of my dm.php file to this.

<?php
$ch = curl_init('http://localhost/mage2/api/rest/customers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$customers = curl_exec($ch);
echo $customers;
?>

现在我收到这样的错误

{"messages":{"error":[{"code":403,"message":"Access denied"}]}}

我做错了什么?

推荐答案

首先转到 magento 管理面板 System->Webservice->REST Roles->Guest->Resources Access ->SET ALL

First of all Go to magento admin panel System->Webservice->RESt Roles->Guest->Resources Access ->SET ALL

同样去System->Webservice->REST Attribute->Guest->Resources Access->SET ALL

然后在网络浏览器中点击这个 url http://****/chanchal/magento/api/rest/products 并检查它显示的错误......

Then Hit this url http://****/chanchal/magento/api/rest/products in web Browser and check what error it shows....

据我所知,它必须以 xml 格式在您的网站上显示产品.

According to me it must show product in your website in xml format.

请告诉我..

我刚刚配置了一个本地主机并得到这个输出参考屏幕截图.确保您的 magento 中有产品.

I configured a localhost just now and got this output refer the Screenshot. Be sure there is product in your magento.

同样按照上面的管理员步骤,客户然后从管理面板创建一个 Ouath 消费者,为 Mozilla Firefox 安装 RESTClient 并按照 这里

Similarly follow the above steps for admin,customer then create a Ouath consumer from admin panel , Install RESTClient For Mozilla Firefox And follow Here

这些步骤是设置所必需的..该链接可能对您有帮助..

These for steps are necessary for the setup..the link might help you..

身份验证端点

1./oauth/initiate - 此端点用于检索请求令牌.

1./oauth/initiate - this endpoint is used for retrieving the Request Token.

2./oauth/authorize - 此端点用于用户授权(客户).

2./oauth/authorize - this endpoint is used for user authorization (Customer).

3./admin/oauth_authorize - 此端点用于用户授权(管理员).

3./admin/oauth_authorize - this endpoint is used for user authorization (Admin).

4./oauth/token - 此端点用于检索访问令牌.

4./oauth/token - this endpoint is used for retrieving the Access Token.

如果您有任何问题,请告诉我.

Let me know if you have any issues.

祝你好运

这篇关于在 magento 中使用 REST api 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆