Twitter Oauth Callback getOAuthRequestToken null
Asked Answered
H

2

5

I am using the sample for the app here https://github.com/itog/Twitter4j-android-Sample and it is working. I use the same method in my app but it return null when I at line

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

Here are my manifest

<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>

This is other code which I copy the same from the sample app.

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}
Hadji answered 11/9, 2014 at 21:37 Comment(1)
walk through it, step by step blog.blundell-apps.com/sending-a-tweetJasmine
H
5

After a big time struggle, I manage to find that I forgot to

  • Ticked the twitter jar library (silly me!)
  • Added security to HTTP

You need to set the minimum sdk available for your app

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

and voila it will worked.

Hadji answered 20/9, 2014 at 0:14 Comment(0)
N
2

It's better to use SocialAuth for android instead if you are just gonna use a 3rd party SDK for the development since it offers much more. Right now I think your problem is getting a developer twitter account.

Nakada answered 11/9, 2014 at 21:48 Comment(1)
I got a developer twitter account. I am using twitter4j for API as well.Hadji

© 2022 - 2024 — McMap. All rights reserved.