2

I'm new to the REST api concept, after hitting the url- http://magentohost/api/rest/products I can see the products, means can access the product resource through guest user.

Then I have created the consumer from the admin panel and get the consumer key and secret then added new admin role and from REST attributes gave all resouce access to admin and linked admin user to REST role. Then I put sample php example for retrieving the products from documentation outside the magento root directory filled the url paths and consumer key and secret and created new file which is call back url.

Now when i run this sample file it works fine, ask user to fill his credentials, authorize the application and sent back to callback url.

There is nothing in the call back page, i have kept it blank, so what should i do to retrieve the list of products ? Is there anything that I'm missing ? How should I move further to learn REST API's ? Thanks in advance

2
  • goo.gl/nukPy0 This Chrome Extension comes in handy when working with REST API's.
    – B00MER
    Commented Apr 18, 2016 at 12:07
  • @B00MER thanks, but i would like to know how to do this with php file, and want to know what should be there in callbackurl to print the products
    – Shrikant
    Commented Apr 18, 2016 at 13:06

1 Answer 1

0
$url = 'magentohost url';
$callbackUrl = $url . "oauth_admin.php";
$temporaryCredentialsRequestUrl = $url . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = $url . 'admin/oauth_authorize';
$accessTokenRequestUrl = $url . 'oauth/token';
$apiUrl = $url . 'api/rest';
$consumerKey = 'consumer_key';
$consumerSecret = 'consumer_secret';
$token = 'token';
$secret = 'token_secret';

try {
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
    $oauthClient->setToken($token, $secret);
    $resourceUrl = "$apiUrl/products";
    $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
    $productsList = json_decode($oauthClient->getLastResponse());
    echo '<pre>';
    print_r($productsList);
}
catch(Exception $e) {
    echo '<pre>';
    print_r($e);
}
2
  • this didn't worked for me because i haven't generated token and token secrete. when redirected to the call back url my url looks like magentohost/oauth_admin.php?oauth_token=6862994ddb288e4400ef786cb50fd315&oauth_verifier=2abfe91360cb04ede3b2904617100a36
    – Shrikant
    Commented Apr 19, 2016 at 8:34
  • 1
    still was useful :)
    – Shrikant
    Commented Apr 19, 2016 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.