drunkenfist
drunkenfist

Reputation: 3038

Magento Rest API retuning 404 for products page

I've installed / configured Magento 1.9. CE on my local Ubuntu 14 machine by following the guide at https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-magento-on-ubuntu-14-04. I've setup my Magento base as http://localhost.

I then tried accessing the APIs from Java using Scribe. This is the code I have right now:

public final class MagentoAuth {

    /**
     * @param args
     */
    public static void main(String[] args) {
        final String MAGENTO_API_KEY = "abc";
        final String MAGENTO_API_SECRET = "xyz";
        final String MAGENTO_REST_API_URL = "http://localhost/api/rest";

        // three-legged oauth
        OAuthService service = new ServiceBuilder()
        .provider(MagentoThreeLeggedOAuth.class)
        .apiKey(MAGENTO_API_KEY)
        .apiSecret(MAGENTO_API_SECRET)
        .debug()
        .build();

        System.out.println("" + service.getVersion());
        Scanner in = new Scanner(System.in);
        System.out.println("Magento's OAuth Workflow");
        System.out.println();

        // Obtain the Request Token
        System.out.println("Fetching the Request Token...");
        Token requestToken = service.getRequestToken();
        System.out.println("Got the Request Token!");
        System.out.println();

        System.out.println("Fetching the Authorization URL...");
        String authorizationUrl = service.getAuthorizationUrl(requestToken);
        System.out.println("Got the Authorization URL!");
        System.out.println("Now go and authorize Main here:");
        System.out.println(authorizationUrl);
        System.out.println("And paste the authorization code here");
        System.out.print(">>");

        Verifier verifier = new Verifier(in.nextLine());
        System.out.println();
        System.out.println("Trading the Request Token for an Access Token...");

        Token accessToken = service.getAccessToken(requestToken, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: "
                + accessToken + " )");
        System.out.println();

        OAuthRequest request = new OAuthRequest(Verb.GET, MAGENTO_REST_API_URL+ "/products?limit=2");
        service.signRequest(accessToken, request);

        Response response = request.send();
        System.out.println();
        System.out.println(response.getCode());
        System.out.println(response.getBody());
        System.out.println();
    }
}

When I run this program, I'm able to successfully get the token, which I then verify from the Magento Admin panel. But then when I paste the token, I get the following error:

404
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /api/rest/products was not found on this server.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address>
</body></html>

Now, I saw on many forums that adding a block to their Apache conf file worked. I have the following in my conf file (/etc/apache2/sites-available/magento.conf):

<VirtualHost *:80>
    DocumentRoot /var/www/html
    <Directory /var/www/html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    </Directory>
</VirtualHost>

However, I was not sure if that is the place to add. So I also added the <Directory> block in my apache.conf file (/etc/apache2/apache.conf) as well and restarted apache2 server. However, this doesn't help me either. I still get the 404 error. What am I doing wrong here?

Not sure if this has anything to do with the problem ,but I added the sample data provided on Magento's website after installing Magento (even though they have specifically mentioned not to do so) because of which all my default data such as admin user etc were deleted from the database. So I had to add the admin user manually myself in the database. Would this have changed something else?

======================== EDIT ===========================

I edited my .htaccess file and added the following:

## you can put here your magento root folder
## path relative to web root

    RewriteBase /var/www/html/

Now, when I try to run my code, it fails even before getting the token with this error:

response status code: 403
response body: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /var/www/html/index.php
on this server.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address>
</body></html>

Thanks.

Upvotes: 2

Views: 3133

Answers (1)

Alex
Alex

Reputation: 34978

Make sure to use Options -MultiViews

Upvotes: 2

Related Questions