0
votes

I'm using Spring Social Facebook plugin; I have a very simple scenario: I want to display, on my web page, my user account Feeds; so far I have built a very simple JUnit test in order to list my FB wall posts but I'm facing some issues with Spring Social integration I'm using spring 4.0.5 and spring social 1.1.0

From what I saw in the documentation, I don't need to use the spring social connect framework (maybe I'll use the ConnectionRepository in future in order to not always do a FB and/or twitter connection on every request, but now I don't need to handle several users)

So, after I created a test APP on FB and obtained the appClientID and appClientSecret, I wrote this simple XML configuration:

<?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.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.appKey}" />
                    <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>

And I wrote this simple java class (I'm behind a proxy but i can reach FB....my network administrator granted me to reach FB):

public class SocialNetworkSvcImpl implements SocialNetworkingSvc
{
    private static final Log logger = LogFactory.getLog(SocialNetworkSvcImpl.class.getName());
    @Autowired
    private Environment env;
    @Value("${facebook.appId}")
    private String facebookAppId;
    @Value("${facebook.appSecret}")
    private String facebookAppSecret;
    @Value("${facebook.clientToken}")
    private String facebookClientToken;
    @Value("${facebook.appNamespace}")
    private String facebookAppNamespace;

    private ClientHttpRequestFactory requestFactory;
    @Autowired
    private ConnectionFactoryRegistry cfr;


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

        try
        {

            FacebookConnectionFactory fcc = (FacebookConnectionFactory)cfr.getConnectionFactory("facebook");
            Facebook facebook = fcc.createConnection(fcc.getOAuthOperations().authenticateClient()).getApi();

            if( proxyEnabled )
            {
                if( requestFactory != null )
                {

                    ((FacebookTemplate)facebook).setRequestFactory(requestFactory);
                }
                else
                {
                    throw new IllegalArgumentException("Impossibile settare la requestFactory; proxy abilitato ma requestFactory null");
                }
            }
            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;
        }
    }
    @PostConstruct
    public void initialize()
    {
        if( proxyEnabled )
        {
            if( !StringUtils.hasText(proxyHost) )
            {
                throw new IllegalArgumentException("Impossibile proseguire; valore proxyHost non ammesso: "+proxyHost);
            }
            if( proxyPort <= 0 )
            {
                throw new IllegalArgumentException("Impossibile proseguire; valore proxyPort non ammesso: "+proxyPort);
            }
            if( !StringUtils.hasText(basicHeaderValue) )
            {
                throw new IllegalArgumentException("Impossibile proseguire; valore basicHeaderValue non ammesso: "+basicHeaderValue);
            }
            String userName = null;
            String password = null;
            if( !StringUtils.hasText(proxyAuthUsername) )
            {
                if( logger.isInfoEnabled() )
                {
                    logger.info("Nessun valore per proxyAuthUsername; controllo se siamo nel profilo sviluppo e se è presente come paramteri della JVM");
                }
                boolean svil = false;
                String[] profili = env.getActiveProfiles();
                for (int i = 0; i < profili.length; i++)
                {
                    if( profili[i].equalsIgnoreCase("svil") )
                    {
                        svil = true;
                        break;
                    }
                }
                if( svil )
                {
                    if(System.getProperty("usernameProxy") != null)
                    {
                        userName = System.getProperty("usernameProxy");
                    }
                }
            }
            else
            {
                if( logger.isInfoEnabled() )
                {
                    logger.info("Valorizzo la username del proxy dal file di properties");
                }
                userName = proxyAuthUsername;
            }
            if( !StringUtils.hasText(proxyAuthPassword) )
            {
                if( logger.isInfoEnabled() )
                {
                    logger.info("Nessun valore per proxyAuthPassword; controllo se siamo nel profilo sviluppo e se è presente come paramteri della JVM");
                }
                boolean svil = false;
                String[] profili = env.getActiveProfiles();
                for (int i = 0; i < profili.length; i++)
                {
                    if( profili[i].equalsIgnoreCase("svil") )
                    {
                        svil = true;
                        break;
                    }
                }
                if( svil )
                {
                    if(System.getProperty("passwordProxy") != null)
                    {
                        password = System.getProperty("passwordProxy");
                    }
                }
            }
            else
            {
                if( logger.isInfoEnabled() )
                {
                    logger.info("Valorizzo la password del proxy dal file di properties");
                }
                password = proxyAuthPassword;
            }
            if( (StringUtils.hasText(userName) && !StringUtils.hasText(password)) || (!StringUtils.hasText(userName) && StringUtils.hasText(password)) )
            {
                throw new IllegalArgumentException("Impossibile proseguire; settata solo "+(StringUtils.hasText(userName)?" la username ":" la password ")+" per accesso al proxy");
            }
            try
            {
                requestFactory = new HttpComponentsClientHttpRequestFactory();
                HttpHost proxy = new HttpHost(proxyHost, proxyPort);

                CredentialsProvider credsProvider = null;
                if( (StringUtils.hasText(userName) && StringUtils.hasText(password)) )
                {
                    credsProvider = new BasicCredentialsProvider();
                    credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(userName, password));
                }

                HttpClientBuilder builder = HttpClients.custom();
                if( credsProvider != null )
                {
                    builder.setDefaultCredentialsProvider(credsProvider);
                }
                builder.setProxy(proxy);
                CloseableHttpClient httpClient = builder.build();
                ((HttpComponentsClientHttpRequestFactory)requestFactory).setHttpClient(httpClient);

            }
            catch (Exception e)
            {
                String message = "Errore nell'inizializzazione del servizio di connessione a social network: "+e.getMessage();
                logger.fatal(message, e);
                throw new IllegalStateException(message);
            }
        }
        else
        {
            if( logger.isInfoEnabled() )
            {
                logger.info("Proxy non abilitato; ci si collega direttamente ai social network"); 
            }
        }
    }
}

But i'm having an error. This is my log:

2014-07-30 09:47:04,717 DEBUG [org.springframework.web.client.RestTemplate] Created POST request for "https://graph.facebook.com/oauth/access_token"
2014-07-30 09:47:04,718 DEBUG [org.springframework.web.client.RestTemplate] Setting request Accept header to [application/x-www-form-urlencoded, multipart/form-data]
2014-07-30 09:47:04,718 DEBUG [org.springframework.web.client.RestTemplate] Writing [{client_id=[${facebook.appKey}], client_secret=[e927afa9c6b3fb7e0e9344e38a5df46b], grant_type=[client_credentials]}] using [org.springframework.social.facebook.connect.FacebookOAuth2Template$1@f653852]
2014-07-30 09:47:04,761 DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: best-match
2014-07-30 09:47:04,797 DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
2014-07-30 09:47:04,800 DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection request: [route: {s}->https://graph.facebook.com:443][total kept alive: 0; route allocated: 0 of 5; total allocated: 0 of 10]
2014-07-30 09:47:04,862 DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection leased: [id: 0][route: {s}->https://graph.facebook.com:443][total kept alive: 0; route allocated: 1 of 5; total allocated: 1 of 10]
2014-07-30 09:47:04,867 DEBUG [org.apache.http.impl.execchain.MainClientExec] Opening connection {s}->https://graph.facebook.com:443
2014-07-30 09:47:04,890 DEBUG [org.apache.http.impl.conn.HttpClientConnectionOperator] Connecting to graph.facebook.com/173.252.100.27:443
2014-07-30 09:47:04,892 DEBUG [org.apache.http.impl.conn.HttpClientConnectionOperator] Connect to graph.facebook.com/173.252.100.27:443 timed out. Connection will be retried using another IP address
2014-07-30 09:47:04,893 DEBUG [org.apache.http.impl.conn.HttpClientConnectionOperator] Connecting to graph.facebook.com/2a03:2880:2110:cf07:face:b00c:0:1:443
2014-07-30 09:47:04,896 DEBUG [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] http-outgoing-0: Shutdown connection
2014-07-30 09:47:04,896 DEBUG [org.apache.http.impl.execchain.MainClientExec] Connection discarded
2014-07-30 09:47:04,897 DEBUG [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] http-outgoing-0: Close connection
2014-07-30 09:47:04,897 DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection released: [id: 0][route: {s}->https://graph.facebook.com:443][total kept alive: 0; route allocated: 0 of 5; total allocated: 0 of 10]
2014-07-30 09:47:04,901 INFO  [org.apache.http.impl.execchain.RetryExec] I/O exception (java.net.SocketException) caught when processing request to {s}->https://graph.facebook.com:443: Network is unreachable
2014-07-30 09:47:04,902 DEBUG [org.apache.http.impl.execchain.RetryExec] Network is unreachable
java.net.SocketException: Network is unreachable
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:239)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:545)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:334)
    at org.springframework.social.facebook.connect.FacebookOAuth2Template.postForAccessGrant(FacebookOAuth2Template.java:58)
    at org.springframework.social.oauth2.OAuth2Template.authenticateClient(OAuth2Template.java:194)
    at org.springframework.social.oauth2.OAuth2Template.authenticateClient(OAuth2Template.java:181)
    at it.spring.service.impl.SocialNetworkSvcImpl.getPosts(SocialNetworkSvcImpl.java:129)
    at it.eng.comi.test.ComiSocialTests.testFeeds(ComiSocialTests.java:54)
    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)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
2014-07-30 09:47:04,914 INFO  [org.apache.http.impl.execchain.RetryExec] Retrying request to {s}->https://graph.facebook.com:443

...

2014-07-30 09:47:11,128 ERROR [it.spring.service.impl.SocialNetworkSvcImpl] Errore nel recupero feed: I/O error on POST request for "https://graph.facebook.com/oauth/access_token":Network is unreachable; nested exception is java.net.SocketException: Network is unreachable
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://graph.facebook.com/oauth/access_token":Network is unreachable; nested exception is java.net.SocketException: Network is unreachable
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:561)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:334)
    at org.springframework.social.facebook.connect.FacebookOAuth2Template.postForAccessGrant(FacebookOAuth2Template.java:58)
    at org.springframework.social.oauth2.OAuth2Template.authenticateClient(OAuth2Template.java:194)
    at org.springframework.social.oauth2.OAuth2Template.authenticateClient(OAuth2Template.java:181)
    at it.spring.service.impl.SocialNetworkSvcImpl.getPosts(SocialNetworkSvcImpl.java:129)
    at it.eng.comi.test.ComiSocialTests.testFeeds(ComiSocialTests.java:54)
    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)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.SocketException: Network is unreachable
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:239)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:545)
    ... 35 more
2014-07-30 09:47:11,130 ERROR [it.eng.comi.test.ComiSocialTests] I/O error on POST request for "https://graph.facebook.com/oauth/access_token":Network is unreachable; nested exception is java.net.SocketException: Network is unreachable
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://graph.facebook.com/oauth/access_token":Network is unreachable; nested exception is java.net.SocketException: Network is unreachable
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:561)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:334)
    at org.springframework.social.facebook.connect.FacebookOAuth2Template.postForAccessGrant(FacebookOAuth2Template.java:58)
    at org.springframework.social.oauth2.OAuth2Template.authenticateClient(OAuth2Template.java:194)
    at org.springframework.social.oauth2.OAuth2Template.authenticateClient(OAuth2Template.java:181)
    at it.spring.service.impl.SocialNetworkSvcImpl.getPosts(SocialNetworkSvcImpl.java:129)
    at it.eng.comi.test.ComiSocialTests.testFeeds(ComiSocialTests.java:54)
    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)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.SocketException: Network is unreachable
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:239)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:545)
    ... 35 more

Instead if I generate an APP Token on FB and directly use it in FacebookTemplate, all works pretty good. How can I solve this issue? Am I wrong with anything? Any tip is really really appreciated

Thank you Angelo

1

1 Answers

0
votes

There are a couple of things wrong that I see right away. The primary problem you're having is a network problem--not a code problem or a Spring Social problem. Even if your network admin says you can see Facebook, the exception you show tells me otherwise. That's something you'll need to work out on your end.

Once you get past that, I doubt this will work anyway. You obtain your access token via authenticateClient(), which only grants you a client token. But then you try to use it for user-specific requests, such as obtaining the home feed. You must have a user token to do that operation and you can only get a user token via user authorization, aka the "OAuth Dance". Spring Social's ConnectController can help you with that.