Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

First Time REST API use
 
samhuckaby
Jr. Member
 
Total Posts:  8
Joined:  2012-08-13
 

Hello Everyone,

I had previously been using SOAP to do my API calls, but was recently referred to use REST/oAuth instead. I really like the way that REST is set up, but I’m having a lot of difficulty getting return results. I’m hoping to get this figured out and then post a how-to article to help people like me, and prevent people from spending several hours trying to get it working.

To set up, In the backend, I created an admin REST Role named “programmer” with allapi resource access.
Next, I set my REST Attributes for admin to give it all resource access.
Finally I created a REST Consumer named “programmer”.

I am using the code from the magento wiki, to give a list of products using an admin account.

<?php

$callbackUrl 
"http://www.#######.net/oauth_admin.php";
$temporaryCredentialsRequestUrl =
"http://www.########.net/oauth/initiate?oauth_callback=" .
urlencode($callbackUrl);
$adminAuthorizationUrl 'http://www.#######.net/admin/oAuth_authorize';
$accessTokenRequestUrl 'http://www.#######.net/oauth/token';
$apiUrl 'http://www.########.net/api/rest';
$consumerKey '##########################';
$consumerSecret '################################';
 
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$consumerSecretOAUTH_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);
}
?>

-------------------------------------------------------------------------------------------------------------------

So at this point is where I have reached my problem. First, the code returns two errors:

PHP Notice:  Undefined index: state in /home/sam/public_html/mage_REST.php on line 18
PHP Notice:  Undefined index: state in /home/sam/public_html/mage_REST.php on line 22

So, being the curious man that I am, I added a line to give $_SESSION[’state’] a value, and then it just returns nothing…

Anyways, if you see anything in here that may be tripping me up, please reply. thanks in advance for any help.

Sam

 
Magento Community Magento Community
Magento Community
Magento Community
 
pvsmadhukar
Jr. Member
 
Total Posts:  3
Joined:  2010-12-11
 

Did you get any resolution for this i am also facing similar kind of issue. shut eye
In my case its working on my local machine and failed when i try on Linux machine production.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pvsmadhukar
Jr. Member
 
Total Posts:  3
Joined:  2010-12-11
 

For those who is facing similar issue.

If you are getting the undefined index issue in your apache log file means your mod_rewrite is not working properly.
Check if its installed and enabled.

if enabled but you are still not able solve , then check if your magento root folder is properly configured in apache httpd.conf file
something like
http://magentohost/<<magento>>
<<magento>> folder should be given full permissions from apache like below

<Directory “/var/www/<magento_folder_here>">
AllowOverride all
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top