如何使用Spring Social在您自己的帐户上以编程方式发布

时间:2013-02-14 00:02:16

标签: oauth twitter spring-social

我正在尝试使用Spring Social以编程方式发推文 - 我创建了一个Twitter应用程序,我为它生成了一个OAuth访问令牌,但是我无法以这样的方式配置Spring Social本身它实际上能够鸣叫。 我的配置是这样的:

ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
        registry.addConnectionFactory(new TwitterConnectionFactory(CONSUMER_KEY, CONSUMER_SECRET));
JdbcUsersConnectionRepository jdbcUsersConnectionRepository = new JdbcUsersConnectionRepository(dataSource, registry, Encryptors.noOpText());
ConnectionRepository connectionRepository = jdbcUsersConnectionRepository.createConnectionRepository("ServerFaultBest");
Twitter twitterApi = connectionRepository.getPrimaryConnection(Twitter.class).getApi();

但由于首先没有连接,我得到了:

Caused by: org.springframework.social.connect.NotConnectedException: Not connected to provider 'twitter'
at org.springframework.social.connect.jdbc.JdbcConnectionRepository.getPrimaryConnection(JdbcConnectionRepository.java:151) ~[spring-social-core-1.0.2.RELEASE.jar:na]

我甚至尝试手动配置TwitterTemplate:

TwitterTemplate twitterTemplate = new TwitterTemplate(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
    twitterTemplate.timelineOperations().updateStatus("First programatic tweet with Spring Social");

但我得到了:

java.lang.IllegalStateException: Connection not obtained from this manager
at org.apache.http.util.Asserts.check(Asserts.java:34)
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.releaseConnection(ThreadSafeClientConnManager.java:252)
at org.apache.http.impl.conn.AbstractClientConnAdapter.abortConnection(AbstractClientConnAdapter.java:338)
at org.apache.http.impl.client.DefaultRequestDirector.abortConnection(DefaultRequestDirector.java:1119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:590)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:858)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:76)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:97)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:58)
at org.springframework.social.support.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:81)
at org.springframework.social.support.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest.java:91)
at org.springframework.social.oauth1.OAuth1RequestInterceptor.intercept(OAuth1RequestInterceptor.java:48)
at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest.java:81)
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:67)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:483)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:309)
at org.springframework.social.twitter.api.impl.TimelineTemplate.updateStatus(TimelineTemplate.java:138)
at org.springframework.social.twitter.api.impl.TimelineTemplate.updateStatus(TimelineTemplate.java:126)

我猜它必须是一个我缺少的简单配置步骤 - 例如我可以通过curl命令发推文就好了。 知道这个配置可能会缺少什么吗?

感谢。
尤金。

1 个答案:

答案 0 :(得分:0)

找到答案 - 我使用的是Apache HttpClient库的测试版,我遇到了这个错误: https://issues.apache.org/jira/browse/HTTPCLIENT-1313

使用最新的GA版本(4.2.3),我可以使用TwitterTemplate在我自己的帐户上发推文。