title: Symfony2 OAuth Client
body: <p>I am trying to add this oauth client library "adoy/oauth2" to symfony to connect with my OAuth2 enable API. This is what I have done so far.</p>
  
  <p>1) composer.json</p>
  
  <blockquote>
  <pre><code>"require": {
      "adoy/oauth2": "dev-master"
  }
  </code></pre>
  </blockquote>
  
  <p>2) AuthController.php</p>
  
  <blockquote>
    <p>// src/Test/WebBundle/Controller/AuthController.php
        namespace Test\WebBundle\Controller;</p>
  
  <pre><code>use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\HttpFoundation\Request;
  
  // these import the "@Route" and "@Template" annotations
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  
  use OAuth2;
  
  
  class AuthController extends Controller
  {
      /**
       * @Route("/authorize", name="auth")
       */
      public function authAction(Request $request)
      {
          $authorizeClient = $this-&gt;container-&gt;get('test_web.authorize_client');
  
          if (!$request-&gt;query-&gt;get('code')) {
              return new RedirectResponse($authorizeClient-&gt;getAuthenticationUrl());
          }
  
          $authorizeClient-&gt;getAccessToken($request-&gt;query-&gt;get('code'));
  
          return new Response($authorizeClient-&gt;fetch('https://api.test.me/me'));
      }
  }
  </code></pre>
  </blockquote>
  
  <p>3) OAuth2Client.php</p>
  
  <pre><code>// src/Test/WebBundle/Service/OAuth2Client.php
  namespace Test\WebBundle\Service;
  
  use OAuth2;
  
  class OAuth2Client
  {
      protected $client;
      protected $authEndpoint;
      protected $tokenEndpoint;
      protected $redirectUrl;
      protected $grant;
      protected $params;
  
      public function __construct(OAuth2\Client $client, $authEndpoint, $tokenEndpoint, $redirectUrl, $grant, $params)
      {
          $this-&gt;client = $client;
          $this-&gt;authEndpoint = $authEndpoint;
          $this-&gt;tokenEndpoint = $tokenEndpoint;
          $this-&gt;redirectUrl = $redirectUrl;
          $this-&gt;grant = $grant;
          $this-&gt;params = $params;
      }
  
      public function getAuthenticationUrl() {
          return $this-&gt;client-&gt;getAuthenticationUrl($this-&gt;authEndpoint, $this-&gt;redirectUrl);
      }
  
      public function getAccessToken($code = null)
      {
          if ($code !== null) {
              $this-&gt;params['code'] = $code;
          }
  
          $response = $this-&gt;client-&gt;getAccessToken($this-&gt;tokenEndpoint, $this-&gt;grant, $this-&gt;params);
          if(isset($response['result']) &amp;&amp; isset($response['result']['access_token'])) {
              $accessToken = $response['result']['access_token'];
              $this-&gt;client-&gt;setAccessToken($accessToken);
              return $accessToken;
          }
  
          throw new OAuth2\Exception(sprintf('Unable to obtain Access Token. Response from the Server: %s ', var_export($response)));
      }
  
      public function fetch($url)
      {
          return $this-&gt;client-&gt;fetch($url);
      }
  }
  </code></pre>
  
  <p>4) CredentialsCommand.php</p>
  
  <pre><code>// src/Test/WebBundle/Command/CredentialsCommand.php
  namespace Test\WebBundle\Command;
  
  use OAuth2;
  use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  use Symfony\Component\Console\Input\InputInterface;
  use Symfony\Component\Console\Output\OutputInterface;
  
  class CredentialsCommand extends ContainerAwareCommand
  {
      protected function configure()
      {
          $this
              -&gt;setName('oauth2:credentials')
              -&gt;setDescription('Executes OAuth2 Credentials grant');
      }
  
      protected function execute(InputInterface $input, OutputInterface $output)
      {
          $credentialsClient = $this-&gt;getContainer()-&gt;get('test_web.credentials_client');
          $accessToken = $credentialsClient-&gt;getAccessToken();
          $output-&gt;writeln(sprintf('Obtained Access Token: &lt;info&gt;%s&lt;/info&gt;', $accessToken));
  
          $url = 'http://oauth-server.local/api/articles';
          $output-&gt;writeln(sprintf('Requesting: &lt;info&gt;%s&lt;/info&gt;', $url));
          $response = $credentialsClient-&gt;fetch($url);
          $output-&gt;writeln(sprintf('Response: &lt;info&gt;%s&lt;/info&gt;', var_export($response, true)));
      }
  }
  </code></pre>
  
  <p>5) Finally services.yml which is something wrong I think</p>
  
  <pre><code>parameters:
      adoy_oauth2.client.class: OAuth2\Client
      test_web.class: Test\WebBundle\Service\OAuth2Client
  
  services:
      adoy_oauth2.client:
          class: %adoy_oauth2.client.class%
          arguments : [%oauth2_client_id%, %oauth2_client_secret%]
      test_web.credentials_client:
          class: %test_web.class%
          arguments : [%adoy_oauth2.client.class%, %oauth2_auth_endpoint%, %oauth2_token_endpoint%, %oauth2_redirect_url%, 'client_credentials', {client_id:%oauth2_client_id%},{client_secret:%oauth2_client_secret%}]
      test_web.authorize_client:
          class: %test_web.class%
          arguments : [adoy_oauth2.client, %oauth2_auth_endpoint%, %oauth2_token_endpoint%, %oauth2_redirect_url%, 'authorization_code', {redirect_uri:%oauth2_redirect_url%}]
  </code></pre>
  
  <p>The Error:</p>
  
  <p>ContextErrorException: Catchable Fatal Error: Argument 1 passed to Test\WebBundle\Service\OAuth2Client::__construct() must be an instance of OAuth2\Client, string given, called in C:\wamp\www\myproj\app\cache\web_dev\appWeb_devDebugProjectContainer.php on line 1460 and defined in C:\wamp\www\myproj\src\Test\WebBundle\Service\OAuth2Client.php line 16</p>
  
  <p>I am just out of idea now after trying a few times tweaking services.yml? Please help..</p>