春季社交Facebook问题

时间:2014-10-01 09:42:10

标签: spring spring-social spring-social-facebook

我正在尝试使用Spring Social Facebook来阅读我的应用程序(目前是一个简单的Java类测试)

但我遇到了一些问题。我创建了一个非常简单的项目(面向maven),我正在使用Spring social 1.1.0

我创建了以下Java类:

import it.eng.comi.spring.service.CoMiSocialNetworkingSvc;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.social.connect.support.ConnectionFactoryRegistry;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.OAuth2Operations;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class CoMiSocialNetworkSvcImpl implements CoMiSocialNetworkingSvc
{
    private static final Log logger = LogFactory.getLog(CoMiSocialNetworkSvcImpl.class.getName());
    @Value("${facebook.appId}")
    private String facebookAppId;
    @Value("${facebook.appSecret}")
    private String facebookAppSecret;
    @Value("${facebook.appNamespace}")
    private String facebookAppNamespace;
    @Autowired
    private ConnectionFactoryRegistry cfr;

    @Override
    public PagedList<Post> getPosts() throws Exception
    {

        try
        {

            FacebookConnectionFactory fcc = (FacebookConnectionFactory)cfr.getConnectionFactory("facebook");
            OAuth2Operations oauthOper = fcc.getOAuthOperations();
            String scope = "publish_stream,offline_access,read_stream,user_about_me,manage_pages";
            AccessGrant ag = oauthOper.authenticateClient(scope);
            String accessToken = ag.getAccessToken();
            logger.info("Access Token: "+accessToken);
            Facebook facebook = new FacebookTemplate(accessToken);
            PagedList<Post> result = facebook.feedOperations().getHomeFeed();
            result.addAll(facebook.feedOperations().getFeed());
            return result;
        }
        catch (Exception e)
        {
            String messagge = "Errore nel recupero feed: "+e.getMessage();
            logger.fatal(messagge, e);
            throw e;
        }
    }
}

我创建了这个简单的JUnit测试:

import java.util.List;
import it.eng.comi.spring.service.CoMiSocialNetworkingSvc;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@ContextConfiguration(value={
        "classpath:application-context-social.xml"
})

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ComiSocialTests
{
    private static final Log logger = LogFactory.getLog(ComiSocialTests.class.getName());
    @Autowired
    private CoMiSocialNetworkingSvc social;
    @Test
    public void testFeeds()
    {
        try
        {
            PagedList<Post> feeds = social.getPosts();
            for (Post post : feeds)
            {
                logger.info("Caption "+post.getCaption()+" like counts "+post.getLikeCount()+" comments count: "+post.getCommentCount()+" descrizione "+post.getDescription());
            }
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
    }   
}

这是我的应用程序上下文XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:facebook="http://www.springframework.org/schema/social/facebook"
    xmlns:twitter="http://www.springframework.org/schema/social/twitter"
    xmlns:social="http://www.springframework.org/schema/social"
    xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
        http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
        http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:property-placeholder location="classpath:configuration.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
    <context:component-scan base-package="it.eng.comi.spring" />
    <bean id="connectionFactoryLocator"
        class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
        <property name="connectionFactories">
            <list>
                <bean
                    class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                    <constructor-arg value="${facebook.appId}" />
                    <constructor-arg value="${facebook.appSecret}" />
                </bean>
                <bean
                    class="org.springframework.social.twitter.connect.TwitterConnectionFactory">
                    <constructor-arg value="${twitter.appKey}" />
                    <constructor-arg value="${twitter.appSecret}" />
                </bean>
            </list>
        </property>
    </bean>
</beans>

当我尝试用显示的实现(显示类的方法getPosts())联系FB时,我收到此错误:

11:35:39,501 INFO  [CoMiSocialNetworkSvcImpl] Access Token: 868557579830099|4JH01Jwud-Ot0UTd_FoF_439r50
11:35:45,149 WARN  [RestTemplate] GET request for "https://graph.facebook.com/me/home?limit=25" resulted in 400 (Bad Request); invoking error handler
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
11:35:50,625 FATAL [CoMiSocialNetworkSvcImpl] Errore nel recupero feed: Authorization is required for the operation, but the API binding was created without authorization.
org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:89)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:65)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:588)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:546)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:517)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:255)
    at org.springframework.social.facebook.api.impl.FeedTemplate.fetchConnectionList(FeedTemplate.java:367)
    at org.springframework.social.facebook.api.impl.FeedTemplate.getHomeFeed(FeedTemplate.java:106)
    at org.springframework.social.facebook.api.impl.FeedTemplate.getHomeFeed(FeedTemplate.java:95)
    at it.eng.comi.spring.service.impl.CoMiSocialNetworkSvcImpl.getPosts(CoMiSocialNetworkSvcImpl.java:146)
    at it.eng.comi.test.ComiSocialTests.testFeeds(ComiSocialTests.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

有谁能建议我如何解决这个问题?我几天都坚持这个,我无法解决它

谢谢你 安吉洛

0 个答案:

没有答案
相关问题