emeraldhieu
emeraldhieu

Reputation: 9439

Twitter doesn't redirect to Android app - Twitter4J

I want to connect to Twitter using Twitter4J. MainActivity call TwitterLoginActivity. TwitterLoginActivity show a WebView then get the accessToken throught OnNewIntent(). The problem is WebView show "web page not available" after I "SignIn" Twitter. This is my code.

MainActivity calls TwitterLoginActivity:

                final Intent intent = new Intent(MainActivity.this, TwitterLoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivityForResult(intent, Constant.LOGIN_REQUEST);

TwitterLoginActivity shows a WebView.

    final WebView twitterSite = new WebView(this);
    final ViewGroup viewGroup = (ViewGroup)findViewById(R.id.twitterLoginRoot);
    viewGroup.addView(twitterSite);

    // Open keyboard when focusing on Twitter login form.
    twitterSite.requestFocus(View.FOCUS_DOWN);
    twitterSite.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(final View v, final MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                    break;
            }
            return false;
        }
    });
    twitterSite.setWebViewClient(new CustomWebViewClient());

    try {
        final RequestToken requestToken = twitter.getOAuthRequestToken(Constant.CALLBACK_URL);
        twitterSite.loadUrl(requestToken.getAuthenticationURL());
    } catch (final TwitterException e) {
        e.printStackTrace();
    }

onNewIntent:

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    System.out.println("HIEU=================================");
    setResult(RESULT_OK);
    finish();
}

I use this to prevent default browser loading.

private class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
        view.loadUrl(url);
        return true;
    }  
}

Manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.twitterapitest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission
        android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name=".TwitterLoginActivity"
            android:label="@string/title_activity_main" 
            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="test123"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Callback URI:

public static final String CALLBACK_URL = "test123:///";

Upvotes: 3

Views: 2720

Answers (2)

Fernando Velazquez
Fernando Velazquez

Reputation: 301

I guess you want the "oauth_verifier" that twitter returns when your loggin is successful, right?

You can get it from the url that returns the shouldOverrideUrlLoading method. Do something like this:

webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        progressDialog.show();
    }

    @Override
    public void onPageFinished(WebView view, final String url) {
        progressDialog.dismiss();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String oauthVerifier = url.substring(url.indexOf("oauth_verifier=") + 15);
        Intent intent = new Intent(context);
        Bundle bundle = new Bundle();

        bundle.putString("oauth_verifier", oauthVerifier);
        intent.putExtras(bundle);

        ((Activity) ctx).setResult(RESULT_OK, intent);

        return true;
    }                 
});

I had a problem similar to yours. I didn't know how to call the onNewIntent into the WebViewClient so I had to rethink what really needed and in my conclusion I realized I just needed the oauth_verifier and I could get it from the returned URL.

I hope this help you :

Upvotes: 2

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Use below URL as your callback url, it will solve your problem.

public static final String CALLBACK_URL = "x-oauthflow-twitter://callback";

Upvotes: 0

Related Questions