2

I need to get access token on social network, example github. The logic is when my authentication user enter in profile he can put button and confirm account with social network and this situation I need access token access token oauth. I have standard registration and authentication and only I need this is get access token and set it in DB.

I install bundle write in kernel

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

my config:

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"

and my routing:

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

and routnig in bundle

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

and my security:

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

and config for service:

<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)
{
}

and in template

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

but I have error

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

stack

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];

How can I just get a token ?

SOLVED

when I add routkng in bundle routing I solved this problem

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

But now I have another when I connect to git hub I have enter in rout in hwi_oauth_connect_service action: connectServiceAction and have standard template, how to reload this controller and action and change template ?? Or how after service app.provider.user_provider make redirect in my routing?

shuba.ivan
  • 3,824
  • 8
  • 49
  • 121
  • Used to try this bundle. To my mind this bundle could be useful, but it is not so hard to implement social functionality without it - there is too much code and, as I remember, the max number of requests to login user through sn was 3 (ok.ru oauth realisation), so we decided to write a little piece of code instead of using this bundle (but we already have guzzle in project - so this variant was easier to us). – Ilya Yarkovets Jan 28 '16 at 07:17
  • I update my question – shuba.ivan Feb 01 '16 at 19:35
  • To get the token, you have to configure your network properly and browse the login url, you will be redirected to the network for give access and back to your app with the response containing the token and all other informations of your user. In your security.yml, change your resource_owner from 'my_gihub' to 'github' – chalasr Feb 02 '16 at 07:04
  • 1
    Done. Now have - Unable to generate a URL for the named route "hwi_oauth_connect_service" as such route does not exist. – shuba.ivan Feb 02 '16 at 15:04

1 Answers1

2

A very good example of HWIOAuthBundle implementation with FOSUserBundle https://gist.github.com/danvbe/4476697

EDIT

HWIOAuthBundle is a bundle for OAuth which provides a great documentation for its configuration/usage.

First, you have to set the firewall on which HWIOAuth will be used. It should be the same as your

Then, you have to register your Symfony2 application on the social network where you would like to connect your users.
When it's done, in the bundle configuration, add the social network with the credentials (application id + token) provided by the social network). See configuring resource owners doc.

After that, to authenticate your users on the social network and get the access token, you have to connect your user provider to the HWIOAuthBundle.

The first link show how make HWIOAuthBundle working with FOSUserBundle as user provider. You can easily adapt it to your needs by setting your own user provider and skip the step about FOSUB.

There is a lot of examples such as HWIOAuthBundleByExample which show how use it with a basic user provider.
Another good : Adding HWIOAuthBundle to your symfony2 project

This bundle is primarily based on configuration, also I will not rewrite the bundle documentation here.

EDIT2

You must have the following in your routing :

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
chalasr
  • 12,971
  • 4
  • 40
  • 82
  • I dont need FOSUserBundle, only I need access token. – shuba.ivan Jan 28 '16 at 09:00
  • See the changes on my answer. To get an access token, you have to authenticate your user on the social network through OAuth. – chalasr Jan 28 '16 at 15:41
  • In your security.yml, please change the resource owner from `my_github` to `github` and retry. Keep me informed – chalasr Feb 01 '16 at 20:02
  • 1
    Done. Now have - Unable to generate a URL for the named route "hwi_oauth_connect_service" as such route does not exist. – shuba.ivan Feb 02 '16 at 15:04
  • thnks, this is ok. And I update my question? may be you know how to reload action and template ? – shuba.ivan Feb 02 '16 at 15:28
  • You're welcome. I don't really understand what you mean by reload action and template ? I think you should open an other question for this specific need, but if you can, try to explain me what you need with a generic use case. If you just want override the default template, say me and I will explain you which possibilities you have. – chalasr Feb 02 '16 at 15:32
  • I think the answers of this question can help you to customize your implementation. http://stackoverflow.com/questions/20998778/how-can-i-override-hwioauthbundle-twig-file – chalasr Feb 02 '16 at 15:51
  • thks, everything worked. Maybe you know how after service app.provider.user_provider make redirect in my routing? – shuba.ivan Feb 03 '16 at 09:12
  • Take a look at this answser http://stackoverflow.com/a/27750126/4363634 and others on the same post. – chalasr Feb 03 '16 at 09:56