Exemple #1
1
 public function authorize()
 {
     try {
         $this->accessToken = $this->helper->getAccessToken();
     } catch (FacebookResponseException $e) {
         // When Graph returns an error
         throw new FacebookAuthenticationException('Graph returned an error: ' . $e->getMessage());
     } catch (FacebookSDKException $e) {
         // When validation fails or other local issues
         throw new FacebookAuthenticationException('Facebook SDK returned an error: ' . $e->getMessage());
     }
     if (!$this->accessToken) {
         throw new FacebookAuthenticationException('Access token not received. ' . $this->helper->getError(), $this->helper->getErrorCode());
     }
     try {
         // Returns a `Facebook\FacebookResponse` object
         $response = $this->facebook->get('/me?fields=id,name', $this->accessToken);
     } catch (FacebookResponseException $e) {
         throw new FacebookAuthenticationException('Graph returned an error: ' . $e->getMessage());
     } catch (FacebookSDKException $e) {
         throw new FacebookAuthenticationException('Facebook SDK returned an error: ' . $e->getMessage());
     }
     $fbUser = $response->getGraphUser();
     if (!($user = $this->doctrine->getRepository('QuizBundle:User')->findOneBySocialId($fbUser['id']))) {
         $user = (new Entity\User())->setAccessToken($this->accessToken)->setSocialType(Entity\User::FACEBOOK)->setSocialId($fbUser['id'])->setName($fbUser['name']);
         $manager = $this->doctrine->getManager();
         $manager->persist($user);
         $manager->flush();
     }
     $token = new SocialToken($user, $this->accessToken, 'facebook', [$this->adminId == $fbUser['id'] ? 'ROLE_ADMIN' : 'ROLE_USER']);
     $this->tokenStorage->setToken($token);
 }
 private function getAccessToken($time = 'short')
 {
     switch ($time) {
         case 'short':
             $accessToken = $this->helper->getAccessToken();
             break;
         case 'long':
             $accessToken = $this->getClient()->getLongLivedAccessToken($this->helper->getAccessToken());
             break;
         default:
             $accessToken = null;
             break;
     }
     return $accessToken;
 }
 public function testAnAccessTokenCanBeObtainedFromRedirect()
 {
     $this->persistentDataHandler->set('state', 'foo_state');
     $_GET['state'] = 'foo_state';
     $_GET['code'] = 'foo_code';
     $response = m::mock('Facebook\\Entities\\FacebookResponse');
     $response->shouldReceive('getDecodedBody')->once()->andReturn(['access_token' => 'access_token_from_code', 'expires' => 555]);
     $client = m::mock('Facebook\\FacebookClient');
     $client->shouldReceive('sendRequest')->with(m::type('Facebook\\Entities\\FacebookRequest'))->once()->andReturn($response);
     $app = new FacebookApp('123', 'foo_app_secret');
     $helper = new FacebookRedirectLoginHelper($app, $this->persistentDataHandler);
     $accessToken = $helper->getAccessToken($client, self::REDIRECT_URL);
     $this->assertInstanceOf('Facebook\\Entities\\AccessToken', $accessToken);
     $this->assertEquals('access_token_from_code', (string) $accessToken);
 }