【问题标题】:Magento 1.9 REST API Invalid auth/bad request (got a 500, expected HTTP/1.1 20X or a redirect)Magento 1.9 REST API Invalid auth/bad request(得到 500,预期 HTTP/1.1 20X 或重定向)
【发布时间】:2017-04-10 23:24:40
【问题描述】:

我在 Magento 1.9.3 中请求 REST API 时收到此错误

   OAuthException Object
(
    [message:protected] => Invalid auth/bad request (got a 500, expected HTTP/1.1 20X or a redirect)
    [string:Exception:private] => 
    [code:protected] => 500

)

【问题讨论】:

  • 请修改您的代码。所以,我可以给你更具体的帮助
  • 问题解决了吗?

标签: rest magento


【解决方案1】:

准备 REST API

使用 REST API 资源需要这些步骤:

  1. 从 Magento Admin 设置 REST 资源操作的权限 面板。
  2. 在 Magento Admin 中配置不同用户类型的属性 面板。
  3. 有 3 种不同类型的用户访问数据:管理员、 顾客,客人。 Admin 是后台登录用户 Customer 是前台登录用户,Guest 是未登录前台 用户。

为第三方应用程序准备 REST API

  1. 在 Magento Admin 中注册第三方应用程序(消费者) 面板。

  2. 第三方应用程序将利用提供的消费者 调用 Magento 商店以获取访问令牌的凭据 访问数据。

PHP 示例

使用 OAuth 身份验证以管理员用户身份创建简单产品

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

【讨论】:

  • $oauthClient->fetch($resourceUrl, array(), 'POST', array('Content-Type' => 'application/json', 'Accept' => 'application/json' ));
  • 试试 $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => / ));
  • 不工作亲爱的.... debugInfo 如下所示:prntscr.com/evxuyu
  • 这是 PHP 版本的问题。它不适用于 php 5.6
  • 不确定然后仔细检查您已安装的 pecl/oauth 包
猜你喜欢
  • 2011-02-17
  • 2016-05-20
  • 1970-01-01
  • 2021-12-21
  • 2019-12-19
  • 1970-01-01
  • 2020-02-25
  • 2015-06-02
相关资源
最近更新 更多