【问题标题】:Symfony2 OAuth ClientSymfony2 OAuth 客户端
【发布时间】:2014-08-26 20:41:16
【问题描述】:

我正在尝试将这个 oauth 客户端库“adoy/oauth2”添加到 symfony 以连接我的 OAuth2 启用 API。这是我到目前为止所做的。

1) 作曲家.json

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

2) AuthController.php

// 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

// 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

// 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) 最后是 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%}]

错误:

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 行中定义

在尝试了几次调整 services.yml 之后,我现在只是想不通?请帮忙..

【问题讨论】:

    标签: php symfony oauth


    【解决方案1】:

    目前 Symfony2 无法使用 %parameter% 语法在服务参数中指定服务 ID。在 YAML 配置中,您应该使用 @service_id 语法作为服务参数。

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2021-07-16
      • 2015-08-22
      • 1970-01-01
      • 2012-11-20
      • 2013-07-03
      • 2018-05-25
      • 2015-01-14
      • 1970-01-01
      相关资源
      最近更新 更多