问题:Magento OAuth Authentication 无法处理自定义 URL 方案

为了取回 OAuth 令牌,我想使用自定义 URL 方案作为我的 iOS 应用程序的回调 URL,例如myapp://oauth-callback.

但是,Magento 似乎无法处理这样的 URL 方案,因为它返回以下错误消息

HTTP Status 400: Bad Request, Response: oauth_problem=parameter_rejected&message=oauth_callback

如果我设置了一个以 http:// 开头的回调 URL,则请求确实有效并且我返回了 OAuth 令牌,问题是操作系统使用此 URL 打开浏览器,这是我们应用程序中不需要的行为。

解答

由于“myapp”,您的 url 在 Zend_Uri::check($url) 中无效。这是 check() 函数:

public static function check($uri)
{
    try {
        $uri = self::factory($uri);
    } catch (Exception $e) {
        return false;
    }

    return $uri->valid();
}

让我们看看工厂是如何工作的:

public static function factory($uri = 'http', $className = null)
    {
        // Separate the scheme from the scheme-specific parts
        $uri            = explode(':', $uri, 2);
        $scheme         = strtolower($uri[0]);
        $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';

        if (strlen($scheme) === 0) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception('An empty string was supplied for the scheme');
        }

        // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
        if (ctype_alnum($scheme) === false) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
        }

        if ($className === null) {
            /**
             * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the
             * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown.
             */
            switch ($scheme) {
                case 'http':
                    // Break intentionally omitted
                case 'https':
                    $className = 'Zend_Uri_Http';
                    break;

                case 'mailto':
                    // TODO
                default:
                    #require_once 'Zend/Uri/Exception.php';
                    throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
                    break;
            }
        }

        #require_once 'Zend/Loader.php';
        try {
            Zend_Loader::loadClass($className);
        } catch (Exception $e) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception("\"$className\" not found");
        }

        $schemeHandler = new $className($scheme, $schemeSpecific);

        if (! $schemeHandler instanceof Zend_Uri) {
            #require_once 'Zend/Uri/Exception.php';
            throw new Zend_Uri_Exception("\"$className\" is not an instance of Zend_Uri");
        }

        return $schemeHandler;
    }

它为 http:// 和 https:// 方案使用 Zend_Uri_Http 类。您只需要在列表中添加自己的方案。

怎么修?

1.复制lib/Zend/Uri.php文件到app/code/local/Zend/Uri.php

2.在工厂函数中找到'switch'代码块(~第119行)并将其替换为下一个: switch ($scheme) { case 'http': // Break intentionally omitted case 'https': $className = 'Zend_Uri_Http'; break; case 'myapp': $className = 'Zend_Uri_Http'; break; case 'mailto': // TODO default: #require_once 'Zend/Uri/Exception.php'; throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported"); break; }

  1. 保存文件并清除 Magento 缓存。现在您的 myapp:// 方案将作为 http:// 和 https:// 工作
Logo

更多推荐