0

My custom Rest API get collection of newsletter subscriber is not working.

Below is my module- Custom_Restapi

Custom/Restapi/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Restapi>
            <version>0.1.0</version>
        </Custom_Restapi>
    </modules>
    <global>
        <models>
            <custom_restapi>
                <class>Custom_Restapi_Model</class>
            </custom_restapi>
        </models>
    </global>
</config>

Custom/Restapi/etc/api2.xml

<?xml version="1.0"?>
<config>
    <api2>
        <resource_groups>
            <custom_restapi translate="title" module="Custom_Restapi">
                <title>Custom Rest API</title>
                <sort_order>10</sort_order>
            </custom_restapi>
        </resource_groups>
        <resources>
            <custom_restapi translate="title" module="Custom_Restapi">
                <group>custom_restapi</group>
                <model>custom_restapi/api2_restapi</model>
                <title>Testing My Rest API</title>
                <sort_order>10</sort_order>
                <privileges>
                    <admin>
                        <retrieve>1</retrieve>
                    </admin>
                </privileges>
                <routes>
                    <route_collection>
                        <route>/custom</route>
                        <action_type>collection</action_type>
                    </route_collection>
                </routes>
                <versions>1</versions>
            </custom_restapi>
        </resources>
    </api2>
</config>

Custom/Restapi/Model/Api2/Restapi.php

<?php

class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
{

}

Custom/Restapi/Model/Api2/Restapi/Rest/Admin/V1.php

<?php
class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi
{


    protected function _retrieveCollection()
    {
        $collection = Mage::getModel('newsletter/subscriber')->getCollection();
        $data = $collection->load()->toArray();
        return isset($data['items']) ? $data['items'] : $data;
    }

}

Getting error on url "http://domain.com/api/rest/custom"

<magento_api>
<messages>
<error>
<data_item>
<code>403</code>
<message>Access denied</message>
</data_item>
</error>
</messages>
</magento_api>

I have checked my Rest Role and Rest Attributes for admin.

Any guess what I am doing wrong? Let me know if you have any queries.

Edited:--

I have made below changes and now It worked but partially. I add below code in api2.xml under <privileges> tag .

 <guest>
         <retrieve>1</retrieve>
</guest>

and also added new file for guest Custom/Restapi/Model/Api2/Restapi/Rest/Guest/V1.php

<?php
class Custom_Restapi_Model_Api2_Restapi_Rest_Guest_V1 extends Custom_Restapi_Model_Api2_Restapi
{
    protected function _retrieveCollection()
    {

        $collection = Mage::getModel('newsletter/subscriber')->getCollection()->load()->toArray();
        return $collection['items'];
    }

}

Now at front it shows xml like-

<magento_api>
<data_item/>
<data_item/>
</magento_api>

I dont now why it is not showing data into tag because I print $collection['items'] in above V1.php created above. it shows array like

Array
(
    [1] => Array
        (
            [subscriber_id] => 1
            [store_id] => 0
            [change_status_at] => 
            [customer_id] => 0
            [subscriber_email] => name@domain.com
            [subscriber_status] => 1
            [subscriber_confirm_code] => 25b702udq9eef7c8ahk078g0x1p19v5o
        )

    [2] => Array
        (
            [subscriber_id] => 2
            [store_id] => 0
            [change_status_at] => 
            [customer_id] => 0
            [subscriber_email] => name11@domain.com
            [subscriber_status] => 1
            [subscriber_confirm_code] => q1ntkr7tww082nwhk48rvtzvem2c66l7
        )

) 

No idea why this data is not showing in tag.

  • Did you use proper oauth tokens when you made the request? are you trying it on postman?? – NID Feb 22 '18 at 11:50
  • @NID kinldy check edits I am using Guest role now. – Shashank Kumrawat Feb 22 '18 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.