我需要在社交网络上获取访问令牌,例如github。逻辑是当我的身份验证用户进入配置文件时,他可以放置按钮并确认社交网络帐户,在这种情况下,我需要访问令牌访问令牌oauth。我有标准的注册和身份验证,只有我需要这个是获取访问令牌并将其设置在数据库中。
我在内核中安装捆绑包写入
"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];
我怎样才能得到一个代币?
解决
当我在捆绑路由中添加 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 hub 时,我有另一个hwi_oauth_connect_service操作:连接服务操作并具有标准模板,如何重新加载此控制器和操作并更改模板?或者售后服务app.provider.user_provider如何在我的路由中进行重定向?
使用 FOSUserBundle 实现 HWIOAuthBundle 的一个很好的例子https://gist.github.com/danvbe/4476697
编辑
HWIOAuthBundle是OAuth的捆绑包,它为其配置/使用提供了很好的文档。
首先,您必须设置将使用 HWIOAuth 的防火墙。它应该与您的相同
然后,您必须在要连接用户的社交网络上注册您的Symfony2应用程序。
完成后,在捆绑包配置中,使用社交网络提供的凭据(应用程序 ID + 令牌)添加社交网络。请参阅配置资源所有者文档。
之后,要在社交网络上对用户进行身份验证并获取访问令牌,您必须将用户提供商连接到 HWIOAuthBundle。
第一个链接显示了如何使 HWIOAuthBundle 与 FOSUserBundle 作为用户提供者一起工作。您可以通过设置自己的用户提供程序并跳过有关 FOSUB 的步骤来轻松适应您的需求。
有很多例子,如HWIOAuthBundleByExample,展示了如何将其与基本用户提供程序一起使用。
另一个好处:将HWIOAuthBundle添加到您的symfony2项目中
主要基于配置,我也不会在此处重写捆绑包文档。
编辑2
路由中必须具有以下内容:
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