我正在尝试使用 Spring social 建立 Twitter 连接。为此,我按照文档编写了此文件:
@Configuration
public class SocialConfig {
@Value("${facebook.clientId}")
private String facebookClientId;
@Value("${facebook.clientSecret}")
private String facebookClientSecret;
@Value("${twitter.consumerKey}")
private String twitterConsumerKey;
@Value("${twitter.consumerSecret}")
private String twitterConsumerSecret;
@Inject
private DataSource dataSource;
/**
* When a new provider is added to the app, register its
* {@link ConnectionFactory} here.
*
* @see FacebookConnectionFactory
*/
@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory(
facebookClientId, facebookClientSecret));
registry.addConnectionFactory(new TwitterConnectionFactory(
twitterConsumerKey, twitterConsumerSecret));
return registry;
}
/**
* Singleton data access object providing access to connections across all
* users.
*/
@Bean
public UsersConnectionRepository usersConnectionRepository() {
JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(
dataSource, connectionFactoryLocator(), Encryptors.noOpText());
repository.setConnectionSignUp(new SimpleConnectionSignUp());
return repository;
}
/**
* Request-scoped data access object providing access to the current user's
* connections.
*/
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
User user = SecurityContext.getCurrentUser();
return usersConnectionRepository().createConnectionRepository(
user.getId());
}
@Bean
public ConnectController connectController(){
return new ConnectController(connectionFactoryLocator(), connectionRepository());
}
/**
* A proxy to a request-scoped object representing the current user's
* primary Facebook account.
*
* @throws NotConnectedException
* if the user is not connected to facebook.
*/
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook() {
return connectionRepository().getPrimaryConnection(Facebook.class)
.getApi();
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Twitter twitter() {
Connection<Twitter> connection = connectionRepository()
.findPrimaryConnection(Twitter.class);
Twitter twitter = connection != null ? connection.getApi()
: new TwitterTemplate();
return twitter;
}
}
这段代码在 facebook 上运行良好,但在 twitter 上给我提供了一个 provider=error。所有键都是正确的(Twitter 键在 Twitter4j 上工作正常)。当我尝试建立连接时,应用程序将我发送到 twitter,然后将地址中的“error=provider”发回给我。