简体   繁体   中英

android twitter4j callback don't work

i have a problem with callback after authenticate with twitter. I get token on onNewItem but web page remain.

This is my code

public class TwitterRequest extends Activity {

    private Twitter niceTwitter;
    public RequestToken niceRequestToken;
    private Twitter twitter;
    private RequestToken requestToken;
    public final static String TWIT_KEY = "dsadsdsdsads";

    public final static String TWIT_SECRET = "sdsdsdsd";

    public final static String TWIT_URL = "callbackapp://tweeter";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        niceTwitter = new TwitterFactory().getInstance();

        niceTwitter.setOAuthConsumer(TWIT_KEY, TWIT_SECRET);

        niceRequestToken = null;

        try {
            niceRequestToken = niceTwitter.getOAuthRequestToken(TWIT_URL);
        } catch (TwitterException e) {
            e.printStackTrace();
            Dbg.p("niceRequestToken: " + niceRequestToken);
        }


        String authURL = niceRequestToken.getAuthenticationURL();

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));

    }

    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        Uri twitURI = intent.getData();
        Intent response = new Intent();

        if (twitURI != null && twitURI.toString().startsWith(TWIT_URL)) {

            String oaVerifier = twitURI.getQueryParameter("oauth_verifier");

            try {

                AccessToken accToken = niceTwitter.getOAuthAccessToken(
                        niceRequestToken, oaVerifier);

                String token = accToken.getToken();

                response.putExtra("token", token);

            } catch (TwitterException te) {
                Log.e("tag",
                        "Failed to get access token: " + te.getMessage());
                response.putExtra("token", "error");
            }

            setResult(RESULT_OK, response);
            finish();
        }
    }

}

When web page is opened i login in twitter and i receive token but browser is visible and android don't close page.

this is my manifest

<activity
            android:name=".activities.twitter.TwitterRequest"
            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:host="tweeter"
                    android:scheme="callbackapp" />
            </intent-filter>
        </activity>

The problem is is that you start an new activity that starts the browser application.

At this point you have 2 activities. And when you call finish() on the first activity, the second will still stay.

I suggest you make a WebView in your activity, this way you control the webpage's visibility

try below modified manifest activity entry

<activity
            android:name=".activities.twitter.TwitterRequest"
            android:launchMode="singleTask" >
            <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="callback"
                    android:scheme="x-oauthflow-twitter" />
            </intent-filter>
        </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM