"http:// {root_dir}/oauth/token"在Magento中找不到用于注册REST API应用程序的文件

Pat*_*xit 6 php rest android oauth magento

我在ZEND中编写了代码,用于访问Magento REST API以访问数据.

<?php

require_once 'Zend/Oauth/Consumer.php';

class AuthController extends Zend_Controller_Action
{

public function init()
{
    $this->hostname = 'http://localhost/magento';
    $consumerKey = 'mkkzxuu1bkveejyzjam5hl2pzaxxepwv';
    $consumerSecret = 'bcmczrp3ofn9vmviqu3j8o1ioa7fisl6';
    $callbackUrl = 'http://localhost/magento/oauth/token';
    $this->config = array(
        'callbackUrl' => $callbackUrl,
        'requestTokenUrl' => $this->hostname . '/oauth/initiate',
        'siteUrl' => $this->hostname . '/oauth',
        'consumerKey' => $consumerKey,
        'consumerSecret' => $consumerSecret,
        'authorizeUrl' => $this->hostname . '/admin/oauth_authorize',
       //  'authorizeUrl' => $this->hostname . '/oauth/authorize',
        'accessTokenUrl' => $this->hostname . '/oauth/token'
    );
}

public function indexAction()
{
    $accesssession = new Zend_Session_Namespace('AccessToken');

    if (isset($accesssession->accessToken)) {

        $token = unserialize($accesssession->accessToken);
        // $client = $token->getHttpClient($this->config);
        $client = new Zend_Http_Client();
        $adapter = new Zend_Http_Client_Adapter_Curl();
        $client->setAdapter($adapter);
        $adapter->setConfig(array(
            'adapter'   => 'Zend_Http_Client_Adapter_Curl',
            'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
        ));
        $client->setUri($this->hostname . '/api/rest/products');
        $client->setParameterGet('oauth_token', $token->getToken());
        echo $token->getToken();
        echo $token->getTokenSecret();
        $client->setParameterGet('oauth_token_secret', $token->getTokenSecret());
        $response = $client->request('GET');
        $products = Zend_Json::decode($response->getBody());
    } else {
        $consumer = new Zend_Oauth_Consumer($this->config);
        $token = $consumer->getRequestToken();
        $requestsession = new Zend_Session_Namespace('RequestToken');
        $requestsession->requestToken = serialize($token);     
        $consumer->redirect();

    }
    $this->view->products = $products;
}

public function callbackAction()
{
    $requestsession = new Zend_Session_Namespace('RequestToken');
    if (!empty($_GET) && isset($requestsession->requestToken)) {
        $accesssession = new Zend_Session_Namespace('AccessToken');
        $consumer = new Zend_Oauth_Consumer($this->config);
        $token = $consumer->getAccessToken(
            $_GET,
            unserialize($requestsession->requestToken)
        );
        $accesssession->accessToken = serialize($token);
        // Now that we have an Access Token, we can discard the Request Token
     //   unset($requestsession->requestToken);
        // $this->_redirect();
        $this->_forward('index', 'index', 'default');
    } else {
        // Mistaken request? Some malfeasant trying something?
       // throw new Exception('Invalid callback request. Oops. Sorry.');
    }
}

public function callbackrejectedAction()
{
    // rejected
}
}
Run Code Online (Sandbox Code Playgroud)

我已经多次尝试这个网址了

http://localhost/magento/oauth/token?oauth_token=medensg02pvrd1rdfjcay4bwkr76whkk&oauth_verifier=qxvbth1rfe4vv78n7r6mprtxvuq2yqhb
Run Code Online (Sandbox Code Playgroud)

但没有得到任何东西,而不是文件未找到错误.

你可以看到这个网址存在于magento官方资源中. http://www.magentocommerce.com/api/rest/authentication/oauth_authentication.html

在接受此控制器之后,在接受此请求后,请求生成以进行授权.

引发错误文件未找到.

Ram*_*rma 2

首先,您需要安装 php 的 oauth 扩展,如果它已经安装,请检查您的 phpinfo 是否已启用。然后转到管理部分并进行以下更改以检查其余 api 的响应。

admin->system->Webservice->rest attribute->guest->resources access and set ALL

admin->system->webservice->rest roles->guest->resources access and set ALL
Run Code Online (Sandbox Code Playgroud)

保存设置并点击您的网址

http://hostname/magento/api/rest/products/
Run Code Online (Sandbox Code Playgroud)

它将以 xml 格式向您显示响应。稍后根据您的要求修改资源访问权限。

一旦你确保magento响应重置API而不是运行你的代码,我觉得它会起作用。