我需要在社交网络上获得访问令牌,例如github。逻辑是,当我的身份验证用户输入配置文件时,他可以在社交网络上放置按钮并确认帐户,而这种情况下,我需要访问令牌访问令牌oauth。我有标准的注册和身份验证,唯一需要的就是get访问令牌,并在DB中设置它。
我在内核中安装了包写
"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在捆绑中穿行
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];我怎么才能得到象征性的东西呢?
解出
当我在捆绑路由中添加了路由时,我解决了这个问题。
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和标准模板,如何重新加载这个控制器和操作并更改模板?或者服务后app.provider.user_provider如何在我的路由中进行重定向?
发布于 2016-01-27 23:28:36
一个很好的用HWIOAuthBundle实现FOSUserBundle https://gist.github.com/danvbe/4476697的例子
编辑
HWIOAuthBundle是OAuth的一个包,它为其配置/使用提供了一个很好的文档。
首先,您必须设置使用HWIOAuth的防火墙。它应该和你的一样
然后,您必须在您希望连接用户的社交网络上注册您的Symfony2应用程序。
完成后,在包配置中添加由社交网络提供的凭据(应用程序id +令牌)。见https://github.com/hwi/HWIOAuthBundle/blob/0.4/Resources/doc/2-configuring_resource_owners.md医生。
之后,要在社交网络上对用户进行身份验证并获取访问令牌,您必须将用户提供程序连接到HWIOAuthBundle。
第一个链接展示了如何使HWIOAuthBundle作为用户提供程序使用FOSUserBundle。通过设置您自己的用户提供程序并跳过有关FOSUB的步骤,您可以很容易地使其适应您的需要。
有很多例子,比如HWIOAuthBundleByExample,它们展示了如何在基本用户提供程序中使用它。
另一件好事:将HWIOAuthBundle添加到symfony2项目中
这个包主要是基于配置的,这里我也不会重写包文档。
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 herehttps://stackoverflow.com/questions/35045758
复制相似问题