微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将storeUrl传递给默认的Magento oauth脚本?

如何解决如何将storeUrl传递给默认的Magento oauth脚本?

我正在尝试创建我的第一个Magento 2扩展和集成,并且一直遵循他们的文档here中的指南。到目前为止一切顺利,我已经完成了auth握手,存储了api请求的所有必需键,并且可以使请求返回我的扩展程序。

看一下本教程底部提供的OauthClient.PHP脚本,该URL的编码如下: return new Uri('http://magento.host/oauth/token/request');,本教程建议您“将本示例中的http://magento.host的实例更改为有效的基本URL。”

<?PHP

use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Http\Uri\UriInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\OAuth1\Service\AbstractService;
use OAuth\OAuth1\Signature\SignatureInterface;
use OAuth\OAuth1\Token\StdOAuth1Token;
use OAuth\OAuth1\Token\TokenInterface;

class OauthClient extends AbstractService
{
    /** @var string|null */
    protected $_oauthVerifier = null;

    public function __construct(
        Credentials $credentials,ClientInterface $httpClient = null,TokenStorageInterface $storage = null,SignatureInterface $signature = null,UriInterface $baseApiUri = null
    ) {
        if (!isset($httpClient)) {
            $httpClient = new \OAuth\Common\Http\Client\StreamClient();
        }
        if (!isset($storage)) {
            $storage = new \OAuth\Common\Storage\Session();
        }
        if (!isset($signature)) {
            $signature = new \OAuth\OAuth1\Signature\Signature($credentials);
        }
        parent::__construct($credentials,$httpClient,$storage,$signature,$baseApiUri);
    }

    /**
     * @return UriInterface
     */
    public function getRequestTokenEndpoint()
    {
        return new Uri('http://magento.host/oauth/token/request');
    }

    /**
     * Returns the authorization API endpoint.
     *
     * @throws \OAuth\Common\Exception\Exception
     */
    public function getAuthorizationEndpoint()
    {
        throw new \OAuth\Common\Exception\Exception(
            'Magento REST API is 2-legged. Current operation is not available.'
        );
    }

    /**
     * Returns the access token API endpoint.
     *
     * @return UriInterface
     */
    public function getAccesstokenEndpoint()
    {
        return new Uri('http://magento.host/oauth/token/access');
    }

    /**
     * Parses the access token response and returns a TokenInterface.
     *
     * @param string $responseBody
     * @return TokenInterface
     */
    protected function parseAccesstokenResponse($responseBody)
    {
        return $this->_parsetoken($responseBody);
    }

    /**
     * Parses the request token response and returns a TokenInterface.
     *
     * @param string $responseBody
     * @return TokenInterface
     * @throws TokenResponseException
     */
    protected function parseRequestTokenResponse($responseBody)
    {
        $data = $this->_parseResponseBody($responseBody);
        if (isset($data['oauth_verifier'])) {
            $this->_oauthVerifier = $data['oauth_verifier'];
        }
        return $this->_parsetoken($responseBody);
    }

    /**
     * Parse response body and create oAuth token object based on parameters provided.
     *
     * @param string $responseBody
     * @return StdOAuth1Token
     * @throws TokenResponseException
     */
    protected function _parsetoken($responseBody)
    {
        $data = $this->_parseResponseBody($responseBody);
        $token = new StdOAuth1Token();
        $token->setRequestToken($data['oauth_token']);
        $token->setRequestTokenSecret($data['oauth_token_secret']);
        $token->setAccesstoken($data['oauth_token']);
        $token->setAccesstokenSecret($data['oauth_token_secret']);
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
        unset($data['oauth_token'],$data['oauth_token_secret']);
        $token->setExtraParams($data);
        return $token;
    }

    /**
     * Parse response body and return data in array.
     *
     * @param string $responseBody
     * @return array
     * @throws \OAuth\Common\Http\Exception\TokenResponseException
     */
    protected function _parseResponseBody($responseBody)
    {
        if (!is_string($responseBody)) {
            throw new TokenResponseException("Response body is expected to be a string.");
        }
        parse_str($responseBody,$data);
        if (null === $data || !is_array($data)) {
            throw new TokenResponseException('Unable to parse response.');
        } elseif (isset($data['error'])) {
            throw new TokenResponseException("Error occurred: '{$data['error']}'");
        }
        return $data;
    }

    /**
     * @override to fix since parent implementation from lib not sending the oauth_verifier when requesting access token
     * Builds the authorization header for an authenticated API request
     *
     * @param string $method
     * @param UriInterface $uri the uri the request is headed
     * @param \OAuth\OAuth1\Token\TokenInterface $token
     * @param $bodyParams array
     * @return string
     */
    protected function buildAuthorizationHeaderForAPIRequest(
        $method,UriInterface $uri,TokenInterface $token,$bodyParams = null
    ) {
        $this->signature->setTokenSecret($token->getAccesstokenSecret());
        $parameters = $this->getBasicAuthorizationHeaderInfo();
        if (isset($parameters['oauth_callback'])) {
            unset($parameters['oauth_callback']);
        }

        $parameters = array_merge($parameters,['oauth_token' => $token->getAccesstoken()]);
        $parameters = array_merge($parameters,$bodyParams);
        $parameters['oauth_signature'] = $this->signature->getSignature($uri,$parameters,$method);

        $authorizationHeader = 'OAuth ';
        $delimiter = '';

        foreach ($parameters as $key => $value) {
            $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"';
            $delimiter = ',';
        }

        return $authorizationHeader;
    }
}

我的问题是如何从存储中传递已在其中设置了集成的URL作为变量?(我将其存储在数据库中)

感谢您抽出宝贵时间来看看。

解决方法

对于以后从数据库表中获取保存的数据之后可能遇到的任何问题,我将Url添加到调用中以在文档的checklogin.php脚本上创建新类:>

$oAuthClient = new OauthClient($credentials,$magentoBaseUrl);

然后在OauthClient.php中,我将url添加到构造中,并更新了getRequestTokenEndpoint方法和getAcessTokenEndpoint:


    public function __construct(
        Credentials $credentials,$magentoBaseUrl = null,ClientInterface $httpClient = null,TokenStorageInterface $storage = null,SignatureInterface $signature = null,UriInterface $baseApiUri = null

    ) {
        if (!isset($httpClient)) {
            $httpClient = new \OAuth\Common\Http\Client\CurlClient();
        }
        if (!isset($storage)) {
            $storage = new \OAuth\Common\Storage\Session();
        }
        if (!isset($signature)) {
            $signature = new \OAuth\OAuth1\Signature\Signature($credentials);
        }
        if (!isset($magentoBaseUrl)) {
            die();
        }
        $this->magentoBaseUrl = $magentoBaseUrl;
        parent::__construct($credentials,$httpClient,$storage,$signature,$baseApiUri);
    }

...

    /**
     * @return UriInterface
     */
    public function getRequestTokenEndpoint()
    {
        return new Uri($this->magentoBaseUrl.'/oauth/token/request');
    }
...

    /**
     * Returns the access token API endpoint.
     *
     * @return UriInterface
     */
    public function getAccessTokenEndpoint()
    {
        return new Uri($this->magentoBaseUrl.'/oauth/token/access');
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。