Spring社交获取facebook访问令牌

时间:2014-04-04 13:27:48

标签: java facebook spring spring-mvc spring-social

我试图通过春季社交从Facebook获取访问令牌。 所以我有控制器方法试图获取代码

 @RequestMapping(value = "/SocialAuth/fb", method = RequestMethod.GET)
    public ModelAndView singInFb(){
        readProperties();

        FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(fbAppId,fbAppKey);
        OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
        OAuth2Parameters params = new OAuth2Parameters();
        params.setRedirectUri(fbRedirectUrl);
        String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
        RedirectView redirectView = new RedirectView(authorizeUrl, true, true, true);
        return new ModelAndView(redirectView);
    }

工作正常。但另一种方法是,当我尝试获取访问代码时

   @RequestMapping(value = "/socialAuth/fb/answer", method = RequestMethod.GET)
    public String fbAuthResponse(@RequestParam String code){
        if(code!=null){
            FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(fbAppId, fbAppKey);
            OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
            AccessGrant accessGrant = oauthOperations.authenticateClient();
            Connection connection = connectionFactory.createConnection(accessGrant);

            UserProfile userProfile = connection.fetchUserProfile();
        }

        return null;

    } 

但我总是得到AccessGrant accessGrant = oauthOperations.authenticateClient();线。我忘了添加什么?

我的maven是

<!--spring social-->
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-config</artifactId>
            <version>1.1.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-core</artifactId>
            <version>1.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-security</artifactId>
            <version>1.1.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-web</artifactId>
            <version>1.1.0.RC1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-facebook</artifactId>
            <version>1.1.0.RC1</version>
        </dependency>

1 个答案:

答案 0 :(得分:0)

我已经使用这些属性设置了http://www.petrikainulainen.net/programming/spring-framework/adding-social-sign-in-to-a-spring-mvc-web-application-configuration/(几乎与facebook一起工作):

<properties>
    <hibernate.version>4.2.4.Final</hibernate.version>
    <liquibase.version>3.1.1</liquibase.version>
    <mysql.connector.version>5.1.26</mysql.connector.version>
    <slf4j.version>1.7.5</slf4j.version>
    <spring.version>4.0.0.RELEASE</spring.version>
    <spring.data.jpa.version>1.4.3.RELEASE</spring.data.jpa.version>
    <spring.security.version>3.2.0.RELEASE</spring.security.version>
    <spring.social.version>1.1.0.RELEASE</spring.social.version>
    <spring.social.facebook.version>1.1.0.RELEASE</spring.social.facebook.version>
    <spring.social.twitter.version>1.1.0.RELEASE</spring.social.twitter.version>

    <build.profile.id>dev</build.profile.id>
    <jdk.version>1.7</jdk.version>
    <skip.integration.tests>true</skip.integration.tests>
    <skip.unit.tests>false</skip.unit.tests>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>