【问题标题】:Trying to Inject Parameters into Custom OAuthUserProvider尝试将参数注入自定义 OAuthUserProvider
【发布时间】:2015-02-06 04:09:15
【问题描述】:

在阅读文档后,我按照示例 here 添加了 HWIOAuthBundle。我被重定向到谷歌就好了,但是当它返回时我收到第一个参数丢失的警告。

config.yml

services:
    wxexchange_oauth_user_provider:
        class:      WX\ExchangeBundle\Service\OAuthUserProvider
        arguments:  [@session, @doctrine, @service_container]
hwi_oauth:
    resource_owners:
        google:
            type:                google
            client_id:           xxxxxx
            client_secret:       xxxxx
            scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
       user_response_class: \WX\ExchangeBundle\Service\OAuthUserProvider
    firewall_name:               main

security.yml

providers:
    my_custom_hwi_provider:
        id: wxexchange_oauth_user_provider 

firewalls:
    main:
        pattern: ^/
        anonymous: ~
        provider: main
        form_login:
            check_path: login_check
            login_path: /Login
            csrf_provider: form.csrf_provider
        logout:
            path: logout
        oauth:
            resource_owners:
                facebook:           "/Login/OAuth/check-facebook"
                google:             "/Login/OAuth/check-google"
            login_path:        /Login/OAuth
            use_forward:       false
            failure_path:      /Login
            oauth_user_provider:
                service: wxexchange_oauth_user_provider

routing.yml

hwi_oauth_login:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /Login/OAuth

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix: /Login/OAuth

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /Login/OAuth

google_login:
    pattern: /Login/OAuth/check-google

facebook_login:
    pattern: /Login/OAuth/check-facebook

OAuthUserProvider.php

class OAuthUserProvider extends BaseOAuthUserProvider
{
    protected $session, $doctrine, $admins;
    public function __construct($session, $doctrine, $service_container)
    {
        $this->session = $session;
        $this->doctrine = $doctrine;
        $this->container = $service_container;
    }
    public function loadUserByUsername($username)
    {
        //code
    }
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        //code
    }
}

错误:

警告:缺少参数 1 WX\ExchangeBundle\Service\OAuthUserProvider::__construct(),调用 /opt/lampp/htdocs/workoutexchange/trunk/WorkoutExchange/vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/OAuth/ResourceOwner/AbstractResourceOwner.php 在第 186 行并在 /opt/lampp/htdocs/workoutexchange/trunk/WorkoutExchange/src/WX/ExchangeBundle/Service/OAuthUserProvider.php 在第 13 行

【问题讨论】:

  • arguments: [[@session, @doctrine, @service_container]... 问题代码中有 两个 左大括号 [[。这反映了您的真实配置还是复制/粘贴错误?
  • 复制粘贴错误...我希望事情就这么简单。

标签: php symfony hwioauthbundle


【解决方案1】:

config.ymlservices: 部分的这一行是错误的:

    user_response_class: \WX\ExchangeBundle\Service\OAuthUserProvider

the configuration reference here。我不确定它需要是什么(Google Oauth example 没有提到它),但我认为它应该是一个简单的值对象,而不是你的用户提供者。

【讨论】:

    【解决方案2】:

    我为这个错误苦苦挣扎了很长时间。

    providers:
    --->>>my_custom_hwi_provider:
    ---->>>>>>> id: wxexchange_oauth_user_provider
    

    评论这两行。您在防火墙部分声明该服务。您不必将其声明为提供者。

    这样你的问题就解决了……

    firewalls:
    ........
            oauth:
                resource_owners:
                    facebook:           "/Login/OAuth/check-facebook"
                    google:             "/Login/OAuth/check-google"
                login_path:        /Login/OAuth
                use_forward:       false
                failure_path:      /Login
                oauth_user_provider:
                    service: wxexchange_oauth_user_provider
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2015-05-22
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      相关资源
      最近更新 更多