【问题标题】:Magento OAuth Authentication can not handle custom URL schemeMagento OAuth 身份验证无法处理自定义 URL 方案
【发布时间】:2015-08-18 20:51:51
【问题描述】:

要取回 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 打开浏览器,这是我们应用程序中不需要的行为。

【问题讨论】:

    标签: ios swift magento oauth


    【解决方案1】:

    由于“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. 在工厂函数中找到“开关”代码块(~第 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; }
    3. 保存文件并清除 Magento 缓存。现在您的 myapp:// 方案将作为 http:// 和 https://

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 2015-04-09
      • 2013-03-28
      • 1970-01-01
      • 2010-09-13
      相关资源
      最近更新 更多