Twitter Oauth回调getOAuthRequestToken为null

时间:2014-09-11 21:37:00

标签: android twitter twitter-oauth twitter4j

我在这里使用该应用的示例https://github.com/itog/Twitter4j-android-Sample并且它正在运行。我在我的应用程序中使用相同的方法,但是当我在行

时它返回null
static final String CALLBACK_URL = "oauth://t4jsample"; [Copy the same as the Sample]
requestToken = twitter.getOAuthRequestToken(CALLBACK_URL);

这是我的清单

<activity
        android:name="com.my.app.SharePreviewActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden|adjustPan" >

        <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:host="t4jsample"
                android:scheme="oauth" />
        </intent-filter>
    </activity>

这是我从示例应用程序中复制的其他代码。

private void askOAuth() {
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(CONSUMER_KEY);
    configurationBuilder.setOAuthConsumerSecret(CONSUMER_SECRET);
    Configuration configuration = configurationBuilder.build();
    twitter = new TwitterFactory(configuration).getInstance();

    try {
        requestToken = twitter.getOAuthRequestToken(CALLBACK_URL);
        Toast.makeText(this, "Please authorize this app!",
                Toast.LENGTH_LONG).show();
        this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                .parse(requestToken.getAuthenticationURL())));
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}

logcat的:

09-17 16:37:40.528: W/System.err(21125):    at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
09-17 16:37:40.528: W/System.err(21125):    at twitter4j.HttpClientBase.post(HttpClientBase.java:82)
09-17 16:37:40.529: W/System.err(21125):    at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:107)
09-17 16:37:40.530: W/System.err(21125):    at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:92)
09-17 16:37:40.531: W/System.err(21125):    at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:263)
09-17 16:37:40.532: W/System.err(21125):    at com.PhotoPreviewActivity.askOAuth(PPActivity.java:320)
09-17 16:37:40.532: W/System.err(21125):    at com.my.onShareTwitter(com.my.java:298)
09-17 16:37:40.533: W/System.err(21125):    at java.lang.reflect.Method.invokeNative(Native Method)
09-17 16:37:40.534: W/System.err(21125):    at java.lang.reflect.Method.invoke(Method.java:511)
09-17 16:37:40.535: W/System.err(21125):    at android.view.View$1.onClick(View.java:3687)
09-17 16:37:40.535: W/System.err(21125):    at android.view.View.performClick(View.java:4299)
09-17 16:37:40.536: W/System.err(21125):    at android.view.View$PerformClick.run(View.java:17549)
09-17 16:37:40.537: W/System.err(21125):    at android.os.Handler.handleCallback(Handler.java:725)
09-17 16:37:40.538: W/System.err(21125):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-17 16:37:40.539: W/System.err(21125):    at android.os.Looper.loop(Looper.java:153)
09-17 16:37:40.539: W/System.err(21125):    at android.app.ActivityThread.main(ActivityThread.java:5322)
09-17 16:37:40.540: W/System.err(21125):    at java.lang.reflect.Method.invokeNative(Native Method)
09-17 16:37:40.540: W/System.err(21125):    at java.lang.reflect.Method.invoke(Method.java:511)
09-17 16:37:40.541: W/System.err(21125):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:848)
09-17 16:37:40.541: W/System.err(21125):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
09-17 16:37:40.541: W/System.err(21125):    at dalvik.system.NativeStart.main(Native Method)
09-17 16:37:40.541: I/System.out(21125): [CDS] fix other exception in HttpUrlConnection
09-17 16:37:40.543: W/System.err(21125): null
09-17 16:37:40.543: W/System.err(21125): Relevant discussions can be found on the Internet at:
09-17 16:37:40.543: W/System.err(21125):    http://www.google.co.jp/search?q=8e063946 or
09-17 16:37:40.543: W/System.err(21125):    http://www.google.co.jp/search?q=c60cdc59
09-17 16:37:40.544: W/System.err(21125): TwitterException{exceptionCode=[8e063946-c60cdc59 8e063946-c60cdc2f], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.2}

2 个答案:

答案 0 :(得分:5)

经过一段时间的努力,我设法发现我忘了

  • 勾选了twitter jar库(傻傻的我!)
  • 为HTTP添加了安全性

您需要设置适用于您应用的最低sdk

if (android.os.Build.VERSION.SDK_INT > 8) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

瞧它会奏效。

答案 1 :(得分:2)

最好使用SocialAuth for android,如果你只是要使用第三方SDK进行开发,因为它提供了更多。现在我认为你的问题是获得开发者推特账号。