简体   繁体   English

Symfony2 OAuth客户端

[英]Symfony2 OAuth Client

I am trying to add this oauth client library "adoy/oauth2" to symfony to connect with my OAuth2 enable API. 我正在尝试将此oauth客户端库“ adoy / oauth2”添加到symfony以与我的OAuth2 enable API连接。 This is what I have done so far. 到目前为止,这是我所做的。

1) composer.json 1)composer.json

 "require": { "adoy/oauth2": "dev-master" } 

2) AuthController.php 2)AuthController.php

// src/Test/WebBundle/Controller/AuthController.php namespace Test\\WebBundle\\Controller; // src / Test / WebBundle / Controller / AuthController.php名称空间Test \\ WebBundle \\ Controller;

 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->container->get('test_web.authorize_client'); if (!$request->query->get('code')) { return new RedirectResponse($authorizeClient->getAuthenticationUrl()); } $authorizeClient->getAccessToken($request->query->get('code')); return new Response($authorizeClient->fetch('https://api.test.me/me')); } } 

3) OAuth2Client.php 3)OAuth2Client.php

// 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->client = $client;
        $this->authEndpoint = $authEndpoint;
        $this->tokenEndpoint = $tokenEndpoint;
        $this->redirectUrl = $redirectUrl;
        $this->grant = $grant;
        $this->params = $params;
    }

    public function getAuthenticationUrl() {
        return $this->client->getAuthenticationUrl($this->authEndpoint, $this->redirectUrl);
    }

    public function getAccessToken($code = null)
    {
        if ($code !== null) {
            $this->params['code'] = $code;
        }

        $response = $this->client->getAccessToken($this->tokenEndpoint, $this->grant, $this->params);
        if(isset($response['result']) && isset($response['result']['access_token'])) {
            $accessToken = $response['result']['access_token'];
            $this->client->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->client->fetch($url);
    }
}

4) CredentialsCommand.php 4)CredentialsCommand.php

// 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
            ->setName('oauth2:credentials')
            ->setDescription('Executes OAuth2 Credentials grant');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $credentialsClient = $this->getContainer()->get('test_web.credentials_client');
        $accessToken = $credentialsClient->getAccessToken();
        $output->writeln(sprintf('Obtained Access Token: <info>%s</info>', $accessToken));

        $url = 'http://oauth-server.local/api/articles';
        $output->writeln(sprintf('Requesting: <info>%s</info>', $url));
        $response = $credentialsClient->fetch($url);
        $output->writeln(sprintf('Response: <info>%s</info>', var_export($response, true)));
    }
}

5) Finally services.yml which is something wrong I think 5)最后services.yml,我认为这是错误的

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%}]

The Error: 错误:

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 ContextErrorException:可捕获的致命错误:传递给Test \\ WebBundle \\ Service \\ OAuth2Client :: __ construct()的参数1必须是OAuth2 \\ Client的实例,给定字符串,在C:\\ wamp \\ www \\ myproj \\ app \\ cache \\ web_dev中调用\\ appWeb_devDebugProjectContainer.php在第1460行,并在C:\\ wamp \\ www \\ myproj \\ src \\ Test \\ WebBundle \\ Service \\ OAuth2Client.php第16行中定义

I am just out of idea now after trying a few times tweaking services.yml? 经过几次调整services.yml之后,我现在不知道了。 Please help.. 请帮忙..

At the moment Symfony2 does not have a way to specify a service id in service arguments using %parameter% syntax. 目前,Symfony2无法使用%parameter%语法在服务参数中指定服务ID。 In YAML config, you should use @service_id syntax for service arguments. 在YAML配置中,您应该对服务参数使用@service_id语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM