Twitter模板在发送直接消息时失败,但get rate limit请求有效

Twitter模板在发送直接消息时失败,但get rate limit请求有效,twitter,spring-social-twitter,Twitter,Spring Social Twitter,为什么不发送直接消息 SpringSocialTwitter:1.1.2.RELEASE不适用于SpringWeb:5.0.6.RELEASE 主类示例: package com.test; import org.springframework.social.twitter.api.ResourceFamily; import org.springframework.social.twitter.api.impl.TwitterTemplate; public class Main {

为什么不发送直接消息

SpringSocialTwitter:1.1.2.RELEASE不适用于SpringWeb:5.0.6.RELEASE

主类示例:

package com.test;
import org.springframework.social.twitter.api.ResourceFamily;
import org.springframework.social.twitter.api.impl.TwitterTemplate;

public class Main {
    public static void main(String[] args) {
        final TwitterTemplate template = new TwitterTemplate("TEST", "TEST","TEST", "TEST");
        template.userOperations().getRateLimitStatus(ResourceFamily.DIRECT_MESSAGES);
        System.out.println("Successfuly obtained rate limits");
        System.out.println("Sending DM");
        template.directMessageOperations().sendDirectMessage("user_id", "text");
     }
}
来自gradle生成文件的依赖项:

dependencies {
    compile "org.springframework:spring-web:5.0.6.RELEASE"
    compile "org.springframework.social:spring-social-twitter:1.1.2.RELEASE"
    compile "com.fasterxml.jackson.core:jackson-databind:2.9.5"
}
执行结果:

Successfuly obtained rate limits
Sending DM
Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://api.twitter.com/1.1/direct_messages/new.json": cannot retry due to server authentication, in streaming mode; nested exception is java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:732)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:698)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:457)
    at org.springframework.social.twitter.api.impl.DirectMessageTemplate.sendDirectMessage(DirectMessageTemplate.java:77)
    at com.test.Main.main(Main.java:13)
Caused by: java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1692)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:55)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:51)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:754)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724)
    ... 4 more

您没有提供回调url。 转到您的twitter应用程序设置页面:


并填写回调Url输入字段。这将解决您的I/O问题。

Calback URL已提供。它不适用于最新版本的SpringWeb,但适用于以前版本的SpringWeb。@AndriyHospodarysko您找到过解决方案吗?当我尝试发送tweet时,也会出现同样的错误。stacktrace说它来自spring-web-5.0.6。@Elias我已经将spring-web的版本减少到5.0.4,它解决了这个问题。