我试图通过我的Quartz Job调用facebook API来自动获取朋友,帖子或新馈送......但我无法通过Job运行。我认为我在SocialConfig上有错。 当我从控制器调用facebookService时,它正常工作。 你能帮我解决这个问题吗?非常感谢。
SocialConfig
public class SocialConfig {
@Value("${facebook.clientId}")
private String facebookClientId;
@Value("${facebook.clientSecret}")
private String facebookClientSecret;
@Inject
private DataSource dataSource;
@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory(facebookClientId, facebookClientSecret));
return registry;
}
@Bean
public UsersConnectionRepository usersConnectionRepository() {
JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
connectionFactoryLocator(), Encryptors.noOpText());
repository.setConnectionSignUp(new SimpleConnectionSignUp());
return repository;
}
@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.NO)
public ConnectionRepository connectionRepository() {
User user = SecurityContext.getCurrentUser();
return usersConnectionRepository().createConnectionRepository(user.getId());
}
@Bean
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public Facebook facebook() {
return connectionRepository().getPrimaryConnection(Facebook.class).getApi();
}
获取错误
org.springframework.beans.factory.BeanCreationException:创建名称为' scopedTarget.facebook':Scope' session'当前线程无效; ;考虑为这个bean定义一个范围代理,如果你想从一个单例引用它;嵌套异常是java.lang.IllegalStateException:找不到线程绑定请求:您是指实际Web请求之外的请求属性,还是处理最初接收线程之外的请求?如果您实际在Web请求中操作并仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter公开当前请求。
我通过在web.xml中添加来尝试RequestContextListener,但它没有工作。
来自作业。
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
try {
ApplicationContextProvider appContext = new ApplicationContextProvider();
FacebookService facebookService = appContext.getApplicationContext().getBean("facebookService", FacebookService.class);
//##It shows error here##
System.out.println("!!User: "+facebookService.getFacebookFriends().get(0).getName());
//runMeTask.printMe();
System.out.println("!!*****Quartz*****!!");
} catch (Exception ex) {
System.out.println(ex);
}
}
Facebook服务
public class FacebookService {
private Facebook facebook;
@Inject
public FacebookService(Facebook facebook) {
this.facebook = facebook;
}
}
public Facebook getFacebook() {
return facebook;
}
public void setFacebook(Facebook facebook) {
this.facebook = facebook;
}
public List<Reference> getFacebookFriends() {
// Get Friend List
return getFacebook().friendOperations().getFriends();
}
答案 0 :(得分:0)
我通过关注Facebook4j
找到了另一个解决方案ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthAppId("app_id")
.setOAuthAppSecret("app_secret")
.setOAuthAccessToken("user_token")
.setOAuthPermissions("email,publish_actions,public_profile, basic_info, user_likes");
FacebookFactory ff = new FacebookFactory(cb.build());
// Generate facebook instance.
this.facebook = ff.getInstance();
这里的问题是,当我的工作运行并调用Facebook API时,它没有用户访问令牌,所以当发布消息到我的石英作业doest权限发布时。