Php 使用令牌时Symfony Oauth2访问被拒绝
我跟随go将Oauth添加到我的Symfony Restful API中,问题是在我从服务器获得令牌后,我无法使其工作,只获得401,有人能帮助我吗?项目(上一次测试中的小更改除外)已启用。顺便说一句,我用的是symfony3.1 失败的测试:Php 使用令牌时Symfony Oauth2访问被拒绝,php,rest,symfony,oauth,fosoauthserverbundle,Php,Rest,Symfony,Oauth,Fosoauthserverbundle,我跟随go将Oauth添加到我的Symfony Restful API中,问题是在我从服务器获得令牌后,我无法使其工作,只获得401,有人能帮助我吗?项目(上一次测试中的小更改除外)已启用。顺便说一句,我用的是symfony3.1 失败的测试: public function testCreate() { $clientManager = $this->client->getContainer()->get('fos_oauth_server.client_
public function testCreate()
{
$clientManager = $this->client->getContainer()->get('fos_oauth_server.client_manager.default');
$client = $clientManager->createClient();
$client->setRedirectUris(array('http://www.example.com'));
$client->setAllowedGrantTypes(array('token', 'authorization_code', 'password'));
$clientManager->updateClient($client);
$crawler = $this->client->request(
'POST',
'/oauth/v2/token',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
'{
"grant_type":"password",
"client_id":"' . $client->getId() . '_' . $client->getRandomId() . '",
"client_secret":"' . $client->getSecret() . '",
"username": "Alvaro",
"password": "1231251265"
}'
);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$response = json_decode($this->client->getResponse()->getContent(), true);
$this->assertTrue( strlen($response['access_token']) > 10 );
$this->assertEquals( 'bearer', $response['token_type'] );
$this->assertTrue( strlen($response['refresh_token']) > 10 );
$this->assertEquals( 3600, $response['expires_in'] );
$crawler = $this->client->request(
'GET',
'/businesses', //@TODO: Move this to a common route
array(),
array(),
array('CONTENT_TYPE' => 'application/json'));
$this->assertEquals(401, $this->client->getResponse()->getStatusCode());
$crawler = $this->client->request(
'GET',
'/businesses', //@TODO: Move this to a common route
array(),
array(),
array('CONTENT_TYPE' => 'application/json', 'Authorization:Bearer' => $response['access_token']));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); <-- failing assertion
`
config.yml
fos_rest:
routing_loader:
default_format: json # All responses should be JSON formated
include_format: false # We do not include format in request, so that all responses
# will eventually be JSON formated
body_listener:
decoders:
json: fos_rest.decoder.json
fos_user:
db_driver: orm
firewall_name: api # Seems to be used when registering user/reseting password,
# but since there is no "login", as so it seems to be useless in
# our particular context, but still required by "FOSUserBundle"
user_class: Aescarcha\UserBundle\Entity\User
fos_oauth_server:
db_driver: orm
client_class: Aescarcha\OauthServerBundle\Entity\Client
access_token_class: Aescarcha\OauthServerBundle\Entity\AccessToken
refresh_token_class: Aescarcha\OauthServerBundle\Entity\RefreshToken
auth_code_class: Aescarcha\OauthServerBundle\Entity\AuthCode
service:
user_provider: fos_user.user_manager # This property will be used when valid credentials are given to load the user upon access token creation
fos_rest:
routing_loader:
default_format: json # All responses should be JSON formated
include_format: false # We do not include format in request, so that all responses
# will eventually be JSON formated
body_listener:
decoders:
json: fos_rest.decoder.json
fos_user:
db_driver: orm
firewall_name: api # Seems to be used when registering user/reseting password,
# but since there is no "login", as so it seems to be useless in
# our particular context, but still required by "FOSUserBundle"
user_class: Aescarcha\UserBundle\Entity\User
fos_oauth_server:
db_driver: orm
client_class: Aescarcha\OauthServerBundle\Entity\Client
access_token_class: Aescarcha\OauthServerBundle\Entity\AccessToken
refresh_token_class: Aescarcha\OauthServerBundle\Entity\RefreshToken
auth_code_class: Aescarcha\OauthServerBundle\Entity\AuthCode
service:
user_provider: fos_user.user_manager # This property will be used when valid credentials are given to load the user upon access token creation