【问题标题】:How can I get data through the Magento REST API如何通过 Magento REST API 获取数据
【发布时间】:2015-05-16 21:31:00
【问题描述】:

如何获取必须通过 Megento REST API 授权的产品信息和客户信息等数据?我按照指南一步步操作,但仍然不知道如何操作。 首先,我根据指南在管理面板创建了一个新的消费者。

  1. 在 Magento 管理面板菜单上,选择系统 > Web 服务 > REST - OAuth 消费者。
  2. 在 OAuth Consumers 页面,点击右上角的 Add New 添加新的消费者。
  3. 新消费者页面打开。 Key 和 Secret 字段是自动填写的,无法编辑。这些值是自动生成的,将用于在 Magento 中识别消费者。

然后,我像这样在我的主机根目录构建了一个 oauth_admin.php。

<?php
$callbackUrl = "http://demo1.sunpop.cn/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://demo1.sunpop.cn/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://demo1.sunpop.cn/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://demo1.sunpop.cn/oauth/token';
$apiUrl = 'http://demo1.sunpop.cn/api/rest';
$consumerKey = 'e94b6b9c113d4903aaab4f08c8150754';
$consumerSecret = 'fcf5bdf58bc36c434e52f1ac916ba5ae';

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']);
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token'].'&oauth_token_secret='.$requestToken['oauth_token_secret']);
        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, array(), 'GET', array('Content-Type' => 'application/json'));
        $productsList = json_decode($oauthClient->getLastResponse());
        print_r($productsList);
    }
} catch (OAuthException $e) {
    print_r($e->getMessage());
    echo "<br/>";
    print_r($e->lastResponse);
}
?>

之后,我尝试访问 myhost/oauth_admin.php。此地址响应 404,但我可以像这样在重定向链接中获取 oauth_token 和 oauth_token_secret。

http://demo1.sunpop.cn/admin/oAuth_authorize?oauth_token=df226dce0223c2706f4a03a10583fb88&oauth_token_secret=46f96db257f7f0dcb1d768370f3d9fcd

然后,我复制了 oauth_token 和 oauth_token_secret 并尝试使用 REST-Client 向 myhost/api/rest/stockitems 发送请求,但返回了 401。

<?xml version="1.0"?>
<magento_api>
  <messages>
    <error>
      <data_item>
        <code>401</code>
        <message>oauth_problem=consumer_key_rejected</message>
      </data_item>
    </error>
  </messages>
</magento_api>

我的环境可以在http://demo1.sunpop.cn/phpinfo.php找到 我是magento的初学者。我想知道我在此过程中犯了什么错误。如何从 RestAPI 获取数据?如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 你好,你得到答案了吗?

标签: api rest magento oauth


【解决方案1】:

请将oAuth_authorize 更改为小号oauth_authorize

【讨论】:

    猜你喜欢
    • 2014-07-30
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多