Error while using REST api in magento
Asked Answered
P

5

0

I have set up magento locally in my system using XAMPP

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

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.

<?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?

Pitterpatter answered 13/12, 2012 at 10:35 Comment(4)
Is Ouath enabled in the Xampp server? Its happens due to thatBourke
@chanz: i checked now. its not enabled.But i think there no way we can install oauth in xamppPitterpatter
magentocommerce.com/api/rest/authentication/… this link might help youBourke
Consumer key, Consumer secret, Access token, Access token secret, are requiredBourke
B
12

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

Similarly Go to 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....

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

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.enter image description here

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..

Authentication Endpoints

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

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

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

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

Let me know if you have any issues.

Best of luck

Bourke answered 13/12, 2012 at 11:21 Comment(10)
the output i get is this <magento_api/>, only this muchPitterpatter
Do you have product in your magento.. it should show the product listingBourke
I am sure there is no product in your magento.Bourke
sorry it was my mistake, i had 3 installation of magento in my system and this ones catalog was empty.Thanks for your valuable time.And adding to this how can we make this access protected using a username ans password?Pitterpatter
i am using xampp with windows.i am not able to install oauth module in windows just see my question <Fatal error: Class 'OAuth' not found in D:\Webserver\xampp\htdocs\dm.php on line 19> this is the error i am getting. is there any workaround for this?Pitterpatter
When I first tried to configured Rest for localhost i was not able to install oauth for Wamp server i tried pear.php.net/ but was not able to configured,so i configured that in lINUX SERVER and it went good.Bourke
@Bourke I have a products in magento and also i was already install oauth extension in zend for windows but when we open url like this '127.0.0.1/magento/api/rest/products', it display 'Access denied'.Frogfish
@Bourke you know how to get all category list using magento rest oauth api?Frogfish
by default in magneto Rest api there is no method for category listing.Bourke
@Bourke Can you help me regard rest api customer getting error : snag.gy/MVINwS.jpgBefall
A
2

A bit of code modifications will easily solve this error 403 forbidden.

What magento engine does is that it uses the default guest user to provide access to the REST api methods. The guest user does not have much powers so it should be better to change this functionality of magento. There are 2 ways of doing this:

1) Quick and dirty fix:
in the file /app/code/core/Mage/Api2/Model/Auth.php, change the value of:
DEFAULT_USER_TYPE = 'guest'
to
DEFAULT_USER_TYPE = 'admin'.

In the file
/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:
Override the two functionalities using the magento overriding mechanism to have a better solution in accordance to magento standards. This way the core files will be intact.

Asco answered 3/7, 2013 at 6:41 Comment(5)
I found out that mine is really a very dirty solution. It makes the resource available publicly without any authentication. People are advised to use this one sensibly. I will not consider this as a solution at all. This can be just used to test whether the resource is available or not. This should NOT go in production environment at all.Asco
Thank you Friend, for testing I have used 1st solution. can you help me for proper solution.Ashantiashbaugh
There are a number of tutorials available on how to override modules in magento like inchoo.net/ecommerce/magento/… and many more. You can go through them very easily to find the exact way.Asco
Not secure, there is an 'admin' user group for a reason, and that is to put certain functionality behind a wall. Opening that up to the world, bad idea!Belemnite
Yes Dan, this is what I stated as soon as I found that out. This is why I mentioned the term dirty.Asco
F
0

We use this link to install oauth for php. Its good and easy to add extension for php.

install oauth php

I hope it helps to all and would solved 'OAuth' not found fatal error.

Frogfish answered 8/2, 2013 at 11:28 Comment(4)
you were add php_oauth.dll file in C:\xampp\php\ext folder and change php.ini file by adding extension=php_oauth.dll in C:/xampp/php/php.ini file.?Frogfish
I have a zend with windows and oauth is completely working in my system. zend is best so use zend server.Frogfish
thats it, in my question i mentioned mine is XAMPP.I don't think it will work in XAMPP.but not quite sure.There will always be a way to do it which i don't know :)Pitterpatter
@BharatChodvadiya For me 127.0.0.1/anusthana/api/rest/products working, if ti try with 127.0.0.1/anusthana/api/rest/customers i am getting error like -> snag.gy/MVINwS.jpg, can you pls help meBefall
B
0

I had the same issue and was struggling for a week but just try insatlling new version of xammp or wamp with supports ouath.The better solution was ,I installed Ammps 1.9 and in php5.4 I resolved the extension of oauth but still make sure that you select the proper php for extension oauth is supported (php5.4)

Brake answered 18/3, 2013 at 15:17 Comment(0)
O
0

For installing 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

Orlosky answered 29/4, 2015 at 1:26 Comment(2)
For me 127.0.0.1/anusthana/api/rest/products working, if ti try with 127.0.0.1/anusthana/api/rest/customers i am getting error like -> snag.gy/MVINwS.jpg, can you pls help me @OrloskyBefall
There must be some authorization issue thats why you are getting access denied.Orlosky

© 2022 - 2024 — McMap. All rights reserved.