简体   繁体   English

Magento API Rest OAuth未授权消费者

[英]Magento api rest oauth not authorizing consumer

I'm attempting to connect to my magento api from an external server but i'm having an issue with OAuth. 我正在尝试从外部服务器连接到我的magento api,但是OAuth出现问题。

I've created a consumer in the backend, assigned what it can access, authorized the consumer through oauth using terminal and it gave me my token and token secret. 我在后端创建了一个使用者,分配了它可以访问的内容,使用终端通过oauth授权了该使用者,并且它给了我我的令牌和令牌密钥。

My PHP is as follows; 我的PHP如下:

<?php

$hostUrl = 'redacted';
$callbackUrl = $hostUrl."oauth_customer.php";
$temporaryCredentialsRequestUrl = $hostUrl."oauth/initiate?oauth_callback=".urlencode($callbackUrl);
$adminAuthorizationUrl = $hostUrl."oauth/authorize";
$accessTokenRequestUrl = $hostUrl."oauth/token";
$apiUrl = $hostUrl."api/rest";


$consumerKey = 'redacted';
$consumerSecret = 'redacted';

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

When I run this php file it redirects me to the magento site and says; 当我运行这个php文件时,它会将我重定向到magento网站并说;

AUTHORIZE APPLICATION consumer_name requests access to your account After authorization application will have access to you account. 授权应用程序Consumer_name请求访问您的帐户授权应用程序将有权访问您的帐户。 Authorize | 授权| Reject 拒绝

When I click "Authorize" it redirects me to a 404 within magento. 当我单击“授权”时,它会将我重定向到magento中的404。 If I return to the php file it will redirect me to that same "authorize application" page over and over again. 如果我返回到php文件,它将一遍又一遍将我重定向到相同的“授权应用程序”页面。

I already have the token and such so I assume that it should already be authorized. 我已经有了令牌,因此我假设它应该已经被授权。

I'm attempting to access the name of products as well as their inventory quantity so I can't simply use guest access. 我试图访问产品的名称及其库存数量,所以我不能简单地使用访客访问权限。 Any help would be greatly appreciated. 任何帮助将不胜感激。

您的回调URL不应位于远程主机上,而应位于应用程序的主机上。

删除$ callbackUrl行并与以下代码交换:

$callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM