2013-08-08 14 views
4

액세스 토큰을 생성합니다. 웹 사이트에서는 FOSUserBundleFOSFacebookBundle을 사용합니다.FOSOAuthServerBundle - 나는 <strong>symfony2 웹 사이트</strong>와 iOS 앱을 통해 FOSOAuthServerBundle 접근</strong>를 사용 <strong>OAuth를 통해 보안 웹 서비스를 수동으로

내가 놓칠 수있는 유일한 이유는 사용자가 iOS 앱에서 페이스 북을 사용하여 로그인하고 다른 사용자처럼 내 api에 액세스 할 수 있도록 사용자 계정에 연결된 oauth에 대한 access_token을 반환 할 수있는 가능성을 제공하는 것입니다.

그래서 기본적으로 나는 사용자가 (토큰이 내 응용 프로그램과 일치) 올바른지 확인하고 인증 토큰을 반환 , 내 웹 서비스에 사용자 facebookID 및 facebook_access_token을 보내 로합니다.

질문 : FOSOAuthServerBundle에 "Facebook"grant_type을 추가하는 쉬운 방법은 무엇입니까?

  • Design for Facebook authentication in an iOS app that also accesses a secured web service

  • Get application id from user access token (or verify the source application for a token)

    • 그러나, 그들은 FOSOauthServerBundle를 사용하지 않는 것 방법을 설명하지 않습니다

      나는 어떤 사람들이 보는이 질문을했을 알고 질문은 꽤 오래되었습니다.

      내가이 번들 사용하여 시도했다 : https://github.com/TheFootballSocialClub/FSCOAuth2FacebookGrantBundle

      을하지만,이 번들이 내 앞에에만 9 번 다운로드 및 내 응용 프로그램에 완벽하게 적합하지 않다되었습니다 (이 페이스 북 사용자 이름은 예를 들어 자신의 facebookId 동일하다고 판단) . 그래서 내가하고 싶은 일은 내 자신의 방식으로 같은 종류의 것을 다시 구현하는 것입니다.

      누군가가 이미 수행 한 경우 Google에서 안내를 제공 할 수 있다면 대단히 감사하겠습니다. 당신은 내 FacebookGrantExtension을 찾을 수 있습니다

      https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/adding_grant_extensions.md는 FB access_token은에서 토큰을 얻을 :

      당신이 부여 확장을 추가해야합니다, 당신은

    답변

    2

    이를 위해 감사, 공식 문서 "그랜트 확장 추가"를 참조하십시오 :

    class FacebookGrantExtension implements GrantExtensionInterface 
    { 
    protected $userManager = null; 
    protected $facebookSdk = null; 
    
    public function __construct(UserManager $userManager, \BaseFacebook $facebookSdk) 
    { 
        $this->userManager = $userManager; 
        $this->facebookSdk = $facebookSdk; 
    } 
    
    /** 
    * @see OAuth2\IOAuth2GrantExtension::checkGrantExtension 
    */ 
    public function checkGrantExtension(IOAuth2Client $client, array $inputData, array $authHeaders) 
    { 
        if (!isset($inputData['facebook_access_token'])) { 
         return false; 
        } 
    
        $this->facebookSdk->setAccessToken($inputData['facebook_access_token']); 
        try { 
         // Try to get the user with the facebook token from Open Graph 
         $fbData = $this->facebookSdk->api('/me'); 
    
         if (empty($fbData) || !isset($fbData['id'])) { 
          return false; 
         } 
    
         // Check if a user match in database with the facebook id 
         $user = $this->userManager->findUserBy(array(
          'facebookId' => $fbData['id'] 
         )); 
    
         // If no user found, register a new user and grant token 
         if (null === $user) { 
          return false; 
         } 
         // Else, return the access_token for the user    
         else { 
          return array(
           'data' => $user 
          ); 
         } 
        } catch(\FacebookApiExceptionion $e) { 
         return false; 
        } 
    } 
    } 
    

    그리고 config.yml :

    my.oauth.facebook_extension: 
        class: My\CoreBundle\Oauth\FacebookGrantExtension 
        arguments: 
         userManager: "@fos_user.user_manager" 
         facebookSdk: "@fos_facebook.api" 
        tags: 
         - { name: fos_oauth_server.grant_extension, uri: 'http://grants.api.mywebsite.com/facebook_access_token' } 
    
    +0

    Ouha I 전에 대답을 못 봤어. 내가 로그인 한 페이스 북 토큰을 얻거나 사용자를 수동으로 등록하고 사용자와 모든 속성에 대해 자동으로 생성 된 access_token을 만들면 사용자가 Facebook에 로그인 할 때 컨트롤러에서 수동으로 수행했습니다. . 그런 다음 보조금 확장을 사용하면 얻을 수있는 이점은 무엇입니까? 감사합니다. –

    +0

    보조금 확장을 사용하면이 방법이 "최선의 방법"이지만 조금 어렵고 적응력이 떨어지더라도 솔루션이 작동합니다. – PierrickM

    +0

    감사합니다! 그래서 나는 어쨌든 2 api 요청을 피할 수 없다는 것을 이해한다. (하나는 페이스 북 용이고 다른 하나는 내 토큰 용이다.) –