php - Magento REST API 示例代码 404s

标签 php .htaccess rest magento oauth

<分区>

按照此处的文档进行操作时 http://www.magentocommerce.com/api/rest/introduction.html

404s 以下的示例代码尝试调用 urls/oauth/initiate 和 /admin/oauth_authorize.

/api/rest 工作正常,因为我在 .htaccess 中有当前规则

RewriteRule ^api/rest api.php?type=rest [QSA,L]

我还缺少其他规则吗?据我了解,magento REST api 应该可以解决问题。或者问题可能与 url 重写无关?

我已经创建了适当的 REST 角色和属性,并将消费者 key / secret 也放在示例代码中,但没有骰子。

澄清一下,当使用 rest 客户端或浏览器访问 api/rest 时, guest 角色可以正常工作。但是,由于上述原因,尝试使用以下示例代码设置身份验证会导致我出现问题。

<?php
    /**
* Example of simple product POST using Admin account via Magento REST API. OAuth authorization is used
*/
$callbackUrl = "http://yourhost/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://magentohost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://magentohost/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://magentohost/oauth/token';
$apiUrl = 'http://magentohost/api/rest';
$consumerKey = 'yourconsumerkey';
$consumerSecret = 'yourconsumersecret';

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, $consumerSecret, OAUTH_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";
        $productData = json_encode(array(
            'type_id'           => 'simple',
            'attribute_set_id'  => 4,
            'sku'               => 'simple' . uniqid(),
            'weight'            => 1,
            'status'            => 1,
            'visibility'        => 4,
            'name'              => 'Simple Product',
            'description'       => 'Simple Description',
            'short_description' => 'Simple Short Description',
            'price'             => 99.95,
            'tax_class_id'      => 0,
        ));
        $headers = array('Content-Type' => 'application/json');
        $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
        print_r($oauthClient->getLastResponseInfo());
    }
} catch (OAuthException $e) {
    print_r($e);
} ?>

最佳答案

这部分 $callbackUrl = "http://yourhost/oauth_admin.php"; 有效吗?如果它不起作用,请修复此问题。请记住将值 http://yourhost/oauth_admin.php 替换为正确的值,然后先在浏览器中尝试。

确保 yourhostmagentohost 都是本地或远程服务器。例如,如果您的 magentohost 是远程服务器而 yourhost 是本地服务器,重定向将失败,您将收到 404 错误。

关于php - Magento REST API 示例代码 404s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731779/

相关文章:

javascript - 从文本框中检索值

php - 在 WooCommerce 中显示免费送货和送货信息之前还剩多少

.htaccess 对除子域之外的所有内容强制使用 "www."并删除尾部斜杠

.htaccess 重定向主域而不是子文件夹

rest - 如何处理 REST api 中的关系

php - 查看已经过去了多少天

php - 在从数据库获取设置的函数上我遇到了错误

http - 将 SSL *仅* 添加到特定文件夹

java - 返回 JSON 的 Spring Restful Web 服务

javascript - REST API 的日期格式