Magento的REST API的OAuth(Magento REST API OAuth)

2019-08-08 04:18发布

Magento的看到。 1.7.0.2

我是新来的Magento,REST和OAuth。 我希望能够使用Magento的REST API,以便能够修改一个在线商店与点播服务的客户管理系统。 我已经能够建立Magento的很轻松了,但我与REST挣扎,特别是使用OAuth。

我目前在OSX 10.7.5上运行MAMP 2.1.1 Magento的。 我能够用这些方向安装OAUTH: 在MAMP环境中安装PHP的OAuth

继Magento的教程中,我使用Firefox RESTClient实现测试的OAuth。 我已经建立了角色和在Magento消费者,并把钥匙放在了在RESTClient实现相应的字段。

当我发布到

http://localhost:8888/store/oauth/initiate?oauth_callback=http://localhost:8888/store/oauth_admin.php

我得到的效应初探

oauth_problem=signature_invalid&debug_sbs=QiIiTo3WGTZLTOhyIest9B5+l5s=

使用上的Magento测试PHP脚本( http://www.magentocommerce.com/api/rest/introduction.html )

<?php
/**
 * Example of products list retrieve using admin account via Magento REST
API. oAuth authorization is used
 */
$callbackUrl = "http://localhost:8888/store/oauth_admin.php";
$temporaryCredentialsRequestUrl =
"http://localhost:8888/store/oauth/initiate?oauth_callback=" .
urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://localhost:8888/store/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://localhost:8888/store/oauth/token';
$apiUrl = 'http://localhost:8888/store/api/rest';
$consumerKey = 'obscured';
$consumerSecret = 'obscured';

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

我得到以下错误:

Notice: Undefined index: state in /Applications/MAMP/htdocs/store/test.php on line 23

Notice: Undefined index: state in /Applications/MAMP/htdocs/store/test.php on line 29
OAuthException Object ( [message:protected] => Invalid auth/bad request (got a 401,         expected HTTP/1.1 20X or a redirect) [string:private] => [code:protected] => 401     [file:protected] => /Applications/MAMP/htdocs/store/test.php [line:protected] => 31 [trace:private] => Array ( [0] => Array ( [file] => /Applications/MAMP/htdocs/store/test.php [line] => 31 [function] => getRequestToken [class] => OAuth [type] => -> [args] => Array ( [0] => http://localhost:8888/store/oauth/initiate?oauth_callback=http%3A%2F%2Flocalhost%3A8888%2Fstore%2Foauth_admin.php ) ) ) [lastResponse] => oauth_problem=signature_invalid&debug_sbs=c3Pb0LJa26al02LJh9hSubXlBs8= [debugInfo] => Array ( [sbs] => GET&http%3A%2F%2Flocalhost%3A8888%2Fstore%2Foauth%2Finitiate&oauth_callback%3Dhttp%253A%252F%252Flocalhost%253A8888%252Fstore%252Foauth_admin.php%26oauth_consumer_key%3D41rv8qwkai1og9yp6ragyew5rag5e9oj%26oauth_nonce%3D10898187885101843ed45b24.99726561%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1359053886%26oauth_version%3D1.0 [headers_sent] => GET /store/oauth/initiate?oauth_callback=http%3A%2F%2Flocalhost%3A8888%2Fstore%2Foauth_admin.php&oauth_consumer_key=41rv8qwkai1og9yp6ragyew5rag5e9oj&oauth_signature_method=HMAC-SHA1&oauth_nonce=10898187885101843ed45b24.99726561&oauth_timestamp=1359053886&oauth_version=1.0&oauth_signature=F36aIxyET2XEVXDCJxm4jxGsRPg%3D HTTP/1.1 User-Agent: PECL-OAuth/1.2.3 Host: localhost:8888 Accept: */* [headers_recv] => HTTP/1.1 401 Authorization Required Date: Thu, 24 Jan 2013 18:58:06 GMT Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.8r DAV/2 PHP/5.2.17 X-Powered-By: PHP/5.2.17 Content-Length: 70 Content-Type: application/x-www-form-urlencoded [body_recv] => oauth_problem=signature_invalid&debug_sbs=c3Pb0LJa26al02LJh9hSubXlBs8= [info] => About to connect() to localhost port 8888 (#0) Trying ::1... connected Connected to localhost (::1) port 8888 (#0) Connection #0 to host localhost left intact Closing connection #0 ) ) 

至于我可以告诉我正确所做的一切,甚至于彻底清除Magento的和做搞清楚如何让安装的Oauth后全新安装。 我不知道下一步该怎么做和任何帮助将不胜感激,谢谢。

Answer 1:

我学会了不能包含一个OAuth请求的URL的端口号。 端口号被剥离,所以当键进行比较,它们不匹配。 端口更改为80(所以它没有被包含在URL)解决了这个问题。



Answer 2:

只是覆盖Mage_Oauth_Model_Server::_validateSignature()上线52(magetno CE 1.8.1)

$this->_request->getHttpHost()

并使其:

 $this->_request->getHttpHost(!Mage::getIsDeveloperMode())

这样一来,如果你是在开发人员模式(通过设置SetEnv在你的虚拟主机<Directory />部分),该方法不会破坏端口号,再加上它不会改变生产环境的行为。

++



文章来源: Magento REST API OAuth