Symfony HWIOAuthBundle 访问令牌

Symfony HWIOAuthBundle Accesstoken

我需要在社交网络上获取访问令牌,例如 github。逻辑是当我的身份验证用户输入个人资料时,他可以放置按钮并确认社交网络帐户,这种情况我需要访问令牌访问令牌 oauth。我有标准的注册和身份验证,我只需要获取访问令牌并将其设置在数据库中。

我在内核中安装 bundle write

"hwi/oauth-bundle": "0.4.*@dev"
new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),

我的配置:

hwi_oauth:
connect:
  account_connector: app.provider.user_provider

firewall_name: secured_area

resource_owners:
    github:
        type:                github
        client_id:           "%github_app_id%"
        client_secret:       "%github_app_secret%"
        scope:              "user,public_repo"

和我的路由:

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

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

github_login:
path: /login/check-github

和 routnig 捆绑

hwi_oauth_security:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login

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

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

还有我的保安:

firewalls:
        secured_area:
        anonymous: ~
        oauth:
            resource_owners:
                github:          "/login/check-github"
            login_path:        /login
            use_forward:       false
            failure_path:      /login

            oauth_user_provider:
                service: app.provider.user_provider

和服务配置:

<parameters>
        <parameter key="my_user_provider.class">Artel\ProfileBundle\Providers\UserProvider</parameter>
</parameters>

        <service id="app.provider.user_provider" class="%my_user_provider.class%">

        <argument type="collection">
            <argument key="github">githubId</argument>
        </argument>

        <call method="setGithubProvider">
            <argument type="service" id="geekhub.user.github_provider" />
        </call>

    </service>

class UserProvider implements OAuthAwareUserProviderInterface
{
/**
 * {@inheritDoc}
 */
public function connect(UserInterface $user, UserResponseInterface $response)
{
   //I think this I get access token in request
}

/**
 * {@inheritdoc}
 */
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
}

并在模板中

 <a class="logo" href="{{ path('hwi_oauth_service_redirect', {'service' : 'github'}) }}">Gh</a>

但是我有错误

Unable to generate a URL for the named route "hwi_oauth_connect_service" as such route does not exist.

堆栈

in app/cache/dev/appDevUrlGenerator.php at line 259   -
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
    if (!isset(self::$declaredRoutes[$name])) {
        throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
    }
    list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = self::$declaredRoutes[$name];

我怎样才能得到令牌?

已解决

当我在捆绑路由中添加 routkng 时,我解决了这个问题

hwi_oauth_security:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login

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

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

但是现在我有另一个当我连接到 git 集线器时我在 hwi_oauth_connect_service 动作中输入了 rout:connectServiceAction 并且有标准模板,如何重新加载这个控制器和动作并更改模板? ? 或者 after service app.provider.user_provider 如何在我的路由中进行重定向?

使用 FOSUserBundle 实现 HWIOAuthBundle 的一个很好的例子 https://gist.github.com/danvbe/4476697

编辑

HWIOAuthBundle 是 OAuth 的一个包,它为其 configuration/usage.

提供了很好的 documentation

首先,您必须设置将使用 HWIOAuth 的防火墙。 它应该和你的一样

然后,您必须在您想要连接用户的社交网络上注册您的 Symfony2 应用程序。
完成后,在捆绑配置中,使用社交网络提供的凭据(应用程序 ID + 令牌)添加社交网络。 请参阅 configuring resource owners 文档。

之后,要在社交网络上验证您的用户并获取访问令牌,您必须将您的用户提供程序连接到 HWIOAuthBundle。

第一个 link 展示了如何使 HWIOAuthBundle 与 FOSUserBundle 作为用户提供程序一起工作。 您可以通过设置自己的用户提供程序并跳过有关 FOSUB 的步骤来轻松地使其适应您的需求。

有很多示例,例如 HWIOAuthBundleByExample,展示了如何将其与基本用户提供程序一起使用。
另一个好处:Adding HWIOAuthBundle to your symfony2 project

这个包主要是基于配置的,我也不会在这里重写包文档。

EDIT2

您的路由中必须包含以下内容:

hwi_oauth_login:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /login
hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect
hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix:   /connect
# ... Add your other routes here