简体   繁体   English

在magento中使用REST API时出错

[英]Error while using REST api in magento

I have set up magento locally in my system using XAMPP 我已经使用XAMPP在系统中本地设置了magento

I have created a file in root directory named dm.php with the contents 我已经在根目录中创建了一个名为dm.php的文件,内容如下

<?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);
}

But this is giving me the following error 但这给了我以下错误

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

Can anybody shed some light on this Thanks 任何人都可以对此有所了解吗?

Since oauth is not possible to install in xampp windows i changed the contents of my dm.php file to this. 由于无法在xampp Windows中安装oauth,因此我将dm.php文件的内容更改为此。

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

Now i am getting an error like this 现在我收到这样的错误

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

What am i doing wrong? 我究竟做错了什么?

First of all Go to magento admin panel System->Webservice->RESt Roles->Guest->Resources Access ->SET ALL 首先,转到magento管理面板, System->Webservice->RESt Roles->Guest->Resources Access ->SET ALL

Similarly Go to System->Webservice->RESt Attribute->Guest->Resources Access ->SET ALL 同样,转到System->Webservice->RESt Attribute->Guest->Resources Access ->SET ALL

Then Hit this url http://****/chanchal/magento/api/rest/products in web Browser and check what error it shows.... 然后在Web浏览器中点击此URL http://****/chanchal/magento/api/rest/products ,并检查显示的错误。

According to me it must show product in your website in xml format. 据我说,它必须以xml格式在您的网站中显示产品。

Please let me know.. 请告诉我..

EDIT: I configured a localhost just now and got this output refer the Screenshot. 编辑:我刚才配置了一个本地主机,并得到此输出,请参阅屏幕快照。 Be sure there is product in your magento. 确保您的magento中有产品。 在此处输入图片说明

Similarly follow the above steps for admin,customer then create a Ouath consumer from admin panel , Install RESTClient For Mozilla Firefox And follow Here 同样地,按照上述步骤进行管理,客户然后从管理面板创建Ouath使用者,为Mozilla Firefox安装RESTClient并按照此处

These for steps are necessary for the setup..the link might help you.. 这些设置步骤是必需的。此链接可能会为您提供帮助。

Authentication Endpoints 认证端点

1. /oauth/initiate - this endpoint is used for retrieving the Request Token. 1. /oauth/initiate此端点用于检索请求令牌。

2. /oauth/authorize - this endpoint is used for user authorization (Customer). 2. /oauth/authorize此端点用于用户授权(客户)。

3. /admin/oauth_authorize - this endpoint is used for user authorization (Admin). 3. /admin/oauth_authorize此端点用于用户授权(Admin)。

4. /oauth/token - this endpoint is used for retrieving the Access Token. 4. /oauth/token此端点用于检索访问令牌。

Let me know if you have any issues. 如果您有任何问题,请告诉我。

Best of luck 祝你好运

A bit of code modifications will easily solve this error 403 forbidden . 稍加修改代码即可轻松解决403 forbidden错误。

What magento engine does is that it uses the default guest user to provide access to the REST api methods. magento引擎的作用是使用默认的guest用户提供对REST api方法的访问。 The guest user does not have much powers so it should be better to change this functionality of magento. 来宾用户没有太多功能,因此最好更改magento的此功能。 There are 2 ways of doing this: 有两种方法可以做到这一点:

1) Quick and dirty fix: 1)快速而肮脏的修复:
in the file /app/code/core/Mage/Api2/Model/Auth.php , change the value of: 在文件/app/code/core/Mage/Api2/Model/Auth.php中 ,更改以下值:
DEFAULT_USER_TYPE = 'guest'
to
DEFAULT_USER_TYPE = 'admin' . DEFAULT_USER_TYPE = 'admin'

In the file 在文件中
/app/code/core/Mage/Api2/Model/Auth/Adapter.php , /app/code/core/Mage/Api2/Model/Auth/Adapter.php
change this line from 更改此行从
return (object) array('type' => Mage_Api2_Model_Auth::DEFAULT_USER_TYPE, 'id' => null);
to this: 对此:
return (object) array('type' => Mage_Api2_Model_Auth::DEFAULT_USER_TYPE, 'id' => '1');

This way the authentication system will not be broken. 这样,身份验证系统就不会被破坏。

2) Proper and long run fix: 2)正确且长期的修复:
Override the two functionalities using the magento overriding mechanism to have a better solution in accordance to magento standards. 使用magento覆盖机制覆盖这两个功能,以根据magento标准提供更好的解决方案。 This way the core files will be intact. 这样,核心文件将保持完整。

We use this link to install oauth for php. 我们使用此链接为php安装oauth。 Its good and easy to add extension for php. 它的好和容易添加扩展名的PHP。

install oauth php 安装oauth php

I hope it helps to all and would solved 'OAuth' not found fatal error. 希望对所有人有帮助,并解决“ OAuth”未发现致命错误。

我遇到了同样的问题,苦苦挣扎了一周,但是尝试用支持ouath安装新版本的xammp或wamp。更好的解决方案是,我安装了Ammps 1.9,并在php5.4中解决了oauth的扩展问题,但仍要确保您选择支持扩展名oauth的适当php(php5.4)

For installing Oauth : http://www.magentocommerce.com/api/rest/authentication/oauth_authentication.html 对于安装Oauth: http : //www.magentocommerce.com/api/rest/authentication/oauth_authentication.html

Installing PHP Extension for Oauth : 1. Download php_oauth.dll file and add it under C:\\xampp\\php\\ext\\ 2. add [PHP_OAUTH] extension=php_oauth.dll in php.ini 安装用于Oauth的PHP扩展:1.下载php_oauth.dll文件,并将其添加到C:\\ xampp \\ php \\ ext \\下。2.在php.ini中添加[PHP_OAUTH] extension = php_oauth.dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM