Magento 1. * Rest API PHP OAuth身份验证

时间:2018-06-28 08:50:57

标签: rest api magento-1.9

我的Magento 1.9 rest API出现问题, 一切都在magento上配置(oauth用户/ keys权限)

我使用Magento文档来获取产品列表并连接到oauth:

代码如下:

// $callbackUrl is a path to your file with OAuth authentication example for the Admin user
$callbackUrl = "http://127.0.0.1:8880/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://127.0.0.1:8880/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
// var_dump($temporaryCredentialsRequestUrl);die();
$adminAuthorizationUrl = 'http://127.0.0.1:8880/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://127.0.0.1:8880/api/rest';
$consumerKey = 'bbc9709fae90569357a40717f75ce911';
$consumerSecret = 'd5c43ea001aa8937c78c25b6c6aa530e';

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_PLAINTEXT, $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, 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);
}

调用此代码时,我得到一个xhr呼叫:

http://127.0.0.1:8880/admin/oAuth_authorize?oauth_token=e8e8e6d8fd36bf55361231f3910e5ee5

我的应用程序返回[XHR]致命错误。 但是,当我在浏览器中调用网址时: enter image description here

我得到这个,单击授权,每次都关闭我的本地服务器: enter image description here

我真的很坚持这一点。...尝试了我所知道的一切,如果有人有主意,不客气。

0 个答案:

没有答案