0

我有一個問題,獲取其他參數idname使用spring-social春季社交Facebook個人資料只提供ID和名稱

依賴關係:

<!-- Spring Social --> 
    <dependency> 
     <groupId>org.springframework.social</groupId> 
     <artifactId>spring-social-config</artifactId> 
     <version>1.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.social</groupId> 
     <artifactId>spring-social-core</artifactId> 
     <version>1.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.social</groupId> 
     <artifactId>spring-social-security</artifactId> 
     <version>1.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.social</groupId> 
     <artifactId>spring-social-web</artifactId> 
     <version>1.1.2.RELEASE</version> 
    </dependency> 

    <!-- Spring Social Facebook --> 
    <dependency> 
     <groupId>org.springframework.social</groupId> 
     <artifactId>spring-social-facebook</artifactId> 
     <version>2.0.3.RELEASE</version> 
    </dependency> 

配置:

<security:http ...> 
... 
    <security:custom-filter before="PRE_AUTH_FILTER" ref="socialAuthenticationFilter" /> 
... 
</security:http> 
<security:authentication-manager alias="authenticationManager"> 
    ... 
    <security:authentication-provider ref="socialAuthenticationProvider" /> 
</security:authentication-manager> 
<bean id="socialAuthenticationProvider" class="org.springframework.social.security.SocialAuthenticationProvider"> 
    <constructor-arg ref="inMemoryUsersConnectionRepository"/> 
    <constructor-arg ref="socialUserDetailsService"/> 
</bean> 

<bean id="inMemoryUsersConnectionRepository" 
     class="org.springframework.social.connect.mem.InMemoryUsersConnectionRepository"> 
    <constructor-arg name="connectionFactoryLocator" ref="connectionFactoryLocator"/> 
    <property name="connectionSignUp" ref="connectionSignUp"/> 
</bean> 

<bean id="connectionFactoryLocator" 
     class="org.springframework.social.security.SocialAuthenticationServiceRegistry"> 
    <property name="authenticationServices"> 
     <list> 
      <ref bean="facebookAuthenticationService"/> 
     </list> 
    </property> 
</bean> 

<bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter"> 
    <constructor-arg name="authManager" ref="authenticationManager"/> 
    <constructor-arg name="userIdSource" ref="userIdSource"/> 
    <constructor-arg name="usersConnectionRepository" ref="inMemoryUsersConnectionRepository"/> 
    <constructor-arg name="authServiceLocator" ref="connectionFactoryLocator"/> 
    <property name="authenticationSuccessHandler" ref="loginGuidAuthenticationSuccessHandler"/> 
</bean> 

<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource"/> 

<bean id="facebookAuthenticationService" 
     class="org.springframework.social.facebook.security.FacebookAuthenticationService"> 
    <constructor-arg name="apiKey" value="<myKey>"/> 
    <constructor-arg name="appSecret" value="<mySecret>"/> 
    <property name="defaultScope" value="email"/> 
</bean> 

<bean id="connectionSignUp" class="my.package.DefaultConnectionSignUp"/> 

我有我自己的一些所需要的服務,我不認爲是相關的實現。當我在執行ConnectionSignUp時調試execute方法時,除nameid之外,所有用戶字段都爲空。

@Override 
public String execute(Connection<?> connection) { 
    Connection<Facebook> fbConnection = (Connection<Facebook>) connection; 
    fbConnection.getApi().userOperations().getUserProfile().getEmail(); //null 
    fbConnection.getApi().userOperations().getUserProfile().getFirstName(); //null 
    fbConnection.fetchUserProfile().getEmail(); //null 
    fbConnection.fetchObject("me", User.class); //email == null 
    ... 
} 

我的JSP形式:

<form name='facebookSocialloginForm' 
      action="<c:url value='/auth/facebook' />" method='POST'> 
     <input type="hidden" name="scope" 
      value="public_profile,email,user_about_me,user_birthday,user_likes"/> 
     ... 
    </form> 

可以訪問用戶私人postslikes雖然。在我的Facebook賬戶上,它表示該應用程序可以訪問電子郵件和其他信息。

有什麼建議嗎?如果我錯過了一些重要的內容,請提問並提供更多信息。

回答

1

facebook api中的變化可能導致了這種情況。嘗試下面的代碼:

UserOperations userOperations = facebook.userOperations(); 
      String [] fields = { "id", "email", "first_name", "last_name" }; 
      org.springframework.social.facebook.api.User profile = facebook.fetchObject 
        ("me", org.springframework.social.facebook.api.User.class, fields); 
+0

謝謝,它工作正常。添加更多的細節。看起來我在我的應用程序的兩個模塊中有兩個不同版本的'spring-social-facebook',它使用的是最老的一個。在解決了這個問題之後''getUserProfile()'方法不工作**所有**'(錯誤消息是(#12)生物字段被棄用的版本v2.8和更高版本)',因爲Facebook API v。2.8是在2.0.3.RELEASE版本中不受支持。因此,直到下一個版本我相信它是最好的(唯一的?)解決方案。 – qwerty1423

0

添加public_profile,電子郵件到您的範圍變量,同時擊中Facebook API,這將明確返回您所需的數據。

相關問題