KnpU\OAuth2ClientBundle\Client\OAuth2Client::getAccessToken PHP Method

getAccessToken() public method

Call this after the user is redirected back to get the access token.
public getAccessToken ( ) : League\OAuth2\Client\Token\AccessToken
return League\OAuth2\Client\Token\AccessToken
    public function getAccessToken()
    {
        if (!$this->isStateless) {
            $expectedState = $this->getSession()->get(self::OAUTH2_SESSION_STATE_KEY);
            $actualState = $this->getCurrentRequest()->query->get('state');
            if (!$actualState || $actualState !== $expectedState) {
                throw new InvalidStateException('Invalid state');
            }
        }
        $code = $this->getCurrentRequest()->get('code');
        if (!$code) {
            throw new MissingAuthorizationCodeException('No "code" parameter was found (usually this is a query parameter)!');
        }
        return $this->provider->getAccessToken('authorization_code', ['code' => $code]);
    }

Usage Example

 /**
  * @expectedException \KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException
  */
 public function testGetAccessTokenThrowsMissingAuthCodeException()
 {
     $this->request->query->set('state', 'ACTUAL_STATE');
     $this->session->get(OAuth2Client::OAUTH2_SESSION_STATE_KEY)->willReturn('ACTUAL_STATE');
     // don't set a code query parameter
     $client = new OAuth2Client($this->provider->reveal(), $this->requestStack);
     $client->getAccessToken();
 }
All Usage Examples Of KnpU\OAuth2ClientBundle\Client\OAuth2Client::getAccessToken