【问题标题】:Not able to use Magento REST APIs using OAuth无法通过 OAuth 使用 Magento REST API
【发布时间】:2013-03-17 17:32:06
【问题描述】:

我正在使用 Magento 版本 1.7.0.2 并尝试通过 OAuth 集成来使用 Magento Rest API。 我已经安装了 OAuth,下面是我放在 magento 根目录中的代码的 sn-p,我通过键入 http://x.x.x.x:5009/oauth_customer.php 在 Web 浏览器中运行它

$callbackUrl = "http://x.x.x.x:5009/oauth_customer.php";
$temporaryCredentialsRequestUrl = "http://x.x.x.x:5009/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://x.x.x.x:5009/oauth/authorize';
$accessTokenRequestUrl = "http://x.x.x.x:5009/oauth/token";
$apiUrl = "http://x.x.x.x:5009/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";
    $oauthClient->fetch($resourceUrl);
    $productsList = json_decode($oauthClient->getLastResponse());
    print_r($productsList);
}
} catch (OAuthException $e) {
   print_r($e);
}

http://x.x.x.x:5009 是 IP 地址,后跟 5009,其中 5009 是指定的端口号。 当我们在浏览器中运行它时,我总是收到以下错误 - 无效的身份验证/错误请求(收到 401,预期 HTTP/1.1 20X 或重定向) oauth_problem=signature_invalid&debug_sbs=Bya6oE4ujTEEFLVL6Mm04PqTA4g=

我无法完成这项工作。

注意 - 我已经生成了消费者密钥和密钥。不确定如何创建具有客户访问 REST API 资源的用户凭据适合上述脚本。 另外我想知道我们是否可以在任何非 magento 站点上以编程方式使用 magento apis 与 oAuth 集成,而无需用户每次都授予对应用程序的访问权限以生成请求令牌。

【问题讨论】:

标签: api magento rest oauth magento-1.7


【解决方案1】:

您必须先生成 oauth 令牌。关注这个http://www.aschroder.com/2012/04/introduction-to-the-magento-rest-apis-with-oauth-in-version-1-7/ 然后测试http://www.magentocommerce.com/api/rest/testing_rest_resources.html

如果您不想进行身份验证,您可以使用 curl 来获取所需的数据。 http://snipplr.com/view/44760/

在 url 中传递有效的 magento 资源 url,例如 www.yourwebsite.com/api/rest/products

【讨论】:

  • 如何为 MAgento 创建应用程序?
猜你喜欢
  • 2014-09-22
  • 1970-01-01
  • 2013-01-16
  • 2013-07-21
  • 2017-10-12
  • 2016-04-17
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多