android twitter4j oauth 在 3g 上不起作用

发布于 2025-01-03 17:37:07 字数 105 浏览 1 评论 0原文

我使用 twitter4j 创建了一个 Android 应用程序。

在 3g 上时,所有 api 调用都无法进行身份验证,但在 wi-fi 上时却可以正常工作。 可能是什么问题?

I've created an android app using twitter4j.

None of the api calls can authenticate when on 3g, but work perfectly when on wi-fi.
What could be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-01-10 17:37:07

我对此进行了广泛的测试,可以确认这只是 Android 4.0 (ICS) 设备上的问题。为什么,我还不确定,尽管 HttpURLConnection 的某些部分在 4.0 中发生了更改,因此其中一些更改使 twitter4j 不兼容。

我想可能是这样的:
Android 4.0 ICS 将 HttpURLConnection GET 请求转换为 POST 请求

但在使用 tcpdump 分析请求后,似乎它实际上正在进行有效的 GET,但由于某种原因,twitter api 仍然说这是一个坏的 。

GET /1/users/show.json?include_entities=true&user_id=6842472 HTTP/1.1
Authorization: OAuth oauth_consumer_key="<redacted>",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1331150787",oauth_nonce="2157367237",oauth_version="1.0",oauth_token="<redacted>",oauth_signature="<redacted>"
Accept-Encoding: gzip
X-Twitter-Client-URL: http://twitter4j.org/en/twitter4j-2.2.5.xml
User-Agent: twitter4j http://twitter4j.org/ /2.2.5  
X-Twitter-Client-Version: 2.2.5
X-Twitter-Client: Twitter4J   
Host: api.twitter.com  
Connection: Keep-Alive


HTTP/1.1 400 Bad Request
Date: Wed, 07 Mar 2012 20:06:30 GMT    
Status: 400 Bad Request   
X-RateLimit-Class: api
X-RateLimit-Reset: 1331153824 
X-Revision: DEV 
Last-Modified: Wed, 07 Mar 2012 20:06:30 GMT  
X-Frame-Options: SAMEORIGIN 
X-Transaction: a0bf805e990a5c79
Content-Type: application/json; charset=utf-8 
X-RateLimit-Remaining: 0 
X-RateLimit-Limit: 150
Pragma: no-cache
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
X-Runtime: 0.00742
X-MID: 2295d07237fcb4763f7f54d2bf46e27dd40e022f
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Vary: Accept-Encoding
Server: tfe
Transfer-Encoding: chunked
Connection: close
Content-Encoding: gzip
Set-Cookie: 
...

除非有人能在 ICS/HttpURLConnection 中找到破坏此问题的内容,否则我能看到的唯一解决方法是为 twitter4j 编写一个使用 HttpClient 而不是 HttpURLConnection 的备用后端,这正是我打算做的

编辑:所以我为 twitter4j 编写了一个使用 apache httpclient 的客户端后端,它遇到了完全相同的问题!又过了一些不眠之夜,wireshark 给我带来了以下非常方便的解决方案,它确实有效:

像这样初始化你的 twitter4j 实例:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(Const.CONSUMER_KEY);
configurationBuilder.setOAuthConsumerSecret(Const.CONSUMER_SECRET);
configurationBuilder.setUseSSL(true);
Configuration configuration = configurationBuilder.build();
twitter = new TwitterFactory(configuration).getInstance();

setUseSSL(true) 是一条神奇的线。

I've tested this extensively and can confirm this is only a problem on Android 4.0 (ICS) devices. Why, I'm unsure yet, though some parts of HttpURLConnection have changed in 4.0, so some of these changes have made twitter4j incompatible.

I thought it may have been this:
Android 4.0 ICS turning HttpURLConnection GET requests into POST requests

but after analysing the requests with tcpdump it seems like it is in fact making a valid GET, but for some reason, the twitter api still says it's a bad request..

GET /1/users/show.json?include_entities=true&user_id=6842472 HTTP/1.1
Authorization: OAuth oauth_consumer_key="<redacted>",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1331150787",oauth_nonce="2157367237",oauth_version="1.0",oauth_token="<redacted>",oauth_signature="<redacted>"
Accept-Encoding: gzip
X-Twitter-Client-URL: http://twitter4j.org/en/twitter4j-2.2.5.xml
User-Agent: twitter4j http://twitter4j.org/ /2.2.5  
X-Twitter-Client-Version: 2.2.5
X-Twitter-Client: Twitter4J   
Host: api.twitter.com  
Connection: Keep-Alive


HTTP/1.1 400 Bad Request
Date: Wed, 07 Mar 2012 20:06:30 GMT    
Status: 400 Bad Request   
X-RateLimit-Class: api
X-RateLimit-Reset: 1331153824 
X-Revision: DEV 
Last-Modified: Wed, 07 Mar 2012 20:06:30 GMT  
X-Frame-Options: SAMEORIGIN 
X-Transaction: a0bf805e990a5c79
Content-Type: application/json; charset=utf-8 
X-RateLimit-Remaining: 0 
X-RateLimit-Limit: 150
Pragma: no-cache
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
X-Runtime: 0.00742
X-MID: 2295d07237fcb4763f7f54d2bf46e27dd40e022f
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Vary: Accept-Encoding
Server: tfe
Transfer-Encoding: chunked
Connection: close
Content-Encoding: gzip
Set-Cookie: 
...

Unless someone can find what it is in ICS/HttpURLConnection that is breaking this the only workaround I can see is to write an alternate backend for twitter4j that uses HttpClient instead of HttpURLConnection, which is exactly what I plan to do.

EDIT: So I wrote the a client backend for twitter4j that uses the apache httpclient and it suffers from the exact same problem!! Some more sleepless hours and wireshark has brought me to the following very handy solution, that actually works:

Initialise your twitter4j instance like this:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(Const.CONSUMER_KEY);
configurationBuilder.setOAuthConsumerSecret(Const.CONSUMER_SECRET);
configurationBuilder.setUseSSL(true);
Configuration configuration = configurationBuilder.build();
twitter = new TwitterFactory(configuration).getInstance();

The setUseSSL(true) is the magic line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文