android - 为什么方法 getOAuthAccessToken 总是在 twitter4j api 中触发异常?

标签 android twitter twitter-oauth twitter4j

我按照很多说明从我的应用程序中制作一条简单的推文。我已经在 Twitter 上注册了它,但我就是不能发推文。我可以登录,但不能更新我的状态。这是登录代码

private void twitterLogOn() {
        Twitter twitter = new TwitterFactory().getInstance();
        try {

            twitter.setOAuthConsumer(consumerKey, consumerSecret);
            rToken = twitter.getOAuthRequestToken(myCallBack);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(rToken.getAuthenticationURL())));
        } catch (IllegalStateException e) {
            // access token is already available, or consumer key/secret is not
            // set.
            if (!twitter.getAuthorization().isEnabled()) {
                System.out.println("OAuth consumer key/secret is not set.");
                finish();
            }
        } catch (Exception e) {
            Toast.makeText(Configuration.this,getString(R.string.networkError), Toast.LENGTH_SHORT).show();
        }
    }

这似乎工作正常,但当我在登录后返回到我的应用程序时,应该执行下一段代码,总是以异常 Toast 结束。

public void onResume() {    
        super.onResume();

        Uri uri = getIntent().getData();

        if (uri != null) {    
            oauthVerifier = uri.getQueryParameter("oauth_verifier");

            try {    
                Twitter tt = new TwitterFactory().getInstance(); // Do I need this new twitter instance?
                tt.setOAuthConsumer(consumerKey, consumerSecret);
                AccessToken at = tt.getOAuthAccessToken(rToken, oauthVerifier); // Gives the error

                       // Do tweet here ...

                } catch (Exception e) {
                    Toast.makeText(Configuration.this, "Network Host not responding",Toast.LENGTH_SHORT).show();
                }
            }
    }

那里有任何好的鹰眼可以告诉我我做错了什么吗?这是引发异常的行

AccessToken at = tt.getOAuthAccessToken(rToken, oauthVerifier);

提前致谢!

编辑

堆栈跟踪:(在某处阅读这只是隐藏了 401 错误)

07-24 12:49:31.931: WARN/System.err(18441): Received authentication challenge is nullRelevant discussions can be on the Internet at:
07-24 12:49:31.931: WARN/System.err(18441):     http://www.google.co.jp/search?q=9ddbeb3a or
07-24 12:49:31.931: WARN/System.err(18441):     http://www.google.co.jp/search?q=5c9c15a6
07-24 12:49:31.931: WARN/System.err(18441): TwitterException{exceptionCode=[9ddbeb3a-5c9c15a6 c8a7b39b-36e69ae1], statusCode=-1, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.3}
07-24 12:49:31.931: WARN/System.err(18441):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:204)
07-24 12:49:31.931: WARN/System.err(18441):     at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
07-24 12:49:31.931: WARN/System.err(18441):     at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
07-24 12:49:31.931: WARN/System.err(18441):     at twitter4j.auth.OAuthAuthorization.getOAuthAccessToken(OAuthAuthorization.java:142)
07-24 12:49:31.931: WARN/System.err(18441):     at twitter4j.auth.OAuthAuthorization.getOAuthAccessToken(OAuthAuthorization.java:160)
07-24 12:49:31.931: WARN/System.err(18441):     at twitter4j.TwitterBaseImpl.getOAuthAccessToken(TwitterBaseImpl.java:349)
07-24 12:49:31.931: WARN/System.err(18441):     at com.my.app.TwitterTweetActivity.onResume(TwitterTweetActivity.java:76)
07-24 12:49:31.931: WARN/System.err(18441):     at com.my.app.TwitterTweetActivity.onResume(TwitterTweetActivity.java:64)
07-24 12:49:31.931: WARN/System.err(18441):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
07-24 12:49:31.931: WARN/System.err(18441):     at android.app.Activity.performResume(Activity.java:3833)
07-24 12:49:31.931: WARN/System.err(18441):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2215)
07-24 12:49:31.941: WARN/System.err(18441):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2240)
07-24 12:49:31.941: WARN/System.err(18441):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1773)
07-24 12:49:31.941: WARN/System.err(18441):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-24 12:49:31.941: WARN/System.err(18441):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:936)
07-24 12:49:31.941: WARN/System.err(18441):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:49:31.941: WARN/System.err(18441):     at android.os.Looper.loop(Looper.java:123)
07-24 12:49:31.941: WARN/System.err(18441):     at android.app.ActivityThread.main(ActivityThread.java:3812)
07-24 12:49:31.941: WARN/System.err(18441):     at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:49:31.941: WARN/System.err(18441):     at java.lang.reflect.Method.invoke(Method.java:507)
07-24 12:49:31.941: WARN/System.err(18441):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-24 12:49:31.941: WARN/System.err(18441):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-24 12:49:31.941: WARN/System.err(18441):     at dalvik.system.NativeStart.main(Native Method)
07-24 12:49:31.941: WARN/System.err(18441): Caused by: java.io.IOException: Received authentication challenge is null
07-24 12:49:31.941: WARN/System.err(18441):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:1153)
07-24 12:49:31.941: WARN/System.err(18441):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:1095)
07-24 12:49:31.951: WARN/System.err(18441):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1048)
07-24 12:49:31.951: WARN/System.err(18441):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
07-24 12:49:31.951: WARN/System.err(18441):     at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:35)
07-24 12:49:31.951: WARN/System.err(18441):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:168)
07-24 12:49:31.951: WARN/System.err(18441):     ... 22 more

方法的 API:getOAuthREquestToken

最佳答案

我自己解决了,只用了 4 个小时!!

我不是 100% 确定推理,但我知道修复有效。

修复:

在此 Activity 的 list 中,您需要将其设为单个实例:

 <activity
        android:name=".TwitterTweetActivity"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="your-unique-schema-01-android" />
        </intent-filter>
    </activity>

现在,当您的 Activity 恢复时,您需要使用 onNewIntent() 捕捉它从浏览器返回的情况,如下所示:

 @Override
protected void onResume() {
    super.onResume();
    dealWithTwitterResponse(getIntent());
}

替换为:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    dealWithTwitterResponse(intent);
}
 

它应该可以工作!

推理(如有错误请指正):

所以问题是您的 rToken 对象与创建时的对象不同...

当您创建 Twitter 实例并获取 rToken 对象时,这是在您的 Activity 中。然后该 Activity 进入后台 (onPause),同时浏览器出现以供用户登录。

当重新创建 Activity 时,rToken 对象不同因此这就是 getOAuthAccessToken() 方法抛出错误的原因。

额外学分:

我刚刚编写了一个教程来匹配:How to send Tweets on Android from a Users Acc

关于android - 为什么方法 getOAuthAccessToken 总是在 twitter4j api 中触发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6065108/

相关文章:

c# - 需要代理身份验证(Forefront TMG 需要授权才能完成请求。拒绝访问 Web 代理筛选器。)

ios - 在iOS上,如何获取用户也在Twitter上关注的关注者列表?

android - 属性 application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) 来自 [com.android.support :support-compat:28. 0.0]

android - 为什么我的 Activity 会泄漏?

android - 应用程序列表中显示错误的 app_name

twitter - 向 Twitter 的 oauth/authorize 发送额外参数

jquery - 基本 Twitter 搜索出现 profile_image_url 未定义错误

android - ConstraintLayout - 让元素占用必要的空间直到可用空间

ios - Twitter登录解析iOS

java - 通过 Twitter 对 Web 应用程序的用户进行身份验证