Magento Rest API为产品页面

时间:2015-06-25 09:36:01

标签: api rest magento

我已经安装/配置了Magento 1.9。按照https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-magento-on-ubuntu-14-04上的指南,在我当地的Ubuntu 14机器上进行CE。我将我的Magento基地设置为http://localhost

然后我尝试使用Scribe从Java访问API。这是我现在的代码:

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

当我运行此程序时,我能够成功获取令牌,然后我从Magento Admin面板验证。但是当我粘贴令牌时,我收到以下错误:

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>

现在,我在许多论坛上看到,在他们的Apache conf文件中添加一个块工作了。我的conf文件中有以下内容(/etc/apache2/sites-available/magento.conf):

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

但是,我不确定这是否是要添加的地方。所以我还在我的apache.conf文件(/etc/apache2/apache.conf)中添加了<Directory>块,并重新启动了apache2服务器。但是,这对我也没有帮助。我仍然得到404错误。我在这里做错了什么?

不确定这是否与此问题有关,但我在安装Magento后添加了Magento网站上提供的示例数据(即使他们已经特别提到不这样做),因为我所有的默认数据都是如此用户等已从数据库中删除。所以我不得不在数据库中手动添加管理员用户。这会改变别的吗?

========================编辑====================== =====

我编辑了我的.htaccess文件并添加了以下内容:

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

    RewriteBase /var/www/html/

现在,当我尝试运行我的代码时,它甚至在获取带有此错误的令牌之前就失败了:

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>

感谢。

1 个答案:

答案 0 :(得分:2)

确保使用选项-MultiViews